1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
| #include<iostream> #include<cstdio> #include<algorithm> #include<cstring> #include<cmath> #include<queue> using namespace std; typedef long long ll; template <typename T> inline void read(T&x){ int w=0;x=0; char ch = getchar(); while(ch<'0'||ch>'9'){ if(ch=='-') w=1; ch = getchar(); } while(ch>='0'&&ch<='9'){ x = (x<<1)+(x<<3)+(ch^48); ch = getchar(); } if(w) x=-x; } template <typename T,typename...Args> inline void read(T&t,Args&...args){ read(t);read(args...); } inline int max(int a,int b){ return (a > b ? a : b); } #define mian main
const int N = 1e6+5; int n; char s[N]; struct TRIE{ int son[27],fail,num; #define son(x,y) Trie[x].son[y] #define fail(x) Trie[x].fail #define num(x) Trie[x].num
}Trie[N]; int dp[N],rt=0,cnt; inline void insert(char *s){ int now = rt; int len = strlen(s); for(int i=0;i<len;++i){ int c = s[i]-'a'; if(!son(now,c)) son(now,c) = ++cnt; now = son(now,c); } ++num(now); } queue <int> q; inline void get_fail(){ int res = 0; q.push(0); while(q.size()){ int now = q.front(); q.pop(); int tfail = fail(now); if(num(tfail)) num(now) = 1; for(int i=0;i<26;++i){ int tson = son(now,i); if(tson) q.push(tson); else son(now,i) = son(tfail,i); if(now) fail(tson) = son(tfail,i); dp[tson] = max(dp[now],dp[fail(tson)])+num(tson); res = max(res,dp[tson]); } } printf("%d\n",res); } inline void Init(){ memset(Trie,0,sizeof(TRIE)*(cnt+1)); memset(dp,0,sizeof(dp)); cnt = 0; } int main(){ while(1){ read(n); if(!n) break; Init(); while(n--){ scanf("%s",s); insert(s); } get_fail(); } return 0; }
|