Arcer's blog

By Arcer, history, 6 months ago, In English

I was just trying the problem 1941C - Рудольф и некрасивая строка and the language used was C++. This is my code, 263228103

#include <bits/stdc++.h>
using namespace std;
int main() {
    int t, n, c = 0;
    cin >> t;
    string s, s1 = "map", s2 = "pie", s3 = "mapie";
    for (int i = 0; i < t; i++) {
        cin >> n;
        cin >> s;
        for (int j = 0; j < n - 2; j++) {
            if (s.find(s3) != string::npos) {
                c++;
                s.erase(s.begin() + s.find(s3) + 2);
            }
            if (s.find(s1) != string::npos) {
                c++;
                s.erase(s.begin() + s.find(s1) + 1);
            }
            if (s.find(s2) != string::npos) {
                c++;
                s.erase(s.begin() + s.find(s2) + 1);
            }
        }
        cout << c << endl;
        c = 0;
    }
}

It passed all my tests but somehow it gets a Wrong answer at testcase 3. Can someone please figure out the mistake made?

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

»
6 months ago, # |
Rev. 2   Vote: I like it -11 Vote: I do not like it
#include <bits/stdc++.h>
using namespace std;
int main() {
    int t, n, c = 0;
    cin >> t;
    string s, s1 = "map", s2 = "pie", s3 = "mapie";
    for (int i = 0; i < t; i++) {
        cin >> n;
        cin >> s;
         for (int j = 0; j <= n - 3; ) {
            string ss = s.substr(j, 3);
            if (ss == s1 || ss == s2) {
                c++;
                j += 3; 
            } else {
                j++; 
            }
        }
        cout << c << endl;
        c = 0;
    }
}

change like this when you find a string matching with s1 or s2 when you standing at first character then if you remove the third character no need to check the string from second character directly jump the next of third character . no need of s3

accepted solution : 263238695

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

I am not able to find the mistake but a simpler solution which comes to my mind is just count the number of times map and pie occurs as a substring and subtract the number of times mapie comes as a substring. You dont need to print the string just count the number of characters to be deleted so no need to actually remove the characters.

Here's my submission to the problem hope it helps 250721773

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

    That is indeed simpler than my solution, thank you