隐藏页面特效

Hotaru's problem

1|0Hotaru's problem


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


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
 
//在manachar的基础上,枚举回文串的中心,再找第三部分。 #include<cstdio> #include<cstring> #include<iostream> using namespace std; int read(){ register int x=0;bool f=1; register char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=0;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return f?x:-x; } const int N=3e5+10; int n,ans,cas,l,T,s[N],S[N],p[N]; void manacher(){ int id=0,mx=-1; for(int i=1;i<l;i++){ if(id+mx>i) p[i]=min(p[id*2-i],id+mx-i); while(i-p[i]>=0&&i+p[i]<=l&&S[i-p[i]]==S[i+p[i]]) p[i]++; if(id+mx<i+p[i]) id=i,mx=p[i]; } } void init(){ l=0;memset(p,0,sizeof p); for(int i=0;i<n;i++) S[++l]=-1,S[++l]=s[i]; S[++l]=-1; } int main(){ for(T=read(),cas=1;ans=0,cas<=T;cas++){ n=read(); for(int i=0;i<n;i++) s[i]=read(); init();manacher(); for(int i=1;i<=n*2+1;i+=2){ for(int j=i+p[i]-1;j-i>ans;j-=2){ if(j-i+1<=p[j]){ ans=max(ans,j-i); break; } } } printf("Case #%d: %d\n",cas,ans/2*3); } return 0; }

 

 


__EOF__

本文作者shenben
本文链接https://www.cnblogs.com/shenben/p/6253249.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   神犇(shenben)  阅读(335)  评论(0编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
点击右上角即可分享
微信分享提示