mardi 7 juillet 2020

Wrong answer in UVA Online judge Problem Number 272 Tex quotes

I have tried to code this problem using c++ string class and also using normal character input. I have used the same logic in both of my solution but the former gets AC while the latter gets WA. I am not able to understand what problem is lying underneath. Please help me out.

Here is the problem link.

C++11 5.3.0 GNU C++ Compiler with options: -lm -lcrypt -O2 -std=c++11 -pipe -DONLINE_JUDGE

Abridged Problem Statement:

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

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.

Output

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!''

Here is the solution which got accepted:

#include "competitive.hpp" // All necessary header files have been included.

using namespace std;

string line , result;

int main() {
    bool open = false;
    while(getline(cin , line)) { result = "";
        for(char ch : line) {
        if(ch == '\"' && open == false) open = true;
            else if(ch == '\"' && open == true) open = false;
        else { result.push_back(ch); continue; }
            
        if(open) { result.push_back('`'); result.push_back('`'); }
        else { result.push_back('\''); result.push_back('\''); }
    }

    printf("%s\n" , result.c_str());
    result.clear();
    }
}

Here is the solution which is getting a wrong answer verdict:

#include "competitive.hpp"

using namespace std;

char ch = '\0';

int main() {

    freopen("in.txt" , "r" , stdin);
    freopen("out.txt" , "w" , stdout);

    bool opened = false;
    while(scanf("%c" , &ch) != EOF) {
        if(ch == '\"') {
            if(opened) { printf("\'\'"); opened = false; }
            else { printf("``"); opened = true; }
        }
        else printf("%c" , ch);
    }
    return 0;
}

Aucun commentaire:

Enregistrer un commentaire