poj2774 Long Long Message(后缀数组or后缀自动机)

转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud

Long Long Message
Time Limit: 4000MS   Memory Limit: 131072K
Case Time Limit: 1000MS

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

Source

POJ Monthly--2006.03.26,Zeyuan Zhu,"Dedicate to my great beloved mother."

题意:

求两个字符串的最长公共子串。

分析:

将两个字符串中间用一个不会出现的'$'符号连接,然后求出lcp,最大的且相邻的两个后缀不属于同一个字符串的就是答案。

用的是DC3

 #include <iostream>
#include <sstream>
#include <ios>
#include <iomanip>
#include <functional>
#include <algorithm>
#include <vector>
#include <string>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <climits>
#include <cctype>
using namespace std;
#define XINF INT_MAX
#define INF 0x3FFFFFFF
#define MP(X,Y) make_pair(X,Y)
#define PB(X) push_back(X)
#define REP(X,N) for(int X=0;X<N;X++)
#define REP2(X,L,R) for(int X=L;X<=R;X++)
#define DEP(X,R,L) for(int X=R;X>=L;X--)
#define CLR(A,X) memset(A,X,sizeof(A))
#define IT iterator
typedef long long ll;
typedef pair<int,int> PII;
typedef vector<PII> VII;
typedef vector<int> VI;
#define MAXN 400010 #define F(x) ((x)/3+((x)%3==1?0:tb))
#define G(x) ((x)<tb?(x)*3+1:((x)-tb)*3+2)
int wa[MAXN*],wb[MAXN*],wv[MAXN*],ww[MAXN*]; int c0(int *r, int a, int b) {
return r[a]==r[b]&&r[a+]==r[b+]&&r[a+]==r[b+];
}
int c12(int k, int *r, int a, int b)
{
if(k==) return r[a]<r[b]||r[a]==r[b]&&c12(,r,a+,b+);
else return r[a]<r[b]||r[a]==r[b]&&wv[a+]<wv[b+];
}
void rsort(int *r, int *a, int *b, int n, int m) {
REP(i,n) wv[i]=r[a[i]];
REP(i,m) ww[i]=;
REP(i,n) ww[wv[i]]++;
REP(i,m-) ww[i+]+=ww[i];
DEP(i,n-,) b[--ww[wv[i]]]=a[i];
} void dc3(int *r, int *sa, int n, int m) {
int j,*rn=r+n,*san=sa+n,ta=,tb=(n+)/,tbc=,p;
r[n]=r[n+]=;
REP(i,n) if(i%!=) wa[tbc++]=i;
rsort(r+,wa,wb,tbc,m);
rsort(r+,wb,wa,tbc,m);
rsort(r,wa,wb,tbc,m);
for(p=,rn[F(wb[])]=,j=;j<tbc;j++)
rn[F(wb[j])]=c0(r,wb[j-],wb[j])?p-:p++;
if(p<tbc) dc3(rn,san,tbc,p);
else REP(i,tbc) san[rn[i]]=i;
REP(i,tbc) if(san[i]<tb) wb[ta++]=san[i]*;
if(n%==) wb[ta++]=n-;
rsort(r,wb,wa,ta,m);
REP(i,tbc) wv[wb[i]=G(san[i])]=i;
int i;
for(i=j=p=;i<ta&&j<tbc;p++)
sa[p]=c12(wb[j]%,r,wa[i],wb[j])?wa[i++]:wb[j++];
for(;i<ta;p++) sa[p]=wa[i++];
for(;j<tbc;p++) sa[p]=wb[j++];
} int ra[MAXN*], height[MAXN*];
void calheight(int *r,int *sa,int n) {
int i,j,k=;
for(i=;i<=n;i++) ra[sa[i]]=i;
for(i=;i<n;height[ra[i++]]=k)
for(k?k--:,j=sa[ra[i]-];r[i+k]==r[j+k];k++);
}
int sa[MAXN *];
char str[MAXN];
char s[MAXN];
int a[MAXN];
int main()
{
ios::sync_with_stdio(false);
while(scanf("%s",str)!=EOF){
scanf("%s",s);
int len2=strlen(s);
int len1=strlen(str);
for(int i=;i<len2;i++){
str[i+len1]=s[i];
}
str[len1+len2]='\0';
int len=len1+len2;
for(int i=;i<len;i++){
a[i]=str[i]-'a'+;
}
a[len]=;
dc3(a,sa,len+,);
calheight(a,sa,len);
int ans=;
for(int i=;i<len;i++){
if(sa[i]<len1&&sa[i-]>=len1||(sa[i]>=len1&&sa[i-]<len1)){
ans=max(height[i],ans);
}
}
printf("%d\n",ans);
} return ;
}

代码君

利用后缀自动机的话,以一个串建一个自动机,然后另一个串直接塞进去跑就行了。相当裸

 #include <iostream>
#include <sstream>
#include <ios>
#include <iomanip>
#include <functional>
#include <algorithm>
#include <vector>
#include <string>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <climits>
#include <cctype>
using namespace std;
#define XINF INT_MAX
#define INF 0x3FFFFFFF
#define MP(X,Y) make_pair(X,Y)
#define PB(X) push_back(X)
#define REP(X,N) for(int X=0;X<N;X++)
#define REP2(X,L,R) for(int X=L;X<=R;X++)
#define DEP(X,R,L) for(int X=R;X>=L;X--)
#define CLR(A,X) memset(A,X,sizeof(A))
#define IT iterator
#define RIT reverse_iterator
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> PII;
typedef vector<PII> VII;
typedef vector<int> VI;
#define X first
#define Y second
#define lson(X) ((X)<<1)
#define rson(X) ((X)<<1|1) #define MAXN 100010 //#define SUFFIX_TREE struct SAM{
SAM* go[];
SAM* par;
int maxl;
#ifdef SUFFIX_TREE
int st_head;
#endif
SAM(int l=):maxl(l) {
#ifdef SUFFIX_TREE
st_head = ;
#endif
}
SAM& operator=(const SAM& s){
maxl = s.maxl;
par = s.par;
memcpy(go, s.go, sizeof(go));
return *this;
#ifdef SUFFIX_TREE
st_head = s.st_head;
#endif
}
int minl() {
return par?par->maxl+:maxl;
}
} node[MAXN<<], *last, *root;
int n_node; SAM* newnode() {
return &node[n_node++];
} void init_sam() {
n_node = ;
last = root = newnode();
} void extend(int c) {
SAM* p = last, *np = newnode();
np->maxl = p->maxl + ;
for(; p && !p->go[c]; p = p->par) p->go[c] = np;
if(!p) np->par = root;
else {
SAM* q = p->go[c];
if(q->maxl == p->maxl + ) np->par = q;
else {
SAM* nq = newnode();
*nq = *q;
nq->maxl = p->maxl + ;
np->par = q->par = nq;
for(;p && p->go[c] == q ;p = p->par) p->go[c] = nq;
}
}
last = np;
#ifdef SUFFIX_TREE
last->st_head = ;
#endif
} string str; #ifdef SUFFIX_TREE VI Map[MAXN<<]; void init_suffixtree(char* s) {
init_sam();
int l = strlen(s);
REP(i,l) extend(s[l-i-]);
REP(i,n_node) Map[i].clear();
REP(i,n_node) if(node[i].st_head) {
SAM* p = &node[i];
while(p!=root) {
string ss = str.substr(p->minl()-,p->maxl-p->minl()+);
reverse(ss.begin(),ss.end());
cout<<ss<<" -> ";
p=p->par;
}
cout<<"|"<<endl;
}
} #endif char s[MAXN]; int main()
{
while(~scanf("%s",s)) {
init_sam();
for(int i=;s[i];i++) extend(s[i]-'a');
scanf("%s",s);
int ans = ;
int l = ;
SAM* now = root;
for(int i=;s[i];i++) {
s[i]-='a';
while(now!=root && now->go[s[i]]==NULL) {
now = now->par;
l = min(l, now->maxl);
}
l++;
if(now->go[s[i]]) now = now->go[s[i]];
else l=;
ans = max(ans, l);
}
printf("%d\n", ans);
}
return ;
}

代码君

上一篇:Git 经常使用的命令


下一篇:CDH离线数据导入solr:利用MapReduceIndexerTool将json文件批量导入到solr