UVA - 11584 Partitioning by Palindromes (线性结构上的动态规划)

题目连接:UVA - 11584 Partitioning by Palindromes

#include <bits/stdc++.h>
using namespace std;
char s[1005];
int dp[1005];
int check(int l, int r){
    while(l<r){
        if(s[l] != s[r]) return 0;
        ++l; --r;
    }
    return 1;
}
int main()
{
    std::ios::sync_with_stdio(0);
    int t, n, m, i, j, k;
    scanf("%d", &t);
    while(t--)
    {
        scanf("%s", s + 1);
        memset(dp, 0, sizeof(dp));
        int len = strlen(s + 1);
        for(i = 1; i <= len; i++)
        {
            dp[i] = 0x3f3f3f;
            for(j = 1; j <= i; j++)
            {
                if(check(j, i))dp[i] = min(dp[j - 1] + 1, dp[i]);
            }
        }
        printf("%d\n", dp[len]);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Miracle_QSH/article/details/81609963