CF872 Div2
比赛地址
讲解视频
A
方法一 : 暴力判断枚举子串 ,判断其是否回文 ,O( \(\frac 14n^3\))
方法二 : 整个串已知是一个回文串 ,贪心地想前 \(n - 1\) 构不构成回文串 , 若不构成 , 则答案就是 \(n - 1\)
若前 \(n - 1\) 构成回文,且 \(n\) 构成回文串 , 类似于 $s[n] = s[1] = s[n - 1] = s[2] ... $ , 全串都相同
#include <bits/stdc++.h>
using namespace std;
int main()
{
ios::sync_with_stdio(false);
cin.tie(0) ; cout.tie(0);
int t;
cin >> t;
while(t--){
string s;
cin >> s;
map<char,bool> ma;
for(int i = 0 ; i < s.length() ; ++i)
ma[s[i]] = true;
if(ma.size() == 1) cout << "-1\n";
else cout << s.length() - 1 << '\n';
}
return 0;
}