MGLAR10 - Growing Strings
Gene and Gina have a particular kind of farm. Instead of growing animals and vegetables, as it is usually the case in regular farms, they grow strings. A string is a sequence of characters.
Strings have the particularity that, as they grow, they add characters to the left and/or to the right of themselves, but they never lose characters, nor insert new characters in the middle.Gene and Gina have a collection of photos of some strings at di?erent times during their growth.
The problem is that the collection is not annotated, so they forgot to which string each photo belongs to. They want to put together a wall to illustrate strings growing procedures, but they
need your help to ?nd an appropriate sequence of photos.
Each photo illustrates a string. The sequence of photos must be such that if si comes immediately before si+1 in the sequence, then si+1 is a string that may have grown from si (i.e., si appears as a consecutive substring of si+1). Also, they do not want to use repeated pictures,so all strings in the sequence must be di?erent.
Given a set of strings representing all available photos, your job is to calculate the size of the largest sequence they can produce following the guidelines above.
Gene and Gina have a particular kind of farm. Instead of growing animals and vegetables, as it is usually the case in regular farms, they grow strings. A string is a sequence of characters. Strings have the particularity that, as they grow, they add characters to the left and/or to the right of themselves, but they never lose characters, nor insert new characters in the middle.
Gene and Gina have a collection of photos of some strings at di?erent times during their growth. The problem is that the collection is not annotated, so they forgot to which string each photo belongs to. They want to put together a wall to illustrate strings growing procedures, but they need your help to ?nd an appropriate sequence of photos.
Each photo illustrates a string. The sequence of photos must be such that if si comes immediately before si+1 in the sequence, then si+1 is a string that may have grown from si (i.e., si appears as a consecutive substring of si+1). Also, they do not want to use repeated pictures, so all strings in the sequence must be di?erent.
Given a set of strings representing all available photos, your job is to calculate the size of the largest sequence they can produce following the guidelines above.
Input
Each test case is given using several lines. The ?rst line contains an integer N representing the number of strings in the set (1 ≤ N ≤ 10^4). Each of the following N lines contains a di?erent non-empty string of at most 1000 lowercase letters of the English alphabet. Within each test case, the sum of the lengths of all strings is at most 10^6.
The last test case is followed by a line containing one zero.
Output
For each test case output a single line with a single integer representing the size of the largest sequence of photos that can be produced.
Sample
input6 plant ant cant decant deca an 2 supercalifragilisticexpialidocious rag 0 output4 2
#include<cstdio> #include<cstdlib> #include<queue> #include<cstring> using namespace std; const int sigma_size=26; const int N=1e4+5; char s[1005]; struct Trie { Trie* next[sigma_size]; Trie* fail; int v,cnt,sum; Trie() { memset(next,0,sizeof(next)); fail=0; cnt=sum=v=0; } }; struct AhoCorasickAutomata { Trie *root; AhoCorasickAutomata() { root=new Trie(); } inline int idx(char c) { return c-‘a‘; } void _insert(char *s,int v) { int len=strlen(s); Trie* p=root; for(int i=0;i<len;i++) { int c=idx(s[i]); if(p->next[c]==0) { Trie* q=new Trie(); p->next[c]=q; } p=p->next[c]; } p->v=v,p->cnt++; } void getfail() { queue<Trie*>Q; root->fail=0; Q.push(root); while(!Q.empty()) { Trie* u=Q.front();Q.pop(); for(int i=0;i<sigma_size;i++) if(u->next[i]) { Trie* p=u->fail; while(p&&!p->next[i])p=p->fail; u->next[i]->fail=p?p->next[i]:root; u->next[i]->sum=max(u->sum,u->next[i]->fail->sum)+u->next[i]->cnt; Q.push(u->next[i]); } } } }; int dfs(Trie* u,int ans) { ans=max(ans,u->sum); for(int i=0;i<sigma_size;i++) if(u->next[i]) ans=max(ans,dfs(u->next[i],ans)); return ans; } int main() { int n; while(scanf("%d",&n),n) { AhoCorasickAutomata ac; for(int i=1;i<=n;i++) { scanf("%s",s); ac._insert(s,i); } ac.getfail(); printf("%d\n",dfs(ac.root,-1)); } return 0; }