#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <climits>
using std::min;
const int MAXN(1010);
const int INFI((INT_MAX-1) >> 1);
char str[MAXN];
int table[MAXN];
bool is_palindromes(int l, int r)
{
while(l < r)
{
if(str[l] != str[r])
return false;
++l;
--r;
}
return true;
}
int main()
{
int T;
scanf("%d", &T);
while(T--)
{
scanf("%s", str+1);
int len = strlen(str+1);
for(int i = 1; i <= len; ++i)
{
table[i] = INFI;
for(int j = 0; j < i; ++j)
if(is_palindromes(j+1, i))
table[i] = min(table[i], table[j]+1);
}
printf("%d\n", table[len]);
}
return 0;
}
Partitioning by Palindromes
最新推荐文章于 2022-11-15 20:59:29 发布