C. Canine poetry
思路
回文串破坏,如何最小破坏,就是把 l e n g t h = = 2 length == 2 length==2 和 l e n g t h = = 3 length == 3 length==3 的回文串破坏
AC(string)
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t -- ) {
string str;
cin >> str;
int len = str.size();
int ans = 0;
for (int i = 1; i < len; i ++ ) {
if (str[i] == str[i - 1]) {
ans ++;
str[i] = '0';
} else if (i >= 2 && str[i] == str[i - 2]) {
ans ++;
str[i] = '0';
}
}
cout << ans << endl;
}
return 0;
}