POJ-2774-Long Long Message(后缀数组)
2016-04-05 15:40
393 查看
Input
Two strings with lowercase letters on two of the input lines individually. Number of characters in each one will never exceed 100000.
Output
A single line with a single integer number – what is the maximum length of the original text written by the little cat.
Sample Input
Sample Output
题意 :求最长连续公共子串
解法 :两串连接,求其后缀数组 后求height数组, 连接节点取特殊值标记
code:
Two strings with lowercase letters on two of the input lines individually. Number of characters in each one will never exceed 100000.
Output
A single line with a single integer number – what is the maximum length of the original text written by the little cat.
Sample Input
yeshowmuchiloveyoumydearmotherreallyicannotbelieveit yeaphowmuchiloveyoumydearmother
Sample Output
27
题意 :求最长连续公共子串
解法 :两串连接,求其后缀数组 后求height数组, 连接节点取特殊值标记
code:
#include <iostream> #include <stdio.h> #include <string.h> #include <stack> #include <queue> #include <map> #include <set> #include <vector> #include <math.h> #include <bitset> #include <algorithm> #include <climits> using namespace std; #define MAXN 200010 int t1[MAXN],t2[MAXN],c[MAXN]; int sa[MAXN],rank[MAXN],height[MAXN]; int r[MAXN]; char s1[MAXN],s2[MAXN]; int s[MAXN]; bool cmp(int *r, int a, int b,int l) { return r[a]==r[b]&&r[a+l]==r[b+l]; } void DA(int str[], int sa[], int rank[], int height[], int n, int m) { // n++; int i,j,p,*x=t1,*y=t2; for(i=0; i<m; ++i)c[i]=0; for(i=0; i<n; ++i)c[x[i]=str[i]]++; for(i=1; i<m; ++i)c[i]+=c[i-1]; for(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=1; 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]]=cmp(y,sa[i-1],sa[i],j)?p-1:p++; //if(p>=n)break; m=p; } // for(i=0; i<n; ++i) // cout<<sa[i]<<" "; //cout<<endl; // cin>>i; // cout<<"this\n"; int k=0; n--; for(i=0; i<=n; ++i)rank[sa[i]]=i; for(i=0; i<n; ++i) { if(k)--k; j=sa[rank[i]-1]; while(str[i+k]==str[j+k])k++; height[rank[i]]=k; } } /* int main() { int n; cin>>n; for(int i=0; i<n; ++i) cin>>s[i]; s =0; DA(s,sa,rank,height,n+1,30); return 0; }*/ int main() { while(~scanf("%s",s1)) { scanf("%s",s2); int i,j,k,len1,len2,len; len1=strlen(s1); len2=strlen(s2); for(i=0; i<len1; ++i) s[i]=s1[i]-'a'+1; s[i++]=27; for(j=0; j<len2; ++j) s[i++]=s2[j]-'a'+1; s[i++]=0; len=i; DA(s,sa,rank,height,len,30); int maxx=0; for(i=2; i<len; ++i) { if(height[i]>maxx) { if(sa[i]<len1 && sa[i-1]>len1)maxx=height[i]; if(sa[i-1]<len1 && sa[i]>len1)maxx=height[i]; } } cout<<maxx<<endl; } return 0; }
相关文章推荐
- 深入理解java多态性
- Problem 1689 分数修改
- Posters
- jquery中eq和get的区别与使用方法
- webrtc第二篇 聊天室
- 截取字符(substr)检索字符位置(instr)
- ssh整合中出现的Caused by: org.springframework.beans.NotWritablePropertyException:
- CSS元素的width和水平margin的设置
- LeetCode 61 - Rotate List
- wifidog+authpuppy认证页面的配置
- HBase常用操作之namespace
- SQLServer-镜像配置
- opencv第四章
- jbpm4.3表结构和表字段说明
- lua5.3与C++交互学习(二)
- ASP.NET中如何实现回调
- Impala之加载HBase数据
- 在Eclipse中使用JUnit4进行单元测试(中级篇)
- 使用Moq修改默认的ASP.NET MVC项目单元测试
- C#委托基础:谈委托和接口