读入相应的字符串,然后以读入的每一个字符串的字符为起点,以构成偶数长度和奇数长度的方式来找回文串,找到之后对于该位置字符的计数进行相应的更新即可,具体实现见如下代码:
#include<iostream>
#include<vector>
#include<string>
#include<set>
#include<stack>
#include<queue>
#include<map>
#include<algorithm>
#include<cmath>
#include<iomanip>
#include<cstring>
#include<sstream>
#include<cstdio>
#include<deque>
using namespace std;
int n;
int dp[1010];
int main(){
cin >> n;
while (n--){
string s;
cin >> s;
fill(dp, dp + 1010, 1 << 20);
s = "0" + s;
dp[0] = 0;
for (int i = 1; i < s.size(); i++){
int left, right;
for (left = i, right = i; left > 0 && right < s.size() && s[left] == s[right]; left--, right++)
dp[right] = min(dp[right], dp[left - 1] + 1);
for (left = i, right = i + 1; left > 0 && right < s.size() && s[left] == s[right]; left--, right++)
dp[right] = min(dp[right], dp[left - 1] + 1);
}
cout << dp[s.size() - 1] << endl;
}
return 0;
}