SPOJ
来源:互联网 发布:优斗士网络推广效果 编辑:程序博客网 时间:2024/04/30 19:01
You are the King of Byteland. Your agents have just intercepted a batch of encrypted enemy messages concerning the date of the planned attack on your island. You immedietaly send for the Bytelandian Cryptographer, but he is currently busy eating popcorn and claims that he may only decrypt the most important part of the text (since the rest would be a waste of his time). You decide to select the fragment of the text which the enemy has strongly emphasised, evidently regarding it as the most important. So, you are looking for a fragment of text which appears in all the messages disjointly at least twice. Since you are not overfond of the cryptographer, try to make this fragment as long as possible.
Input
The first line of input contains a single positive integer t<=10, the number of test cases. t test cases follow. Each test case begins with integer n (n<=10), the number of messages. The next n lines contain the messages, consisting only of between 2 and 10000 characters 'a'-'z', possibly with some additional trailing white space which should be ignored.
Output
For each test case output the length of longest string which appears disjointly at least twice in all of the messages.
Example
Input:14abbabbadabddkabababacabababaOutput:2
(in the example above, the longest substring which fulfills the requirements is 'ba')
时限开了9s 我tm30ms刚过。。。#include <stdio.h>#include <string.h>#include <math.h>#include <algorithm>using namespace std;const int N = 1000000+10;typedef long long LL;int sa[N];int t1[N],t2[N],c[N];int rank1[N],height[N];void build_sa(int s[],int n ,int m){ int i,j,p,*x=t1,*y=t2; for(int i=0; i<m; i++) c[i]=0; for(int i=0; i<n; i++) c[x[i]=s[i]]++; for(int i=0; i<m; i++) c[i]+=c[i-1]; for(int i=n-1; i>=0; i--) sa[--c[x[i]]]=i; for(j=1; j<=n; j<<=1) { p=0; for(i=n-j; i<n; i++) y[p++]=i; for(i=0; i<n; i++) if(sa[i]>=j) y[p++]=sa[i]-j; for(i=0; i<m; i++) c[i]=0; for(i=0; i<n; i++) c[x[y[i]]]++; for(i=0; i<m; i++) c[i]+=c[i-1]; for(i=n-1; i>=0; i--) sa[--c[x[y[i]]]]=y[i]; swap(x,y); p=1; x[sa[0]]=0; for(i=1; i<n; i++) x[sa[i]]=(y[sa[i-1]]==y[sa[i]])&&(y[sa[i-1]+j]==y[sa[i]+j])?p-1:p++; if(p>=n) break; m=p; }}int s[N];void getheight(int s[],int n){ int i, j, k=0; for(i=0; i<=n; i++) rank1[sa[i]]=i; for(i=0; i<n; i++) { if(k) k--; j=sa[rank1[i]-1]; while(s[i+k]==s[j+k]) k++; height[rank1[i]]=k; } return ;}char str[N];int id[N], pos[N], vis[20], low[20], up[20];int judge(int mid,int m,int n){ int cnt=0; memset(vis,0,sizeof(vis)); for(int i=1;i<=n;i++) { if(height[i]<mid) { memset(vis,0,sizeof(vis)); cnt=0; continue; } if(!vis[id[sa[i-1]]]) { vis[id[sa[i-1]]]=1; low[id[sa[i-1]]]=up[id[sa[i-1]]]=sa[i-1]; } if(!vis[id[sa[i]]]) { vis[id[sa[i]]]=1; low[id[sa[i]]]=up[id[sa[i]]]=sa[i]; } if(vis[id[sa[i-1]]]==1) { low[id[sa[i-1]]]=min(low[id[sa[i-1]]],sa[i-1]); up[id[sa[i-1]]]=max(up[id[sa[i-1]]],sa[i-1]); if(up[id[sa[i-1]]]-low[id[sa[i-1]]]>=mid) { vis[id[sa[i-1]]]=2; cnt++; } } if(vis[id[sa[i]]]==1) { low[id[sa[i]]]=min(low[id[sa[i]]],sa[i]); up[id[sa[i]]]=max(up[id[sa[i]]],sa[i]); if(up[id[sa[i]]]-low[id[sa[i]]]>=mid) { vis[id[sa[i]]]=2; cnt++; } } if(cnt==m) return 1; } return 0;}int main(){ int t; scanf("%d", &t); while(t--) { int n=0, m, x=2*'z', maxt=0; scanf("%d", &m); for(int i=0;i<m;i++) { scanf("%s",str); int len=strlen(str); maxt=max(maxt,len); for(int j=0;str[j];j++) { s[n]=str[j];id[n]=i;n++; } s[n]=++x;id[n]=-1;n++; } s[n]=0,id[n]=0; build_sa(s,n+1,1000); getheight(s,n); int l=1,r=maxt,ans=0; while(l<=r) { int mid=(l+r)/2; if(judge(mid,m,n)) l=mid+1,ans=mid; else r=mid-1; } printf("%d\n",ans); } return 0;}
- SPOJ
- SPOJ
- SPOJ
- SPOJ
- SPOJ
- SPOJ
- SPOJ
- SPOJ
- SPOJ
- SPOJ
- SPOJ
- SPOJ
- SPOJ
- SPOJ
- SPOJ
- SPOJ
- SPOJ
- SPOJ
- 个人收藏(持续更新)
- “小程序”前景未明,战火却越燃越旺
- centos6.8 修改防火墙 开放端口 屏蔽ip
- 奥维奥快讯:SAP+锤子科技-一位成功的创业者背后总有一个SAP
- Java设计模式_(创建型)_建造者模式
- SPOJ
- VC中的lambda
- LeetCode 523. Continuous Subarray Sum
- Tensorflow 运算设备的配置
- 随笔----致曾经的寻知图
- SplSubject 内容产述
- Easy UI使用记录
- 基础Activity文件
- web.xml里配置load-on-startup的意思