-1
#include <bits/stdc++.h>
using namespace std;

string solve(string s){
    cout << "inside solve";
    for(int i=0; i<s.length(); i++){
        int occur=0;
        while(i+1 < s.length()){
            if(s[i] == s[i+1])
                occur++;
        }
        if(occur%2!=0 && occur%3!=0 && occur%5!=0)
            return "no";
    }
    cout << "inside solve";
    return "yes";
}

int main(){
    ios_base::sync_with_stdio(0);
    //cin.tie(0); cout.tie(0);
    int tc;
    cin >> tc;
    //cout << tc;
    while(tc--){
        string s;
        cin >> s;
        //cout << "Inside main's while";
        cout << solve(s);
    }
    return 0;
}

In my main function after I'm inputting the string nothing is happening. Is there some problem in using cin >> s. Also when I'm uncommenting the line cin.tie(0); cout.tie(0); It is not going inside while loop too. So what is happening?

  • `s[i] == s[i+1]` will at some point reference memory outside the string, which is undefined behavior. Also: [What is a debugger?](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems') – Lala5th Apr 23 '22 at 06:57
  • 4
    A common problem, unfortunately. You seem to be trying to teach yourself C++ with code from competition sites. Competition code is not intended to teach. It is intended to win a competition. It's quickly dashed off due to speed constraints, has practically no descriptive text, and often has bugs that the judging system is not looking for, and thus qualifies as a correct answer when it is actually terribly flawed. What you want to do is learn C++ from [reputable sources](https://stackoverflow.com/questions/388242) and then, if you want, compete for fun, practice, of just to challenge yourself. – user4581301 Apr 23 '22 at 06:59
  • 4
    Obligatory reading: [Why should I not #include ?](https://stackoverflow.com/q/31816095/2752075), [Why is "using namespace std;" considered bad practice?](https://stackoverflow.com/q/1452721/2752075). – HolyBlackCat Apr 23 '22 at 07:01
  • `while(i+1 < s.length())` should restrict the range to keep `i+1` in bounds. Plus not incrementing `i` in the `while` loop helps keep it from ever getting anywhere, let alone out of bounds. – user4581301 Apr 23 '22 at 07:04

1 Answers1

1

You have an endless loop here:

        while(i+1 < s.length()){
            if(s[i] == s[i+1])
                occur++;
        }

'i' is not modified in the lopp. The loop will run forever. The condition is always true.

Use a second variable k=i+1, take this in the while loop and compare s[i] with s[k++].

Do not continue to work with these so called 'competition sides'

A M
  • 14,694
  • 5
  • 19
  • 44