HDU 5371 Hotaru's problem(Manacher算法)

Hotaru's problem

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s):0    Accepted Submission(s): 0


Problem Description
Hotaru Ichijou recently is addicated to math problems. Now she is playing with N-sequence.
Let's define N-sequence, which is composed with three parts and satisfied with the following condition:
1. the first part is the same as the thrid part,
2. the first part and the second part are symmetrical.
for example, the sequence 2,3,4,4,3,2,2,3,4 is a N-sequence, which the first part 2,3,4 is the same as the thrid part 2,3,4, the first part 2,3,4 and the second part 4,3,2 are symmetrical.

Give you n positive intergers, your task is to find the largest continuous sub-sequence, which is N-sequence.
 

Input
There are multiple test cases. The first line of input contains an integer T(T<=20), indicating the number of test cases. 

For each test case:

the first line of input contains a positive integer N(1<=N<=100000), the length of a given sequence

the second line includes N non-negative integers ,each interger is no larger than  109  , descripting a sequence.
 

Output
Each case contains only one line. Each line should start with “Case #i: ”,with i implying the case number, followed by a integer, the largest length of N-sequence.

We guarantee that the sum of all answers is less than 800000.
 

Sample Input
  
  
1 10 2 3 4 4 3 2 2 3 4 4
 

Sample Output
  
  
Case #1: 9
 

Source
 

题目大意:给出一个字符串,求出它的最长子串,它的子串满足,将它的子串分成三部分,第一部分与第二部分对称,第一部分与第三部分相同。


解题思路:先用求回文串的Manacher算法,求出以第i个点和第i+1个点为中心的回文串长度,记录到数组c中 

比如 10 9 8 8 9 10 10 9 8 我们通过运行Manacher求出第i个点和第i+1个点为中心的回文串长度 0 0 6 0 0 6 0 0 0

两个8为中心,10 9 8 8 9 10是个回文串,长度是6。 两个10为中心,8 9 10 10 9 8是个回文串,长度是6。

要满足题目所要求的内容,需要使得两个相邻的回文串,共享中间的一部分,比如上边的两个字符串,共享 8 9 10这

一部分。 也就是说,左边的回文串长度的一半,要大于等于共享部分的长度,右边回文串也是一样。 因为我们已经

记录下来以第i个点和第i+1个点为中心的回文串长度, 那么问题可以转化成,相距x的两个数a[i],a[i+x],满足a[i]/2>=x

 并且 a[i+x]/2>=x,要求x尽量大

这可以用一个set维护,一开始集合为空,依次取出a数组中最大的元素,将其下标放入set中,每取出一个元素,再该

集合中二分查找比i+a[i]/2小,但最大的元素,更新答案。 然后查找集合中比i-a[i]/2大,但最小的元素,更新答案。

答案就是3*ans


代码如下:

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <numeric>
#include <iomanip>
#include <bitset>
#include <sstream>
#include <fstream>
#include <limits.h>
#define debug "output for debug\n"
#define pi (acos(-1.0))
#define eps (1e-6)
#define inf (1<<28)
#define sqr(x) (x) * (x)
#define mod 1000000007
using namespace std;
typedef long long ll;
typedef unsigned long long ULL;
#define MAX 100005
int n,p[2*MAX],s[MAX],s1[2*MAX];
void Manacher()
{
    int i,k,mx=0,id=0;
    memset(s1,0,sizeof(s1));
    s1[0]=-1;s1[1]=-2;
    for(i=0,k=2;i<n;i++)
    {
        s1[k++]=s[i];
        s1[k++]=-2;
    }
    p[0]=0;
    for(i=0;i<2*n+1;i++)
    {
        if(mx>i)
            p[i]=min(p[2*id-i],mx-i);
        else
            p[i]=1;
        while(s1[i-p[i]]==s1[i+p[i]])
            p[i]++;
        if(mx<p[i]+i)
        {
            mx=p[i]+i;
            id=i;
        }
    }
}
int main()
{
    int i,j,k,t,ans,cas=1;
    scanf("%d",&t);
    while(t--)
    {
        memset(s,0,sizeof(s));
        memset(p,0,sizeof(p));
        scanf("%d",&n);
        for(i=0;i<n;i++)
            scanf("%d",&s[i]);
        Manacher();
        ans=0;
        for(i=1;i<2*n+1;i+=2)
        {
            if((p[i]-1)/2>ans)
            {
                for(j=i+p[i]-1;;j-=2)
                {
                    if(p[j]>=j-i)
                    {
                        ans=(j-i)/2;
                        break;
                    }
                    if((j-i)/2<=ans)
                        break;
                }
            }
        }
        printf("Case #%d: %d\n",cas++,3*ans);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值