hdu5371_Hotaru's problem_Manacher
来源:互联网 发布:世界杯预选赛网络直播 编辑:程序博客网 时间:2024/06/03 05:39
Hotaru's problem
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 3479 Accepted Submission(s): 1144
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.
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 than109 , descripting a sequence.
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
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.
We guarantee that the sum of all answers is less than 800000.
Sample Input
1102 3 4 4 3 2 2 3 4 4
Sample Output
Case #1: 9
题意:
求这样的一个串的最大长度:第一部分和第二部分为对称串,第一部分和第三部分相同。
解:
这题的意思还是想让你求回文串来着,第一部分和第二部分为回文串,第二部分和第三部分为回文串。
一开始做的时候也同样犯了一个低级的错误,就是直接在模板里加了 p[i] <= p[i+ p[i] -1] 这一判断语句。一开始也没反应过来为什么错了,后来看了网上的一篇题解,博主说了一个样例就明白过来了:
1 2 4 4 5 5 4 4 5 5
正确的答案应该输出6,而按自己的代码算出来的却是1。
原因是在Manacher中的len数组存下的是0 1 2 1 2 1 2 3 2 1 3 7 2 1 2 7 2 1 2 1 2 1,找到第一个7的时候判断语句跳转的是之后的第6位,而不是下一个7的位置。所以才明白过来,就是第二部分的长度并不是和len数组中所记录的长度是相等的,所以代码会有问题。
所以重新思考一下题,其实无论怎么算这种串的长度,最重要的其实就是中间串 。那么只要判定在第一和第二部分组成的串的右子串内存在大于或者等于右子串长度的len[i]值就行了(就是存在第二和第三部分的回文串的左子串),因为同为回文串,有公共部分的话那么组成的就是符合题意中的串了。
代码:
#include <iostream>#include <cstdio>#include <cstdlib>#include <algorithm>#include <cstring>using namespace std;const int MANX=2e6+100;int str0[MANX],str[MANX<<1];int p[MANX<<1],len;void start(){ str[0]=-2,str[1]=-1; //len=strlen(str0); for(int i=0;i<len;i++) { str[i*2+2]=str0[i]; str[i*2+3]=-1; } str[len*2+2]=-6;}void solve(){ memset(p,0,sizeof(p)); int mx=0,id; for(int i=1;i<len*2+2;++i) { if(mx>i)p[i]=min(p[2*id-i],mx-i); else p[i]=1; for(;str[i-p[i]]==str[i+p[i]]&&i-p[i]>=0&&i+p[i]<len*2+2;p[i]++); if(p[i]+i>mx)mx=p[i]+i,id=i; }}void print()//查看p[i]数组的函数,可忽略{ for(int i=0;i<len*2+2;i++) printf("%d ",p[i]); cout<<endl;}int main(){ int t; scanf("%d",&t); int count=0; while(t--){ scanf("%d",&len); for(int i=0;i<len;i++) scanf("%d",&str0[i]); start(); solve(); int ans=1; for(int i=3;i<len*2+2;i+=2) for(int j=ans;j<=p[i];j+=2) if(p[i+j-1]>=j)ans=j; //print(); printf("Case #%d: %d\n",++count,ans/2*3); } return 0;}
1 0
- hdu5371_Hotaru's problem_Manacher
- s
- s
- s
- s
- s
- s
- s
- s
- s
- S
- S
- s
- S
- s
- s
- s
- s
- 无法安装64位office,因为已有32位版本的完美解决方法
- Redis ACL之redis_set类分析介绍
- MailBee.NET Objects发送电子邮件(SMTP)教程一:使用SMTP服务器发送消息
- 我的安卓开发之路 2017.04.27
- Angular2 AsyncPipe
- hdu5371_Hotaru's problem_Manacher
- android屏幕适配详解
- 清除浮动的几种方法
- 【Linux】Ubuntu 查看系统位数以及内核版本
- sql学习笔记3--编辑数据
- 【ios】如何按照自定义类的对象的某个属性进行排序
- 项目-高并发系统三大利器
- Unity Scene场景自定义坐标轴
- RHEL--练习题--脚本自动划分SWAP以及开机自动挂载