#include <iostream>
#include <vector>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <cstring>
#include <unordered_map>
#include <unordered_set>
#include <algorithm>
#include <numeric>
#include <chrono>
#include <ctime>
#include <cmath>
#include <cctype>
#include <string>
#include <cstdio>
#include <iomanip>
#include <thread>
#include <mutex>
#include <condition_variable>
#include <functional>
#include <iterator>
using namespace std;
const int MAXSIZE = 1080;
char ch[MAXSIZE];
bool a[MAXSIZE][MAXSIZE];
int n,len,dp[MAXSIZE];
void Input() {
cin >> n;
cin.get();
}
void IsOk(int first,int last) {
while (first > 0 && last <= len && ch[first] == ch[last]) {
//first - last 是否为回文
a[first][last] = 1;
--first;
++last;
}
}
int main()
{
Input();
while (n--) {
gets(ch+1);
len = strlen(ch+1);
for (int i = 1; i <= len; ++i)
{
IsOk(i, i);
IsOk(i, i + 1);
}
dp[0] = 0;
for (int i = 1; i <= len; ++i) {
dp[i] = dp[i - 1] + 1;
for (int j = 0; j < i; ++j) {
if (a[j+1][i]) {
dp[i] = min(dp[i], dp[j] + 1);
}
}
}
cout << dp[len] << endl;
memset(a, 0, sizeof(a));
}
return 0;
}
07-15
12-25
06-01
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交