POJ 2774(后缀数组)
来源:互联网 发布:淘宝怎么卖话费 编辑:程序博客网 时间:2024/05/16 08:20
(http://acm.hust.edu.cn/vjudge/contest/view.action?cid=105595#problem/I)
题意:寻找字符串A,B的最长相同子串
解法:构造个后缀数组,随便搞一搞就好了。感觉用Hash+二分也该也能做,太懒了不想了。。。
#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#include <cmath>#include <vector>#include <queue>#include <stack>#include <set>#include <map>#include <queue>//#include <tr1/unordered_set>//#include <tr1/unordered_map>#include <bitset>//#pragma comment(linker, "/STACK:1024000000,1024000000")using namespace std;#define lson l, m, rt<<1#define rson m+1, r, rt<<1|1#define inf 1e9#define debug(a) cout << #a" = " << (a) << endl;#define debugarry(a, n) for (int i = 0; i < (n); i++) { cout << #a"[" << i << "] = " << (a)[i] << endl; }#define clr(x, y) memset(x, y, sizeof x)#define ll long long#define ull unsigned long long#define FOR(i,a,b) \ for(i=a;a<b?i<=b:i>=b;a<b?i++:i--)const int maxn = 100000*2*2+100;struct suffix_array{ char s[maxn]; int sa[maxn],t[maxn],t2[maxn],c[maxn]; int n; void build_sa(int m) { int i,*x=t,*y=t2; FOR(i,0,m-1) c[i]=0; FOR(i,0,n-1) c[x[i]=s[i]]++; FOR(i,1,m-1) c[i]+=c[i-1]; FOR(i,n-1,0) sa[--c[x[i]]]=i; for(int k=1;k<=n;k<<=1){ int p=0; FOR(i,n-k,n-1) y[p++]=i; FOR(i,0,n-1) if(sa[i]>=k) y[p++]=sa[i]-k; FOR(i,0,m-1) c[i]=0; FOR(i,0,n-1) c[x[y[i]]]++; FOR(i,0,m-1) c[i]+=c[i-1]; FOR(i,n-1,0) sa[--c[x[y[i]]]]=y[i]; swap(x,y); p=1; x[sa[0]]=0; FOR(i,1,n-1) x[sa[i]]=y[sa[i-1]]==y[sa[i]]&& y[sa[i-1]+k]==y[sa[i]+k]?p-1:p++; if(p>=n) break; m=p; } } int Rank[maxn],height[maxn]; void getHeight() { int k=0; for(int i=0;i<n;i++) Rank[sa[i]]=i; for(int i=0;i<n;i++){ if(k) k--; int j=sa[Rank[i]-1]; while(s[i+k]==s[j+k]) k++; height[Rank[i]]=k; } }}su;char s[maxn],s2[maxn];int main(){ //freopen("input.txt","r",stdin); while(~scanf("%s%s",s,s2)) { int len=strlen(s); strcat(s,"&"); strcat(s,s2); strcpy(su.s,s); su.n=strlen(s)+1; su.build_sa(255); su.getHeight(); int ans=0,last1=0,last2=0; for(int i=1;i<=su.n;i++) { if( su.sa[i] <= len ) { last1 = inf; last2 = min(last2,su.height[i]); ans = max( ans , last2); } else { last2 = inf; last1 = min(last1,su.height[i]); ans = max( ans , last1); } } printf("%d\n",ans); } return 0;}
0 0
- POJ 2774(后缀数组)
- poj-2774(后缀数组)
- POJ 2774 后缀数组
- poj 2774 //后缀数组
- poj 2774 后缀数组
- poj 2774(后缀数组)
- poj 2774 后缀数组
- poj 2774 后缀数组
- POJ-2774-后缀数组
- POJ 2774 后缀数组
- poj 2774 后缀数组模板
- 【poj 2774】 后缀数组 height【】
- POJ 1734(后缀数组)
- POJ 3693(后缀数组)
- POJ 3415(后缀数组)
- POJ 2774 Long Long Message (后缀数组模板)
- poj(2774)后缀数组法求公共连续…
- poj 2774 Long Long Message(后缀数组)
- 阿里云配置日记
- Spark培训之计算员工薪资
- android设备设置改变时的数据保留和恢复
- 程序员的故事 001 小眼镜入职
- 运算符重载
- POJ 2774(后缀数组)
- HDU 5630 Rikka with Chess
- 2016.2.20 面试归来
- Android_Fragment
- android基础学习综合实例——天气预报App(基本功能实现)
- fishhook
- BASE64加解密实现方法
- Android学习心得第一课
- 背包的第k优解问题