I was just trying the problem 1941C - Rudolf and the Ugly String 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?








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