-1

https://www.codechef.com/problems/XYSTR my code is

#include <bits/stdc++.h>

using namespace std;

int main() {
int t;
cin >> t;

while (t--) {
    string str;
    int flag = 0, num = 0;
    cin >> str;
    for (int i = 0; str[i] != '\0'; i++) {//xy xy xy xx xy yx
                                          //1  1  1     1  1 
        if(str[i]== 'x' && str[i+1] == 'y') {
           num++;
           i++;
       }
       if(  str[i] == 'y' && str[i+1] == 'x') {
           num++;
           i++;
       }
    }
    cout << num<< endl;
}}

Please help! I am not getting where the mistake is. Please, point it out.

rawrex
  • 4,044
  • 2
  • 8
  • 24
  • 9
    IMO your biggest mistake is using a so-called competition site for learning programming and C++. That's not what such sites are for, and which is why they are chock-full of really bad code as examples, bad code which could form bad habits, and such habits are so bad they could make you virtually unemployable. Read [good books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) and take classes to learn programming and C++ properly. Including things like ***debugging*** (which is what you need to do here). – Some programmer dude Jun 21 '21 at 07:19
  • Other than that, welcome to stackoverflow.com. Please take some time to read [the help pages](http://stackoverflow.com/help), especially the sections named ["What topics can I ask about here?"](http://stackoverflow.com/help/on-topic) and ["What types of questions should I avoid asking?"](http://stackoverflow.com/help/dont-ask). Also please take the [tour] and read about [ask] good questions. Lastly please read [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). – Some programmer dude Jun 21 '21 at 07:20
  • 1
    [Why should I not `#include `?](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) [Why is `using namespace std;` considered bad practice?](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – Evg Jun 21 '21 at 07:25
  • 2
    You get 2 for "xyx", but it should be 1. That's a small enough case for working through the code by hand. (Hint: think carefully about when the second conditional is executed.) – molbdnilo Jun 21 '21 at 07:25
  • 2
    please provide a [mre] within the question without relying on external links. What is the code supposed to do? What is the input? What is the expected and actual output? – Alan Birtles Jun 21 '21 at 07:26

1 Answers1

0

I think you are approaching the problem wrong. What you are trying to do is finding out number of occurrences where x and y are next to each other. The idea isn't to find that but to find possible combinations in which different x are paired with different y. So, one has to count number of x and y, and then do some math to find how many different combinations exit for x and y to be paired with each other. In the example referred in the link also, it is clear that there are no combinations when one of x or y are missing, and multiple combinations when either of x or y have multiple occurrences, irrespective of the order they are present in the original array.

  • 2
    I think you haven't read the problem description very carefully - only "next to each other" characters can make a pair. – molbdnilo Jun 21 '21 at 08:12
  • @molbdnilo but the problem isn't to find the number of such pairs in the input string as is tried in above program. But to find count of combinations in which such pairs can be arranged. – Vineet Gupta Jun 21 '21 at 18:38
  • "Students standing next to each other in the row are friends. [...] Two students can only form a pair if they are friends." (A missing `else` is the entire problem.) – molbdnilo Jun 21 '21 at 19:16