【Good Bye 2020 C】Canine poetry

题目链接

链接

翻译

translation

题解

只要不存在长度为 \(2\)\(3\) 的回文, 就不会有更长的回文啦。

所以,每个位置都和前一个以及前前一个位置判断一下是否相同就好, 然后如果某个位置修改过。

就把它当做是一个通配符,不会和任何一个字符相同。

代码

#include <bits/stdc++.h>
#define LL long long
using namespace std;
 
const int N = 1e5;
 
int T;
string s;
int tag[N+10];
 
int main() {
	#ifdef LOCAL_DEFINE
		freopen("in.txt", "r", stdin);
	#endif
	ios::sync_with_stdio(0), cin.tie(0);
    cin >> T;
    while (T--){
        cin >> s;
        int len = s.size();
        for (int j = 0;j < len; j++){
            tag[j] = 0;
        }
        int ans = 0;
        for (int i = 0;i < len; i++){
            if (i > 0 && s[i] == s[i-1]){
                if (tag[i-1] == 0){
                    tag[i] = 1;
                    ans++;
                }
            }
            if (i > 1 && s[i] == s[i-2]){
                if (tag[i-2] == 0){
                    tag[i] = 1;
                    ans++;
                }
            }
        }
        cout << ans << endl;
    }
	return 0;
}
posted @ 2021-01-26 14:11  AWCXV  阅读(70)  评论(0编辑  收藏  举报