UVA - 11584 Partitioning by Palindromes

来源:互联网 发布:上海的美食知乎 编辑:程序博客网 时间:2024/05/24 02:38

DP:

#include<cstdio>#include<iostream>#include<algorithm>#include<cstring>using namespace std;const int maxn = 1000 + 5;char s[maxn];int d[maxn];int solve(int a, int b){    while(a<b)    {        if(s[a] != s[b]) return 0;        ++a;        --b;    }    return 1;}int main(){    int T;    scanf("%d", &T);    while(T--)    {        memset(s,0,sizeof(s));        memset(d,0,sizeof(s));        scanf("%s", s+1);        int n = strlen(s+1);        for(int i = 1; i <= n; ++i)        {            d[i] = d[i-1] + 1;            for(int j = 1; j <= i; ++j)            {                if(solve(j, i)) d[i] = min(d[i], d[j-1] + 1);            }        }        printf("%d\n", d[n]);    }    return 0;}


0 0