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

    这题能够用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
    ");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
    ",maxx/2*3);
        }
        return 0;
    }
    


  • 相关阅读:
    Spring Boot中整合Sharding-JDBC读写分离示例
    Spring Boot Admin2.X监控的服务context-path问题
    Spring Boot中整合Sharding-JDBC单库分表示例
    Spring Cloud Gateway 之 AddRequestHeader GatewayFilter Factory
    程序猿:论学习方式的重要性
    如何使用mybatis《三》
    如何使用mybatis《二》
    如何使用mybatis《一》
    mybatis中自建的类型别名
    mybatis中#和$符号的区别
  • 原文地址:https://www.cnblogs.com/zhchoutai/p/6880274.html
Copyright © 2020-2023  润新知