Azphyxia's blog

By Azphyxia, history, 6 weeks ago, In English

include

include

include

using namespace std;

//This is solution to the problem 41A //Please help

int main() { string s1; string s2; char user[]={}; char arr[]={}; cin >> s1; unsigned int last; last = s1.length() — 1 ;

for(int j = 0;j<s1.length();j++)
{
    cin >> user[j];
}

int counter;

for (int q = 0;q<s1.length();q++)
{
   if(user[q]==s1[last - q])
   {
    counter = counter + 1;
   }
}

if (counter == s1.length())
{
    cout << "YES"<< endl;
}
else 
{
    cout << "NO"<< endl;
}

//cout << counter;

}

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

»
6 weeks ago, # |
  Vote: I like it 0 Vote: I do not like it

counter isn't initialized to any value.

  • »
    »
    6 weeks ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    That will give you a garbage value, but won't throw an error.

    • »
      »
      »
      6 weeks ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      Yes, that wont be a big issue.

      And I am getting runtime error on CF(0ms).

»
6 weeks ago, # |
  Vote: I like it 0 Vote: I do not like it

CF explicitly tells you what the issue is: out of bounds

You are trying to take input in $$$user[j]$$$ but the array $$$user$$$ does not have the index $$$j$$$, that is, $$$j \ge size\_of\_user$$$.

  • »
    »
    6 weeks ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Can you please elaborate ?

    • »
      »
      »
      6 weeks ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      What is the size of the $$$user$$$ array?

      • »
        »
        »
        »
        5 weeks ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        Number of characters in string s1( s1 size may vary) is size of user array

        • »
          »
          »
          »
          »
          5 weeks ago, # ^ |
            Vote: I like it 0 Vote: I do not like it

          Avoid changing the index of values as much as possible. I faced the exact same problem. Try and let me know. Btw, even I am a beginner, so I don't know the exact reason as to why this happens. I know it is wrong to answer questions if I am not rigorous with my approach, but if it helps you, please try it out. Also, if anyone could help me with the logic of it, I would be more than thankful to you. Peace.