SPOJ 1812 LCS2

Orz

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 #define N 250010
 4 #define M 100
 5 #define RG register 
 6 #define IL inline
 7 #define rep(a,b,c) for(register int (a)=(b);(a)<=(c);++(a))
 8 #define INF (1<<30)
 9 #define mst(a,b) memset((a),(b),sizeof((a)))
10 IL bool isitdigit(char c){    return c<='9'&&c>='0';    }
11 IL int read()
12 {
13     RG int s;RG char c;
14     while(!isitdigit(c=getchar()));
15     for(s=c-'0';isitdigit(c=getchar());s=(s<<1)+(s<<3)+c-'0');
16     return s;
17 }
18 IL int maxify(int & a,int b){    return a>b? a:a=b;    }
19 IL int minify(int & a,int b){    return a<b? a:a=b;    }
20 
21 
22 int ch[N][26],fa[N],len[N],mx[N],mn[N],order[N],tot,last;
23 IL void extend(char c)
24 {
25     c-='a';
26     RG int p=last,o=++tot;
27     for(len[o]=len[p]+1;~p&&!ch[p][c];p=fa[p]) ch[p][c]=o;
28     if(!~p) fa[o]=0;
29     else{
30         int q=ch[p][c];
31         if(len[q]==len[p]+1) fa[o]=q;
32         else{
33             int temp=++tot;memcpy(ch[temp],ch[q],sizeof(ch[q]));
34             fa[temp]=fa[q];fa[q]=fa[o]=temp;
35             for(len[temp]=len[p]+1;~p&&ch[p][c]==q;p=fa[p]) ch[p][c]=temp;
36         }
37     }
38     last=o;
39 }
40 IL void topsort()
41 {
42     static int arry[N];
43     for(int i=0;i<=tot;i++) arry[len[i]]++; 
44     for(int i=1;i<=len[last];i++) arry[i]+=arry[i-1]; 
45     for(int i=tot;i>=0;i--) order[arry[len[i]]--]=i;
46 }
47 
48 
49 int m,ans=0;
50 char s[M][N]
51 
52 
53 int main()
54 {
55     fa[0]=-1;
56     freopen("test.txt","r",stdin);
57     m=read(); 
58     rep(i,1,m) scanf("%s",s[i]);
59     rep(i,0,strlen(s[1])-1) extend(s[1][i]);
60     int now=0,l=0;
61     mst(mn,0x3f);
62     rep(i,2,m)
63     {
64         mst(mx,0);
65         rep(j,0,strlen(s[i])-1)
66         {
67             int c=s[i][j]-'a';
68             while(now&&!ch[now][c]) now=fa[now],maxify(mx[now],len[now]); 
69             if(ch[now][c]) maxify(mx[ch[now][c]],mx[now]+1),now=ch[now][c]; 
70         }
71         for(int i=tot;i>=0;i--) maxify(mx[fa[order[i]]],mx[order[i]]),minify(mx[order[i]],len[order[i]]);    
72         for(int i=tot;i>=0;i--) minify(mn[i],mx[i]);
73     }
74     for(int i=tot;i>=0;i--) if(mn[i]!=0x3f3f3f3f) maxify(ans,mn[i]);
75     printf("%d",ans);
76     return 0;
77 }

猜你喜欢

转载自www.cnblogs.com/MediocreKonjac/p/9256876.html