Hotaru's problem(hdu5371+Manacher)多校7

Hotaru's problem

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


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
 


 

Author

 

UESTC
 


 

Source

 

 


 

 

关于manacher算法,链接:>>manacher<<

 

转载请注明出处:寻找&星空の孩子  

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5371

 

 

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define maxn 100010*2
const int mm=1e9+7;
int P[maxn];
//(p.s. 可以看出,P[i]-1正好是原字符串中回文串的总长度)
int s1[maxn];
int s2[maxn];
int n;
int min( int x, int y )//Wa,没加这个。。。
{
    return x < y ? x : y;
}

void manacher(int* s)
{
    int i,id=0,mx=0;
    P[0]=0;
    for(i=1;i<=2*n+1;i++)
    {
        if(mx > i)
            P[i] = min(P[2*id-i],mx-i);
        else
            P[i] = 1;
        while(s[i+P[i]]==s[i-P[i]] )
        {
            P[i]++;
        }
        if(mx < P[i] + i)
        {
            mx = P[i] + i;
            id = i;
        }
    }
}


void init(int n)
{
    int i, j = 2;
    s2[0] =-1, s2[1] = -2;

    for(i=0;i<n;i++)
    {
        s2[j++] = s1[i];
        s2[j++] = -2;
    }
    s2[j]=-3;
}

int main()
{
 //   printf("%d\n",mm);
    int T,ca=1;
    scanf("%d",&T);
    while(ca<=T)
    {
        scanf("%d",&n);
        for(int i=0;i<n;i++) scanf("%d",&s1[i]);
        init(n);
        manacher(s2);
        int ans=0;
        for(int i=1;i<=2*n+1;i+=2)
        {
 //           printf("i=%d\tP=%d\n",i,P[i]);

                for(int j=P[i]+i-1;j-i>ans;j-=2)
                {
                    if(j-i+1<=P[j])
                    {
                        ans=max(ans,j-i);
                        break;//坑
                    }
                }

        }
        printf("Case #%d: %d\n",ca++,ans/2*3);
    }
    return 0;
}
/*
2
10
2 3 4 4 3 2 2 3 4 4
7
1 2 3 2 1 2 3
*/


 

 

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值