poj 2774 求兩字串的最長公共子串 字尾陣列
http://poj.org/problem?id=2774
Language:
Long Long Message
Description
The little cat is majoring in physics in the capital of Byterland. A piece of sad news comes to him these days: his mother is getting ill. Being worried about spending so much on railway tickets (Byterland is such a big country, and he has to spend 16 shours
on train to his hometown), he decided only to send SMS with his mother.
The little cat lives in an unrich family, so he frequently comes to the mobile service center, to check how much money he has spent on SMS. Yesterday, the computer of service center was broken, and printed two very long messages. The brilliant little cat soon found out: 1. All characters in messages are lowercase Latin letters, without punctuations and spaces. 2. All SMS has been appended to each other – (i+1)-th SMS comes directly after the i-th one – that is why those two messages are quite long. 3. His own SMS has been appended together, but possibly a great many redundancy characters appear leftwards and rightwards due to the broken computer. E.g: if his SMS is “motheriloveyou”, either long message printed by that machine, would possibly be one of “hahamotheriloveyou”, “motheriloveyoureally”, “motheriloveyouornot”, “bbbmotheriloveyouaaa”, etc. 4. For these broken issues, the little cat has printed his original text twice (so there appears two very long messages). Even though the original text remains the same in two printed messages, the redundancy characters on both sides would be possibly different. You are given those two very long messages, and you have to output the length of the longest possible original text written by the little cat. Background: The SMS in Byterland mobile service are charging in dollars-per-byte. That is why the little cat is worrying about how long could the longest original text be. Why ask you to write a program? There are four resions: 1. The little cat is so busy these days with physics lessons; 2. The little cat wants to keep what he said to his mother seceret; 3. POJ is such a great Online Judge; 4. The little cat wants to earn some money from POJ, and try to persuade his mother to see the doctor :( 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 yeshowmuchiloveyoumydearmotherreallyicannotbelieveit yeaphowmuchiloveyoumydearmother Sample Output 27 Sour |
解題思路:
熟悉字尾陣列的人都知道要求兩個字串的最長公共子串就是求任意兩字尾的最長公共字首。因此我們可以利用字尾陣列的height陣列。先用個分隔符將兩個字串連線起來,再用字尾陣列求出height陣列的值,找出一個height值最大並且i與i-1的sa值分別在兩串字元中就好.....
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
using namespace std;
const int maxn=2111111;
/******************************************************************
** 字尾陣列 Suffix Array
** INIT:solver.call_fun(char* s);
** CALL: solver.lcp(int i,int j); //字尾i與字尾j的最長公共字首
** SP_USE: solver.LCS(char *s1,char* s2); //最長公共字串
******************************************************************/
struct SuffixArray
{
int r[maxn];
int sa[maxn],rank[maxn],height[maxn];
int t[maxn],t2[maxn],c[maxn],n;
int m;//模板長度
void init(char* s)
{
n=strlen(s);
for (int i=0; i<n; i++) r[i]=int(s[i]);
m=300;
}
int cmp(int *r,int a,int b,int l)
{
return r[a]==r[b]&&r[a+l]==r[b+l];
}
/**
字元要先轉化為正整數
待排序的字串放在r[]陣列中,從r[0]到r[n-1],長度為n,且最大值小於m。
所有的r[i]都大於0,r[n]無意義演算法中置0
函式結束後,結果放在sa[]陣列中(名次從1..n),從sa[1]到sa[n]。s[0]無意義
**/
void build_sa()
{
int i,k,p,*x=t,*y=t2;
r[n++]=0;
for (i=0; i<m; i++) c[i]=0;
for (i=0; i<n; i++) c[x[i]=r[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 (k=1,p=1; k<n; k*=2,m=p)
{
for (p=0,i=n-k; i<n; i++) y[p++]=i;
for (i=0; i<n; i++) if (sa[i]>=k) y[p++]=sa[i]-k;
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],k)?p-1:p++;
}
n--;
}
/**
height[2..n]:height[i]儲存的是lcp(sa[i],sa[i-1])
rank[0..n-1]:rank[i]儲存的是原串中suffix[i]的名次
**/
void getHeight()
{
int i,j,k=0;
for (i=1; i<=n; i++) rank[sa[i]]=i;
for (i=0; i<n; i++)
{
if (k) k--;
j=sa[rank[i]-1];
while (r[i+k]==r[j+k]) k++;
height[rank[i]]=k;
}
}
int d[maxn][20];
//元素從1編號到n
void RMQ_init(int A[],int n)
{
for (int i=1; i<=n; i++) d[i][0]=A[i];
for (int j=1; (1<<j)<=n; j++)
for (int i=1; i+j-1<=n; i++)
d[i][j]=min(d[i][j-1],d[i+(1<<(j-1))][j-1]);
}
int RMQ(int L,int R)
{
int k=0;
L=rank[L];
R=rank[R];
if(L>R) swap(L,R);
L++;
while ((1<<(k+1))<=R-L+1) k++;
return min(d[L][k],d[R-(1<<k)+1][k]);
}
void LCP_init()
{
RMQ_init(height,n);
}
int lcp(int i,int j)
{
if (rank[i]>rank[j]) swap(i,j);
return RMQ(rank[i]+1,rank[j]);
}
void call_fun(char* s)
{
init(s);//初始化字尾陣列
build_sa();//構造字尾陣列sa
getHeight();//計算height與rank
LCP_init();//初始化RMQ
}
int solve(int len)
{
int maxx=0,pos=0;
for(int i=2; i<n; i++)
{
if(height[i]>maxx)
{
if(0<=sa[i-1]&&sa[i-1]<len&&len<sa[i])
maxx=height[i];
if(0<=sa[i]&&sa[i]<len&&len<sa[i-1])
maxx=height[i];
}
}
return maxx;
}
} solver;
int main()
{
char str[maxn],str1[maxn],s[maxn];
while(~scanf("%s",str))
{
scanf("%s",str1);
int n=0,len=strlen(str);
for(int i=0; i<len; i++)
s[n++]=str[i];
s[n++]='$';
int len1=strlen(str1);
for(int i=0; i<len1; i++)
s[n++]=str1[i];
s[n]=0;
solver.call_fun(s);
int maxx=solver.solve(len);
printf("%d\n",maxx);
}
return 0;
}
/**
dkghaabcdefghijklmnopqrstuvwxy
kghsghasabcdefghijklmnopqrstuvwxyzg
**/
相關文章
- POJ 2774-Long Long Message(字尾陣列+高度陣列-最大公共子串長度)陣列
- POJ 3294 Life Forms(字尾陣列求k個串的最長子串)ORM陣列
- POJ 3693 Maximum repetition substring(字尾陣列求最長重複子串)陣列
- POJ 1743 Musical Theme (字尾陣列,求最長不重疊重複子串)陣列
- POJ 2217-Secretary(字尾陣列+高度陣列-最大公共子串長度)陣列
- poj3080-kmp+列舉子串 求最長公共子串KMP
- 兩個字串的最長公共子串字串
- SPOJ 687. Repeats(字尾陣列求最長重複子串)陣列
- POJ 3415-Common Substrings(字尾陣列+單調棧-公共子串的長度)陣列
- ural 1297 最長迴文子串 字尾陣列陣列
- POJ2774Long Long Message(字尾陣列模板)陣列
- 最長公共子串 二維陣列 Go實現陣列Go
- SPOJ 694 求一個字串有多少子串 字尾陣列字串陣列
- lCS(最長公共子串)
- POJ 3415 Common Substrings(字尾陣列求重複字串)陣列字串
- 字串篇(python)—兩個字串的最長公共子序列字串Python
- 【Kmp求既是字首又是字尾的子串】POJ - 2752 Seek the Name, Seek the FameKMP
- 求字串中不含重複字元的最長子串字串字元
- 線性dp:最長公共子串
- HDU 4622 Reincarnation( 任意區間子串的長度, 字尾陣列+RMQ)陣列MQ
- POJ 3581-Sequence(字尾陣列)陣列
- POJ 1743 Musical Theme(字尾陣列)陣列
- 最長公共子序列求方案數
- URAL 1297. Palindrome(字尾陣列求最大回文串)陣列
- poj 3415 Common Substrings(長度大於k的相同子串對數xian 字尾陣列+單調桟統計)陣列
- HDU 5769-Substring(字尾陣列-不相同的子串的個數)陣列
- 最長子串
- [演算法筆記]動態規劃之最長公共子串和最長公共子序列演算法筆記動態規劃
- 演算法練習:求字串的最長重複子串(Java實現)演算法字串Java
- LEECODE 5 求最長迴文子串
- SPOJ 220. Relevant Phrases of Annihilation(字尾陣列多次不重疊子串)陣列
- 【谷歌面試題】找出字串中只包含兩種字元的最長子串谷歌面試題字串字元
- 演算法-兩最長迴文子串演算法
- 978 最長湍流子陣列陣列
- 牛客網 Coincidence(最長公共子串LCS板題)IDE
- 最長公共子序列
- 演算法之字串——最長迴文子串演算法字串
- POJ1743 Musical Theme(字尾陣列 二分)陣列