give you N (2-4000) string, find out their common substring
Analysis: Because the last time to say that the problem will not be the kind of violent practice, and then read some other knowledge, that is, the suffix tree, all the suffixes of a string are added to the dictionary tree, and then use other strings to match, so that the speed of the match is the same time, Ah, But for the first time I put all the prefixes of the former N-1 string into the dictionary tree, and then thought that if a node is accessed N-1 times, and the nth string can also access this node, then this must be their common substring, but it is too naïve to return directly to the MLE, a fine pondering, Think of the worst case is the 8000 (N) string, each string is not the same, and each string is a length of ten (Len), then the space complexity should be len* (len+1)/2*n*26 about the worst case in theory is 4.1 billion memory, not super to the ghost then another idea , What if we just add all the suffixes of the first string to the dictionary tree? Because the common substring is asked, the first string must also include the common substrings of all strings, so that the memory overhead drops to len* (len+1)/2*26 about 50w, which is relatively easy to accept.
here is the AC code:
==========================================================================================================
#include <stdio.h>#include<string.h>#include<algorithm>#include<stdlib.h>using namespacestd;Const intMAXN = -;Const intMAXM =1007;Const intOO = 1e9+7;structnode{intTimes ; Node*NEXT[MAXN];};intBuildtrie (Node *head,CharS[],intx) { intI, k, depth =0; Node*p =Head; for(i=0; T[n]; i++) {k= S[i]-'a'; if(P->next[k] = =NULL) { if(X! =1) Break; P->NEXT[K] =Newnode (); } P= p->Next[k]; if(P->times +1>=x) {///If this node is accessed by this string or the previous node has accessedP->times =x; Depth++; } Else Break; } returndepth;}voidCleartrie (Node *head) {///Destroy TreeNode *p =Head; for(intI=0; i<maxn; i++) { if(P->next[i]! =NULL) Cleartrie (P-Next[i]); } free (P);}intMain () {intI, J, N; while(SCANF ("%d", &N), N) {node*head =Newnode (); Chars[maxm]={0}, ans[maxm]={0}; for(i=1; i<n; i++) {scanf ("%s", s); for(j=0; S[J]! =' /'; J + +) Buildtrie (head, S+J, I); } scanf ("%s", s); intMax =0; for(j=0; S[J]! =' /'; J + +) { intLen = Buildtrie (head, s+J, N); CharP[MAXM] = {0}; strncpy (p, s+J, Len); if(Max < Len | | (Max==len && strcmp (ans, p) >0) ) strcpy (ans, p), Max=Len; } if(ans[0] ==0) printf ("IDENTITY lost\n"); Elseprintf ("%s\n", ans); Cleartrie (head); } return 0;}
Corporate identity-hdu 2328 (multiple strings for common substrings)