zoukankan      html  css  js  c++  java
  • HDU5371 Hotaru's problem

    本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作。

    本文作者:ljh2000
    作者博客:http://www.cnblogs.com/ljh2000-jump/
    转载请注明出处,侵权必究,保留最终解释权!

     
    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
    解题报告:
      求形如ABA(B与A对称0)的串的最大长度。
      先用manacher预处理出以每个点为中心的最长回文子串长度,枚举第二个部分即B的起点,再枚举串的长度,发现可行则更新答案(代码中非常清楚了)。可以用已经得到的ans剪枝。
      辣鸡HDU评测速度飘忽不定,刷了几次才跑过。
     
     
    //It is made by ljh2000
    #include <iostream>
    #include <cstdlib>
    #include <cstring>
    #include <cstdio>
    #include <cmath>
    #include <algorithm>
    using namespace std;
    typedef long long LL;
    const int MAXN = 200011;
    int n,len,a[MAXN],p[MAXN],ans;
    inline int getint(){
        int w=0,q=0; char c=getchar(); while((c<'0'||c>'9') && c!='-') c=getchar();
        if(c=='-') q=1,c=getchar(); while (c>='0'&&c<='9') w=w*10+c-'0',c=getchar(); return q?-w:w;
    }
    
    inline void manacher(){
    	//memset(p,0,sizeof(p)); 
    	for(int i=1;i<=n;i++) p[i]=0; 
    	int maxR=0,id=0;
    	for(int i=1;i<=n;i++) {
    		if(i<maxR) p[i]=min(p[2*id-i],maxR-i);
    		else p[i]=1;
    		for(;i+p[i]<=n && a[i-p[i]]==a[i+p[i]];p[i]++) ;
    		if(i+p[i]>maxR) { maxR=i+p[i]; id=i; }
    	}
    }
    
    inline void work(){
    	int T=getint(); int Case=0;
    	while(T--) {
    		len=getint(); a[0]=-2; a[1]=-1; n=1; for(int i=1;i<=len;i++) { a[++n]=getint(); a[++n]=-1; }
    		manacher(); ans=1;
    		//+2的原因是对称中心只能取在区分符号上
    		for(int i=3;i<n/*!!!*/;i+=2) //枚举the second part
    			for(int j=ans;j<=p[i];j+=2) //枚举一个部分的长度,利用ans剪枝
    				if(p[i+j-1]>=j) //如果the third part的回文长度大于等于j,即j可以作为答案(part2和part3对称)
    					ans=j;
    		Case++; ans/=2; ans*=3;//只算了两个part的长度
    		printf("Case #%d: %d
    ",Case,ans);
    	}
    }
    
    int main()
    {
        work();
        return 0;
    }
    

      

  • 相关阅读:
    PLSQL远程访问Oracle数据库配置
    考勤系统之计算工作小时数
    C#压缩或解压(rar和zip文件)
    C#基础Queue(队列)的应用
    PetaPoco模糊查询
    牛腩购物14:商品相关表的设计 后台增加用户管理 Eval高级应用 商品类别无限分类,外键的建立,repeater嵌套repeater
    asp.net 4.0 新功能 路由
    sqlserver 2008 使用维护计划,备份数据库
    asp.net Linq和泛型,IEnumerable和IQueryable之间的区别,Lambda表达式,Linq to Sql停止开发转为 Entity Framework
    List 对象集合的操作和使用 List 集合对象 对象集合 自动属性 对象初始化 集合初始化器
  • 原文地址:https://www.cnblogs.com/ljh2000-jump/p/6266113.html
Copyright © 2011-2022 走看看