hrbust2363 Symmys(manacher+dp)

Symmys

Time Limit: 1000 MS Memory Limit: 262144 K
Total Submit: 10(2 users) Total Accepted: 1(1 users) Rating: Special Judge: No
Description
ClearY is not good at English. He often fails in his English test. To help him study English, his English teacher asks him to rewrite some sentence. But ClearY doesn’t want to rewrite; he wants to play games. He comes up with a solution to complete rewriting earlier. He uses both his hands to write but he can only write the same letter at the same time. Every time, he chooses a symmetry center, using his left hand writing from this position to left continually and using his right hand writing from this position to right continually. He doesn’t not care about writing same letter two or more times in one place. He wants to know how many symmetry center at least he need to choose to complete his work.

Input

First line contains an integer T (1 ≤ T ≤ 1000), represents there are T test cases.
For each test case: one line contains a string of up to 106 lower case letters in length. It’s guaranteed that the sum length of all string will not exceed 5*106.

Output

For each test case, output one line containing “Case #X: Y”(without quotes), where X is the case number starting from 1, and Y is the minimum number of symmetry center at least ClearY need to choose.

Sample Input

2
aba
abbaab

Sample Output

Case #1: 1
Case #2: 2

Hint

For the second sample case, he can write “abba”(position from 1 to 4) and “baab”(position from 3 to 6).

Source

“科林明伦杯”哈尔滨理工大学第七届程序设计团队赛

题意:求最少多少个对称点可以将原串完全覆盖

首先用manacher处理回文串,得出每个回文串的半径,然后用L[i]刷新覆盖i点的最左距离: L[i+len[i]1]=min(L[i+len[i]1],ilen+1) ,然后求一下后缀min,这样可以使得后面的较小值覆盖掉前面,f[i]记录1~i区间的答案,最后用个dp得出答案: f[i]=min(f[L[i]1]+1,f[i])

#include<stdio.h>
#include<iostream>
#include<string>
#include<string.h>
#include<algorithm>
#include<stdlib.h>
#include<math.h>
#include<vector>
#include<map>
using namespace std;
const int N=2e6+200;
const int M=1e6+200;
int rl[N],len,l[N],f[N];
char str[M],s[N];
int manacher()
{
    s[0]='$';
    s[1]='#';
    int n=2;
    for(int i=1;i<=len;i++)
    {
        s[n++]=str[i];
        s[n++]='#';
        rl[i]=rl[len+i]=0;
        l[i]=l[len+i]=0x3f3f3f3f;
        f[i]=f[len+i]=0x3f3f3f3f;
    }
    l[0]=l[2*len+1]=f[2*len+1]=0x3f3f3f3f;
    f[0]=0;
    s[n]='\0';
    n--;
    int maxr=0,pos=0,ans=0;
    for(int i=1;i<=n;i++)
    {
        if(i<maxr)
        rl[i]=min(rl[2*pos-i],maxr-i);
        else
        rl[i]=1;
        while(i-rl[i]>=0&&i+rl[i]<=n&&s[i+rl[i]]==s[i-rl[i]])rl[i]++;
        if(maxr<i+rl[i]-1)
        {
            maxr=i+rl[i]-1;
            pos=i;
        }
        l[i+rl[i]-1]=min(l[i+rl[i]-1],i-rl[i]+1);
    }
    for(int i=n-1;i>=1;i--)
           l[i]=min(l[i],l[i+1]);
    for(int i=1;i<=n;i++)
        f[i]=min(f[i],f[l[i]-1]+1);
    return f[n];

}
int main()
{
    int t,q=0;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%s",str+1);
        len=strlen(str+1);
        printf("Case #%d: %d\n",++q,manacher());
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值