5月 04, 2008

【解題】TeX Quotes

@
ACM Volume II 272 - TeX Quotes


The Problem

TeX is a typesetting language developed by Donald Knuth. It takes source text together with a few typesetting instructions and produces, one hopes, a beautiful document. Beautiful documents use `` and " to delimit quotations, rather than the mundane " which is what is provided by most keyboards. Keyboards typically do not have an oriented double-quote, but they do have a left-single-quote ` and a right-single-quote '. Check your keyboard now to locate the left-single-quote key ` (sometimes called the ``backquote key") and the right-single-quote key ' (sometimes called the ``apostrophe" or just ``quote"). Be careful not to confuse the left-single-quote ` with the ``backslash" key \. 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 ''. Most typists, however, are accustomed to delimiting their quotations with the un-oriented double-quote ".

If the source contained:

"To be or not to be," quoth the bard, "that is the question."

then the typeset document produced by TeX would not contain the desired form:

``To be or not to be," quoth the bard, ``that is the question."

In order to produce the desired form, the source file must contain the sequence:

``To be or not to be,'' quoth the bard, ``that is the question.''

You are to write a program which converts text containing double-quote (") characters into text that is identical 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 either `` if the " opens a quotation and by '' if the " closes a quotation. Notice that the question of nested quotations does not arise: The first " must be replaced by ``, the next by '', the next by ``, the next by '', the next by ``, the next by '', and so on.


Input and Output

Input will consist of several lines of text containing an even number of double-quote (") characters. Input is ended with an end-of-file character. The text must be output exactly as it was input except that:

  * the first " in each pair is replaced by two ` characters: `` and   * the second " in each pair is replaced by two ' characters: ''.


Sample Input

"To be or not to be," quoth the Bard, "that
is the question".
The programming contestant replied: "I must disagree.
To `C' or not to `C', that is The Question!"


Sample Output

``To be or not to be,'' quoth the Bard, ``that
is the question''.
The programming contestant replied: ``I must disagree.
To `C' or not to `C', that is The Question!''


解題思考

  我的作法是,先使用一個 boolean 變數紀錄現在接著要輸出左單引還是右單引。

  再來,既然「"」才是重點,就直接使用 string 的搜尋函式找「"」。輸出之前的內容之後,再根據 boolean 變數判斷輸出的單引號。跳過輸出這個「"」之後重複上述動作,直到搜尋完字串為止。

  別懷疑,只要這樣這題就算完成了。


參考解答(C++)

#include <iostream>
#include <string>

using namespace std;

int main(void)
{
    bool quote = true;
    string str;

    while (getline(cin, str))
    {
        int search[2] = {0, 0};

        while ((search[1] = str.find("\"", search[0])) != string::npos)
        {
            cout << str.substr(search[0], search[1] - search[0]);

            if (quote)  { cout << "``"; }
            else        { cout << "''"; }

            quote = !quote;
            search[0] = search[1] + 1;
        }

        cout << str.substr(search[0], str.size() - search[0]);

        cout << endl;
    }

#ifndef ONLINE_JUDGE
    system("pause");
#endif
}

6 回覆:

Zac 提到...

你好酷... 台灣有希望了

Unknown 提到...

呃....有這麼誇張嗎?
總之是該感謝你的誇獎吧XD

匿名 提到...

「台灣有希望」怎麼有一種不好的感覺...希望大家還是要多靠靠自己啊!

Unknown 提到...

呃...我不懂你的意思

匿名 提到...

我的不對耶 為什麼?

#include

int main(void)
{
char text[1000];
while(gets(text)){
int i,count=1;
for(i=0;text[i]!='\0';i++){
if(text[i]=='"'){
if(count){putchar('`'); putchar('`');}
else {putchar('\''); putchar('\'');}
count=!count;
}
else{
putchar(text[i]);
}
}
putchar('\n');
}

return 0;
}

Unknown 提到...

你需要將原始程式的 int count = 1
移到 while(gets(text)) { ... } 之外喔

仔細看題目的 Sample Input,單行文字的雙引號不見得是偶數的
用你的程式來跑的話,讀入每一行都會重置 count 變數,於是:

"To be or not to be," quoth the Bard, "that
is the question".

這兩行輸入被讀取後會輸出:

``To be or not to be,'' quoth the Bard, ``that
is the question``.

但實際答案卻是:

``To be or not to be,'' quoth the Bard, ``that
is the question''.

注意到 question 後面引號的差異了嗎?

張貼留言