Poj Solution 2604

http://poj.org/problem?id=2604

#include <iostream>
#include <string>
#include <vector>

using namespace std;
typedef vector<string>::size_type VST;
vector<string> v;
char Stack[1000];
int pos[1000];
int vpos[1000];
int top = 0;
void process()
{
    bool bEnd;

    for (VST i = 0;i < v.size();i++)
    {
        if (v[i].empty())
        {
            if (top != 0)
            {
                while (top--)
                {
                    v[vpos[top]].erase(pos[top],1);
                }
                top++;
            }
            continue;
        }

        string &s = v[i];
        for (int j = 0;j < s.size();j++)
        {
            if (s[j] == '"')
            {
                if (top == 0)
                {
                    Stack[top] = '"';
                    pos[top] = j;
                    vpos[top++] = i;
                }
                else
                {
                    s[j] = 39;
                    s.insert(j,1,39);
                    v[vpos[--top]][pos[top]] = 96;
                    v[vpos[top]].insert(pos[top],1,96);
                    if(vpos[top] == i)
                        j += 2;
                    else
                        j += 1;
                }
                continue;
            }
            else if (s[j] == '\')
            {
                if (j + 1 < s.size() && s[j + 1] == '"')
                {
                    j += 2;
                    continue;
                }

                if (s.substr(j,4) == string("\par") || s.substr(j,9) == string("\endinput"))
                {
                    if (top != 0)
                    {
                        while (top--)
                        {
                            v[vpos[top]].erase(pos[top],1);
                        }
                        top++;
                        j = 0;
                        continue;
                    }
                }
            }
        }
    }
}

int main()
{
    string str;
    while (1)
    {
        getline(cin,str,'n');
        v.push_back(str);
        if (str.find("\endinput") != string::npos)
            break;
    }
    process();
    for(int i = 0;i < v.size();i++)
        cout<<v[i]<<endl;
    return 0;
}

/*
Preparing an article
Time Limit: 1000MS  Memory Limit: 65536K 
Total Submissions: 431  Accepted: 93 

Description

TeX is the leading typesetting system for mathematics, science, and engineering and has been adopted as standard by the American Mathematical Society. LaTeX was developed later by Leslie Lamport. It is based on TeX and provides a set of higher level commands for production of complex documents. In TeX or LaTeX, any text editor program may be used to enter and modify the input text. The source text contains the actual text as well as formatting commands beginning with . Commands are delimited by any non-alphabetic character. One example of beautification by TeX is that it uses `` (two left-single-quotes) and '' (two right-single-quotes) to delimit quotations, rather than the mundane " (one double quote) which is provided by most keyboards. Keyboards typically do not have an oriented double-quote, but they do have a left-single-quote (`) and right-single-quote ('). TeX lets the user type two left-single-quotes (``) to create a left-double-quote and two right-single-quotes ('') to create a right-double-quote. Now, you have a text only file containing at most 250 lines at most 80 symbols each, as source or input, and you want to use TeX to beautify it. Rather than doing everything by hand, as the first step of automation you want to convert the quotes into the TeX format by using a program. This program will convert the text with double-quotes (") into an identical text except that double quotes have been replaced by the two-character sequences required by TeX for delimiting quotations with oriented double-quotes. The double-quote (") characters should be replaced appropriately by proper double single quotes depending on whether it is an opening or closing quotation mark. Question of nested quotations does not arise. The first " must be replaced by ``, the next by '', the next by ``, the next by '', and so on. An opening double quote must have its closing quote in the same paragraph. If a match is not found in the same paragraph for an opening quote, this quote has to be deleted. Paragraph ends in the source text are marked either by at least one blank line, or a par command or both. Your program must also be careful about the " command which is used to produce umlaut or dieresis ("e leads to ?). These are to be left untouched.
Input

Input will consist of several lines of text containing a number of double quotes ("), as well as some TeX commands. End of input will be marked by an endinput command.
Output

Output will be an exact replica of the input, except the double quotes are to be modified according to the rules described above.
Sample Input

There is no "q in this sentence. par 
"Talk child," said the unicorn. 

She s"aid, "thinspace `Enough!', he said." 
endinput 

Sample Output

There is no q in this sentence. par 
``Talk child,'' said the unicorn. 

She s"aid, ``thinspace `Enough!', he said.'' 
endinput 

Hint

Double-quote (") has ASCII code 34, 
left-single-quote (`) has ASCII code 96, 
right-single-quote (') has ASCII code 39.

*/
											
This entry was posted in poj. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *