UVa 11584 Partitioning by Palindromes(DP 最少回文串)

 

原题地址

题意  :

判断一个串最少可以分解为多少个对称串   一个串从左往后和从右往左是一样的  这个串就称为对沉串

思路:

令d[i]表示给定串的前i个字母至少可以分解为多少个对称串  那么对于j=1~i   (j,i)是一个对称串  那么有  d[i]=min(d[i],d[j-1]+1)   然后就得到答案了

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<set>
#include<queue>
#include<stack>
using namespace std;
typedef long long ll;
const int mod = 1e9+7;
const double PI = 4*atan(1.0);
const int maxm = 1e8+5;
const int maxn = 2e5+5;
const int INF = 0x3f3f3f3f;
const ll LINF = 1ll<<62;
char s[100001];
int dp[100001];
bool ishuiwen(int a,int b)//进行一下判断,判断i之前从j-i中是否为回文串,如果有,那么dp[i]=dp[j-1]+1和dp[i]中小的那个
{
    while(a<=b&&s[a]==s[b])
    {
        a++;
        b--;
    }
    return a>=b;
}
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        cin>>s+1;
        int l=strlen(s+1);
        memset(dp,0x3f3f3f3f,sizeof(dp));
        dp[0]=0;
        for(int i=1; i<=l; i++)
        {
            for(int j=1; j<=i; j++)
            {
                if(ishuiwen(j,i))
                    dp[i]=min(dp[i],dp[j-1]+1);
            }
        }
        cout<<dp[l]<<endl;

    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值