UVA 11584 划分回文串 (动态规划+字符串预处理)

We say a sequence of characters is a palindrome if it is the same written forwards and backwards. For example, ‘racecar’ is a palindrome, but ‘fastcar’ is not. A partition of a sequence of characters is a list of one or more disjoint non-empty groups of consecutive characters whose concatenation yields the initial sequence. For example, (‘race’, ‘car’) is a partition of ‘racecar’ into two groups. Given a sequence of characters, we can always create a partition of these characters such that each group in the partition is a palindrome! Given this observation it is natural to ask: what is the minimum number of groups needed for a given string such that every group is a palindrome?
For example: • ‘racecar’ is already a palindrome, therefore it can be partitioned into one group. • ‘fastcar’ does not contain any non-trivial palindromes, so it must be partitioned as (‘f’, ‘a’, ‘s’, ‘t’, ‘c’, ‘a’, ‘r’). • ‘aaadbccb’ can be partitioned as (‘aaa’, ‘d’, ‘bccb’).
Input
Input begins with the number n of test cases. Each test case consists of a single line of between 1 and 1000 lowercase letters, with no whitespace within.
Output
For each test case, output a line containing the minimum number of groups required to partition the input into groups of palindromes.
Sample Input
3 racecar fastcar aaadbccb
Sample Output
1 7 3
#include <queue>
#include <iostream>
#include<cstring>
#include<stdio.h>
#define maxn 1004
using namespace std;
int n,dp[maxn];
bool table[maxn][maxn];
char seq[maxn*2];

int trans(int i,int j)
{
    if(table[i][j]) return 1;
    return j-i+1;
}
/*
题目大义:
给定一个字符串,问最少能分解成多少个回文串


动态规划部分的思想很简单,
两重循环即可,如果预处理知道任意区间的
字符串是否为回文的即可。

重点是如何预处理得知任意区间的字符串是否为回文的,
这里采用枚举中心的方法,
但普通的方法仅对奇数长度的字符串有效,
而对于偶数长度的回文字符串却不奏效。

所以引入一种处理方法,
即在每两个字符之间插入一个特殊字符,
对新字符串进行扫描判断判断,
最终得出的回文区间i,j映射3到真实区间i>>1,(j-1)>>1;
(最终区间范围是0~l-1)
*/

int main()
{
    ios::sync_with_stdio(false);
    int t;scanf("%d",&t);
    while(t--)
    {
        seq[0]='#';
        scanf("%s",seq);
        int l=strlen(seq);

        for(int i=l;i>=0;i--) seq[(i<<1)+1]=seq[i];
        for(int i=0;i<=2*l-1;i+=2) seq[i]='#';

        memset(table,0,sizeof(table));
        for(int i=0;i<=2*l-1;i++)
        {
            int ll=i,rr=i;
            while(ll>=0&&rr<=2*l-1&&seq[ll]==seq[rr])
            {
                table[ll>>1][(rr-1)>>1]=true;
                ll--,rr++;
            }
        }

        /*
        for(int i=0;i<l;i++)
            for(int j=0;j<=i;j++)
              if(table[j][i]) cout<<j<<" "<<i<<endl;*/
        dp[0]=1;
        for(int i=0;i<l;i++)
        {
            dp[i]=trans(0,i);
            for(int j=0;j<i;j++)
            {
                if(!table[j+1][i]) continue;
                dp[i]=min(dp[i],dp[j]+1);
            }
        }
        printf("%d\n",dp[l-1]);
    }
    return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值