akane86's blog

By akane86, history, 5 weeks ago, In English,

My code is correct and returns correct answer locally, but it gets wrong answer on Codeforces.


#include<bits/stdc++.h> using namespace std; int main(){ int g; vector<string> s; int grps; string l; cin >> g; while(g--){ cin >> l; s.push_back(l); } for(int i = 0; i < s.size()-1; i++){ if(s[i] != s[i+1]) grps++; } cout << grps++ << endl; }
 
 
 
 
  • Vote: I like it
  • 0
  • Vote: I do not like it

»
5 weeks ago, # |
Rev. 7   Vote: I like it 0 Vote: I do not like it

First: I think that the problem is with grps variable. Pay attention that the value of grps can be anything because you didn't give value to it when you declared it. The code will work if you set its value to 0 at the beginning of the code OR declare it outside of the main function.

Second: Your second mistake is that instead of grps++ (in the last line) you had to write ++grps. I hope you know why!...

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

    Yeah declaring grps locally without declaration(like int grps = 0) it could get initialized to anything (ie undefined behavior), and also expanding from above a = x++ means that a gets assigned to x and x increases, but a = ++x means that x increases to x+1 and a = x+1