SPOJ 220 Relevant Phrases of Annihilation(每个字符串至少出现两次且不重叠的最长子串)
来源:互联网 发布:apache mp4 mime类型 编辑:程序博客网 时间:2024/05/17 04:49
转载请注明出处,谢谢http://blog.csdn.net/acm_cxlove/article/details/7854526 by---cxlove
题目:给定n个字符串,求出每个字符串至少出现两次且不重叠的最长子串
http://www.spoj.pl/problems/PHRASES/
将n个字符串连接起来,中间用不同的特殊字符隔开。求出后缀数组
二分答案,通过height值将后缀分组,判断在每一组中,每个字符串是否至少出现两次
而且由于题目需要不重叠的子串,所以还要记录每个字符串位置的最大值和最小值
判断其差是否小于当前答案
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>#define N 100005#define LL long long#define maxn 200005#define inf 1<<30using namespace std;//以下为倍增算法求后缀数组 int wa[maxn],wb[maxn],wv[maxn],Ws[maxn]; int cmp(int *r,int a,int b,int l) {return r[a]==r[b]&&r[a+l]==r[b+l];} void da(const int *r,int *sa,int n,int m){ int i,j,p,*x=wa,*y=wb,*t; for(i=0;i<m;i++) Ws[i]=0; for(i=0;i<n;i++) Ws[x[i]=r[i]]++; for(i=1;i<m;i++) Ws[i]+=Ws[i-1]; for(i=n-1;i>=0;i--) sa[--Ws[x[i]]]=i; for(j=1,p=1;p<n;j*=2,m=p){ for(p=0,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<n;i++) wv[i]=x[y[i]]; for(i=0;i<m;i++) Ws[i]=0; for(i=0;i<n;i++) Ws[wv[i]]++; for(i=1;i<m;i++) Ws[i]+=Ws[i-1]; for(i=n-1;i>=0;i--) sa[--Ws[wv[i]]]=y[i]; for(t=x,x=y,y=t,p=1,x[sa[0]]=0,i=1;i<n;i++) x[sa[i]]=cmp(y,sa[i-1],sa[i],j)?p-1:p++; } return; } int sa[maxn],Rank[maxn],height[maxn]; //求height数组 void calheight(const int *r,int *sa,int n){ int i,j,k=0; for(i=1;i<=n;i++) Rank[sa[i]]=i; for(i=0;i<n;height[Rank[i++]]=k) for(k?k--:0,j=sa[Rank[i]-1];r[i+k]==r[j+k];k++); return; }char ch[maxn];int str[maxn];int l[105];int mx[15],mn[15];int in[maxn];int k;bool check(int mid,int n){for(int i=0;i<k;i++){mx[i]=0;mn[i]=inf;} for(int i=1;i<=n;i++){if(height[i]<mid){for(int i=0;i<k;i++){mx[i]=0;mn[i]=inf;}mx[in[sa[i]]]=sa[i];mn[in[sa[i]]]=sa[i];}else{mx[in[sa[i]]]=max(mx[in[sa[i]]],sa[i]);mn[in[sa[i]]]=min(mn[in[sa[i]]],sa[i]);mx[in[sa[i-1]]]=max(mx[in[sa[i-1]]],sa[i-1]);mn[in[sa[i-1]]]=min(mn[in[sa[i-1]]],sa[i-1]);int j;for(j=0;j<k;j++){if(mx[j]-mn[j]<mid)break;}if(j==k) return true;}}return false;}int main(){int cnt=0,t;scanf("%d",&t);while(t--){scanf("%d",&k);int n=0;for(int i=0;i<k;i++){scanf("%s",ch);l[i]=strlen(ch);for(int j=n;j<n+l[i];j++){str[j]=ch[j-n]-'a'+1;in[j]=i;}n+=l[i]+1;str[n-1]=27+i;}n--;str[n]=0;da(str,sa,n+1,27+k+5);calheight(str,sa,n);int low=0,high=10000,mid,ans=0;while(low<=high){mid=(low+high)/2;if(check(mid,n)){ans=mid;low=mid+1;}else high=mid-1;}printf("%d\n",ans);}return 0;}
- SPOJ 220 Relevant Phrases of Annihilation(每个字符串至少出现两次且不重叠的最长子串)
- spoj 220 Relevant Phrases of Annihilation(每个字符串至少出现两次且不重叠的最长子串)
- spoj 220. Relevant Phrases of Annihilation( 后缀数组 每个字符串至少出现两次且不重叠的最长子串)
- spoj 220 Relevant Phrases of Annihilation (后缀数组 每个串中都至少出现两次的不重叠最长子串)
- SPOJ PHRASES 每个字符串至少出现两次且不重叠的最长子串
- spoj 220 每个字符串至少出现两次且不重叠的最长子串 (后缀数组)
- SPOJ 220后缀数组:求每个字符串至少出现两次且不重叠的最长子串
- spoj220 每个字符串至少出现两次且不重叠的最长子串(后缀数组)
- 后缀数组(每个字符串至少出现两次且不重叠的最长子串)spoj220
- SPOJ 220. Relevant Phrases of Annihilation(后缀数组多次不重叠子串)
- spoj 220 Relevant Phrases of Annihilation(n个串的最长公共重复2次子串)
- spoj 220 PHRASES - Relevant Phrases of Annihilation(后缀数组)
- SPOJ-220-Relevant Phrases of Annihilation
- SPOJ 220 Relevant Phrases of Annihilation (后缀数组)
- SPOJ 220 Relevant Phrases of Annihilation(后缀数组+二分)
- Relevant Phrases of Annihilation SPOJ
- Relevant Phrases of Annihilation SPOJ
- spoj Relevant Phrases of Annihilation
- HTML学习笔记2
- ARM Cortex-M3 学习笔记(4-1)
- How to Perfectly Uninstall Windows Internet Explorer 8/9/10
- uva-216 - Getting in Line
- ARM Cortex-M3 学习笔记(4-2)
- SPOJ 220 Relevant Phrases of Annihilation(每个字符串至少出现两次且不重叠的最长子串)
- 7、怎样把一个链表掉个顺序
- C++ 字符、字符串与int型数据的转化
- 8、请编写能直接实现 int atoi(const char * pstr)函数功能的代码
- Objective-C中一种消息处理方法performSelector withObject
- [init.rc] android开机过程执行shell脚本
- Python_Syntax
- Objective-C语法快速参考
- Facebook Haystack图片存储架构