red_coder's blog

By red_coder, 12 years ago, In English

here is a question from codechef...

and here is my code ....

int main()
{
    int t;
    si(t);
    while(t--)
    {
        string A,B;
        cin.ignore();
        getline(cin,A);
        cin>>B;
        for(int i=0;i<A.size();i++)
        {
          if(A[i]==' ')
          continue;
          A[i]= B[(A[i]-'A')];
        }
        cout<<A<<"\n";
    }
    return 0;
}

why am i getting wrong answer afterall i am getting everything correct on my local compiler....

  • Vote: I like it
  • -23
  • Vote: I do not like it

| Write comment?
»
12 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Can first line of test has symbols as "." "," "!" ?

  • »
    »
    12 years ago, # ^ |
      Vote: I like it -7 Vote: I do not like it

    it would contain only Capital characters and spaces..

»
12 years ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

i have still not got the answer to my question... Why am i getting wrong answer????

  • »
    »
    12 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    What is si(t) in your code?

    • »
      »
      »
      12 years ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      its for scanf().. i have used a macro

      define si(n) scanf("%d",&n)

      • »
        »
        »
        »
        12 years ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        It is bad to use scanf and cin simultaneously.

  • »
    »
    12 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    another question, even 2:

    1. Where is definition of si(int &)?

    2. Why don't use a magical thing (which is only known by me?) called "debugger"?

  • »
    »
    12 years ago, # ^ |
    Rev. 2   Vote: I like it 0 Vote: I do not like it

    Try putting the cin.ignore() just before the while loop starts. Edit: I'm wrong, thought you had 2 getlines in there.

»
12 years ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

Maybe I missed something, but for input

2
FDY GAI BG UKMY
KIMHOTSQYRLCUZPAGWJNBVDXEF
FDY GAI BG UKMY
KIMHOTSQYRLCUZPAGWJNBVDXEF

your program returns

SKY IS BLUE

I tried this submission...

  • »
    »
    12 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    no, it returns THE SKY IS BLUE two times (for two test cases)

    • »
      »
      »
      12 years ago, # ^ |
      Rev. 3   Vote: I like it 0 Vote: I do not like it

      no, it's working as I described on my Windows XP, can someone else cofirm this ?

      I tried on 2 PCs

      g++-3 -v
      gcc version 3.4.4 (cygming special, gdc 0.12, using dmd 0.125)

      same with

      g++-4 -v
      gcc version 4.5.3 (GCC)
      • »
        »
        »
        »
        12 years ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        i have used online IDE as well as my own compiler in both cases it returns THE SKY IS BLUE SEE THIS

        • »
          »
          »
          »
          »
          12 years ago, # ^ |
            Vote: I like it 0 Vote: I do not like it

          I just tried to help, I believe that when you replace your getline and strings with gets and char arrays, your solution will be accepted.

          I'm not C/C++ guru, but ideone is using gcc (4.3.4), codechef is using g++ (4.3.2), I also used g++ and it didn't work. What compiler are you using?

          • »
            »
            »
            »
            »
            »
            12 years ago, # ^ |
              Vote: I like it 0 Vote: I do not like it

            gets is not a good function, it is even not recommended as far as i know. It's better not to use scanf and cin at the same time.

            In addition, using string::size() in cycle condition is a bad practice because this function takes linear time.

            • »
              »
              »
              »
              »
              »
              »
              12 years ago, # ^ |
                Vote: I like it 0 Vote: I do not like it

              AFAIK gets is not recommended because it's not checking bounds, for programming contests it's ok I guess.

              You can tell us what to use rather than what not to use ;-)

              • »
                »
                »
                »
                »
                »
                »
                »
                12 years ago, # ^ |
                  Vote: I like it 0 Vote: I do not like it

                I said that you should not combine cin and scanf.

                In my opinion, cin and string more convenient than gets and char arrays. I think that author of this topic thinks the same because he uses cin in his code.

                • »
                  »
                  »
                  »
                  »
                  »
                  »
                  »
                  »
                  12 years ago, # ^ |
                    Vote: I like it 0 Vote: I do not like it

                  When I try this, still not working

                  int main()
                  {
                      int t;
                      cin >> t;
                      while(t--)
                      {
                          string A,B;
                          cin.ignore(10000, '\n');
                          getline(cin,A);
                          cin >> B;
                          for(int i=0;i<A.size();i++)
                          {
                            if(A[i]==' ')
                              continue;
                            A[i]= B[(A[i]-'A')];
                          }
                          cout << A << endl;
                      }
                      return 0;
                  }
                  

                  for my input above I'm getting

                  THE SKY IS BLUE,
                  THE SKY IS BLUE,
                  
              • »
                »
                »
                »
                »
                »
                »
                »
                12 years ago, # ^ |
                  Vote: I like it 0 Vote: I do not like it
                #include <cstdio>
                #include <iostream>
                #include <string>
                
                using namespace std;
                
                int main()
                {
                    int t;
                    cin >> t;
                    while(t--)
                    {
                        string A,B;
                        cin.ignore(10000, '\n');
                        getline(cin,A);
                        cin >> B;
                        for(int i=0;i<A.size();i++)
                        {
                          if(A[i]==' ')
                            continue;
                          A[i]= B[(A[i]-'A')];
                        }
                        cout << A << endl;
                    }
                    return 0;
                }
                
                

                Try this code with includes, it is working on VC++2005 on codeforces server.

            • »
              »
              »
              »
              »
              »
              »
              12 years ago, # ^ |
                Vote: I like it +3 Vote: I do not like it

              size() is OK, strlen works in O(n)