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
#include
#include
#include
#include
#include
#include
#include
#include
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(i2*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(maxr1)
        {
            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());
    }
}

你可能感兴趣的:(【Manacher】,【动态规划】)