poj 1226 Substrings (后缀数组应用)
来源:互联网 发布:电脑电子书阅读软件 编辑:程序博客网 时间:2024/06/06 12:28
题意:
给出n个串,求一个最长的串,使得这个串或是这个串反转后的串出现在n个串中。
题解:
将每个串反转接到原串后面,用分隔符分开,然后就可以像求最长公共子串那样做了。
#include<iostream>#include<math.h>#include<stdio.h>#include<algorithm>#include<string.h>#include<vector>#include<queue>#include<map>#include<set>using namespace std;#define B(x) (1<<(x))typedef long long ll;const int oo=0x3f3f3f3f;const ll OO=1LL<<61;const int MOD=10007;const int maxn=41000;int rank[maxn],SA[maxn],height[maxn];int t1[maxn],t2[maxn],t3[maxn],t4[maxn];char str[maxn];int ss[maxn],in[maxn],n;set<int>g;void Swap(int*& a,int*& b){ int *temp=a; a=b; b=temp;}bool cmp(int t[],int a,int b,int l){ return t[a]==t[b]&&t[a+l]==t[b+l];}void build_SA(int s[],int len,int up){ int *k1=t1,*k2=t2,*r=t3,*cnt=t4; for(int i=0;i<up;i++)cnt[i]=0; for(int i=0;i<len;i++)cnt[k1[i]=s[i]]++; for(int i=1;i<up;i++)cnt[i]+=cnt[i-1]; for(int i=len-1;i>=0;i--)SA[--cnt[k1[i]]]=i; for(int d=1,p=1;p<len;d<<=1,up=p){ p=0; for(int i=len-d;i<len;i++)k2[p++]=i; for(int i=0;i<len;i++)if(SA[i]-d>=0)k2[p++]=SA[i]-d; for(int i=0;i<len;i++)r[i]=k1[k2[i]]; for(int i=0;i<up;i++)cnt[i]=0; for(int i=0;i<len;i++)cnt[r[i]]++; for(int i=1;i<up;i++)cnt[i]+=cnt[i-1]; for(int i=len-1;i>=0;i--)SA[--cnt[r[i]]]=k2[i]; Swap(k1,k2); k1[SA[0]]=0; p=1; for(int i=1;i<len;i++){ k1[SA[i]]= cmp(k2,SA[i-1],SA[i],d) ? p-1 : p++; } }}void get_height(int s[],int len){ for(int i=1;i<=len;i++)rank[SA[i]]=i; for(int i=0,p=0;i<len;i++){ int j=SA[rank[i]-1]; while(s[i+p]==s[j+p])p++; height[rank[i]]=p; if(p)p--; }}bool ok(int tag,int len){ int i=0; while(1){ while(i<=len&&height[i]<tag)i++; if(i>len)break; g.clear(); g.insert(in[SA[i-1]]); while(i<=len&&height[i]>=tag){ g.insert(in[SA[i]]); i++; } if(g.size()==n)return true; } return false;}int solve(int len){ g.clear(); int l=0,r=len,mid,ans=0; while(l<=r){ mid=(l+r)>>1; if(ok(mid,len)){ ans=mid; l=mid+1; }else{ r=mid-1; } } return ans;}int main(){ int len,T,tt; scanf("%d",&T); while(T--){ scanf("%d",&n); len=0; for(int i=1;i<=n;i++){ scanf("%s",str); tt=strlen(str); for(int j=0;j<tt;j++){ in[len]=i; ss[len++]=str[j]; } in[len]=i; ss[len++]=i*2-1+130; for(int j=tt-1;j>=0;j--){ in[len]=i; ss[len++]=str[j]; } in[len]=i; ss[len++]=i*2+130; } len--; ss[len]=0; build_SA(ss,len+1,400); get_height(ss,len); printf("%d\n",solve(len)); } return 0;}/**12AAAABBBB*/
0 0
- poj 1226 Substrings (后缀数组应用)
- POJ 1226 Substrings(后缀数组)
- POJ 1226 Substrings (后缀数组)
- 【POJ 1226】Substrings【后缀数组】
- POJ 1226 Substrings 后缀数组
- POJ 1226 Substrings (后缀数组)
- poj 3415 Common Substrings (后缀数组应用)
- POJ-1226(后缀数组)(Substrings)
- POJ 1226 Substrings(后缀数组+二分)
- poj 1226 Substrings (后缀数组)
- POJ-1226 Substrings(后缀数组)
- |poj 1226|后缀数组|二分|Substrings
- poj 1226 Substrings (二分+后缀数组)
- POJ 1226 Substrings KMP暴力 或 后缀数组
- poj 3415 Common Substrings 后缀数组
- poj Common Substrings(后缀数组&单调队列)
- 【后缀数组】 POJ 3415 Common Substrings
- [POJ 3415]Common Substrings(后缀数组)
- __asm__ __volatile__内嵌汇编用法简述
- 错误:The method replace(int, Fragment) in the type FragmentTransaction is not applicable for the argum
- Java中的private、protected、public和default的区别
- 20 Books Everyone Should Read Before Age 40
- nodejs之ejs标签系统
- poj 1226 Substrings (后缀数组应用)
- c#导出Excel及操作
- 反汇编及函数调用堆栈分析
- WCDMA信令流程
- poj 2985
- Installed Pop Corn time on Xiaomi
- 单元测试——Mock
- 重构笔记3-form template method
- itop exynos4412 lcd驱动 详细分析 (四)