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?
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
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
That is indeed simpler than my solution, thank you