您的位置:首页 > 其它

POJ 2774 最长公共子串

2015-06-07 22:34 295 查看
一定好好学SAM。。。模板在此:

#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<queue>
#include<cstring>
#define PAU putchar(' ')
#define ENT putchar('\n')
using namespace std;
const int maxn=250000+10,sig=26;
struct SAM{
struct node{
int len;node*p,*tx[sig];
}sam[maxn<<1],*nodecnt,*root,*x;
void init(){nodecnt=sam;return;}
node*newnode(int len=0){node*t=nodecnt++;t->len=len;return t;}
void extend(int pos,char ch){
int c=ch-'a';node*p=x;x=newnode(pos+1);
for(;p&&!p->tx[c];p=p->p) p->tx[c]=x;
if(!p) x->p=root;
else{ node*q=p->tx[c];
if(q->len==p->len+1) x->p=q;
else{ node*r=newnode();
r[0]=q[0];r->len=p->len+1;q->p=x->p=r;
for(;p&&p->tx[c]==q;p=p->p) p->tx[c]=r;
}
} return;
}
void build(char*s){x=root=newnode();for(int i=0;s[i];i++)extend(i,s[i]);return;}
int query(char*t){
int len=strlen(t),ans=0;node*p=root;
for(int i=0,L=0;i<len;i++){
int c=t[i]-'a';
if(p->tx[c])L++,p=p->tx[c];
else{
for(;p&&!p->tx[c];p=p->p);
if(p) L=p->len+1,p=p->tx[c];
else p=root,L=0;
} ans=max(L,ans);
} return ans;
}
}sol;
inline int read(){
int x=0,sig=1;char ch=getchar();
while(!isdigit(ch)){if(ch=='-')sig=-1;ch=getchar();}
while(isdigit(ch))x=10*x+ch-'0',ch=getchar();
return x*=sig;
}
inline void write(int x){
if(x==0){putchar('0');return;}if(x<0)putchar('-'),x=-x;
int len=0,buf[15];while(x)buf[len++]=x%10,x/=10;
for(int i=len-1;i>=0;i--)putchar(buf[i]+'0');return;
}
char s[maxn],t[maxn];
void init(){
scanf("%s%s",s,t);
sol.init();sol.build(s);write(sol.query(t));
return;
}
void work(){
return;
}
void print(){
return;
}
int main(){init();work();print();return 0;}


注意一个copy的写法:r[0]=q[0];似乎就不用写一个copy函数了?(雾
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: