hdu5371 Hotaru's problem

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

Total Submission(s): 2189    Accepted Submission(s): 774

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  ,
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

这题能够用Manacher算法做。由于题目要找的是三段(第一段和第二段对称,第二段和第三段对称)。事实上就是两个连在一起的回文串,我们能够先用Manacher算法初始化各个点的p[i]值(即能够向右延伸的最大距离。包含本身,这时已经增加了-1取代算法中的‘#‘,-2取代算法中的‘$‘),然后对于每一个i。枚举j(j属于1~p[i]-1),假设i+j-p[i+j]+1<=i,那么说明i。j能够分别作为第一、二段的点和第二、三段的点)。

这里有个优化,由于枚举时满足条件的仅仅有‘#‘(即‘-1’),所以我们能够使i,j每次变化2.
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<string>
#include<algorithm>
using namespace std;
#define maxn 100060
int a[maxn],b[2*maxn],p[2*maxn];
int main()
{
    int n,m,i,j,T,mx,idx,maxx,num1=0;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&n);
        for(i=0;i<n;i++){
            scanf("%d",&a[i]);
        }
        if(n<3){
            printf("0\n");continue;
        }
        b[0]=-2;
        b[1]=-1;
        for(i=0;i<n;i++){
            b[i*2+2]=a[i];
            b[i*2+3]=-1;
        }
        n=2*n+2;mx=0;
        for(i=0;i<n;i++){
            if(i<mx){
                p[i]=min(p[idx*2-i],mx-i);
            }
            else p[i]=1;
            while(b[i-p[i]]==b[i+p[i]]){
                p[i]++;
            }
            if(mx<i+p[i]){
                mx=i+p[i];
                idx=i;
            }
        }
        maxx=0;
        for(i=3;i<n;i+=2){
            for(j=p[i]-1;j>=1;j-=2){
                if(j<maxx)break;
                if(i+j-p[i+j]+1<=i){
                    maxx=max(maxx,j);break;
                }
            }

        }
        num1++;
        printf("Case #%d: ",num1);
        printf("%d\n",maxx/2*3);
    }
    return 0;
}

hdu5371 Hotaru's problem

时间: 2024-10-29 05:10:18

hdu5371 Hotaru&#39;s problem的相关文章

Hotaru&amp;#39;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 playin

HDU 5371 (2015多校联合训练赛第七场1003)Hotaru&amp;#39;s problem(manacher+二分/枚举)

pid=5371">HDU 5371 题意: 定义一个序列为N序列:这个序列按分作三部分,第一部分与第三部分同样,第一部分与第二部分对称. 如今给你一个长为n(n<10^5)的序列,求出该序列中N序列的最大长度. 思路: 来自官方题解:修正了一些题解错别字(误 先用求回文串的Manacher算法.求出以第i个点为中心的回文串长度.记录到数组p中 要满足题目所要求的内容.须要使得两个相邻的回文串,共享中间的一部分,也就是说.左边的回文串长度的一半,要大于等于共享部分的长度,右边回文串也

[2015hdu多校联赛补题]hdu5371 Hotaru&#39;s problem

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5371 题意:把一个数字串A翻过来(abc翻过来为cba)的操作为-A,我们称A-AA这样的串为N-sequence,现在给你一个数字串,问你这个串中最长的N-sequence子串长度 解:可以想到A-A是一个回文串,-AA也是一个回文串,那么首先Manacher跑一遍求出所有回文子串 可以想到任意两个互相覆盖的回文子串都可以表示成N-sequence 然后有三种搞法: 1.时间复杂度O(N*logN

hdu5371 Hotaru&#39;s problem(manacher 算法+枚举)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5371 题目大意:给一串数字,在子串中找到"1-2-1"的形式,其中1和2 是回文串,找出最长的那一串. 思路:利用manacher算法得出最长序列.观察子串形式,1和2是回文串,其实2和后面那个1也是回文串. 在之前我们已经通过manacher算法得到了每个数字所能延伸的长度,所以我们只要枚举第二段到第三段的距离即可. 当遇到第i个数字时,那么第二段和第三段的距离就是i+p[i],令j=i

CF #261 div2 D. Pashmak and Parmida&amp;#39;s problem (树状数组版)

Parmida is a clever girl and she wants to participate in Olympiads this year. Of course she wants her partner to be clever too (although he's not)! Parmida has prepared the following test problem for Pashmak. There is a sequence a that consists of n

programming-challenges Shoemaker&amp;#39;s Problem (110405) 题解

Greedy. 证明: Let's say we have job 1, 2, ..., n, and they have time and fine as t1, f1, t2, f2, ..., tn, fn and they are in the order of t1/f1 <= t2/f2 <= t3/f3 <= ... <= tn/fn So this is the objective schedule. Now we change 1 with m (1 < m

poj 2480 Longge&amp;#39;s problem 积性函数性质+欧拉函数

题意: 求f(n)=∑gcd(i, N) 1<=i <=N. 分析: f(n)是积性的数论上有证明(f(n)=sigma{1<=i<=N} gcd(i,N) = sigma{d | n}phi(n / d) * d ,后者是积性函数),能够这么解释:当d是n的因子时,设1至n内有a1,a2,..ak满足gcd(n,ai)==d,那么d这个因子贡献是d*k,接下来证明k=phi(n/d):设gcd(x,n)==d,那么gcd(x/d,n/d)==1,所以满足条件的x/d数目为phi(

POJ 2480 Longge&amp;#39;s problem 积性函数

题目来源:POJ 2480 Longge's problem 题意:求i从1到n的gcd(n, i)的和 思路:首先假设m, n 互质 gcd(i, n*m) = gcd(i, n)*gcd(i, m) 这是一个积性函数积性函数的和还是积性函数 由欧拉函数知识得 phi(p^a) = p^a - p^(a-1) p是素数 a是正整数 得到终于答案f(n) = f(p1^a1)*f(p2^a2)*...*f(pn^an) 当中f(p^a) = a*(p^a-p^(a-1))+p^a #includ

UVA - 10239 The Book-shelver&amp;#39;s Problem

Description Problem D The Book-shelver's Problem Input: standard input Output: standard output Time Limit: 5 seconds Memory Limit: 32 MB You are given a collection of books, which must be shelved in a library bookcase ordered (from top to bottom in t