Reprint please indicate the source, thank you http://blog.csdn.net/ACM_cxlove? Viewmode = Contents
By --- cxlove
Question: Give a string of A, give several strings of B, and ask how many different substrings of A are suffixed with a string of B. Http://acm.buaa.edu.cn/problem/533/
Dshawn asked me out a Sam for the birthday of Nanae. This is a boring cross-course class question ~~~~~ For Sam, the solution is relatively simple. After both string a and string B are reversed, the suffix is the prefix. The question is changed to how many different substrings in string a are prefixed with a certain string B. After a Sam is created with string a, first pre-process the number of different successors for each substring in string. This is the DP on a simple Dag. The result is actually the number of different substrings prefixed with the current substring (=. Traverse each B string. If a contains this sub-string, count the number of successors of this Sub-string and overlay it. In this case, there is a duplicate substring. This is probably because if a node is counted, all subsequent nodes are included. One of the methods is to mark the reachable nodes after processing, and then process them in sequence through the Dag relationship, and undo all the subsequent tags.
Another solution is that the source of the problem is that some strings in string B are the prefix of a string. Sort B in Lexicographic Order and process it in sequence. If a B has passed through a marked node, it exits directly.
The practice of aekdycoin is Sa, which is terrible, but it is troublesome and time-consuming. Orz
#include<iostream>#include<cstdio>#include<cstring>#include<queue>#include<algorithm>#define LL long longusing namespace std;const int N=210005;struct Node{ char word[105]; bool operator<(const Node n)const{ return strcmp(word,n.word)<0; }}a[1005];struct SAM { SAM *pre,*son[26]; int f,len; LL cnt;}*root,*tail,que[N],*b[N];char str[N];int cnt[N];int tot=0;int c[N];int n;void add(int c,int l){ SAM *p=tail,*np=&que[tot++]; np->len=l; while(p&&p->son[c]==NULL) p->son[c]=np,p=p->pre; if(p==NULL) np->pre=root; else{ SAM *q=p->son[c]; if(p->len+1==q->len) np->pre=q; else{ SAM *nq=&que[tot++]; *nq=*q; nq->len=p->len+1; np->pre=q->pre=nq; while(p&&p->son[c]==q) p->son[c]=nq,p=p->pre; } } tail=np;}LL ans=0;int main(){// freopen("1.in","r",stdin);// freopen("output.txt","w",stdout); root=tail=&que[tot++]; scanf("%s",str); int l=strlen(str); for(int i=0;i<l/2;i++) swap(str[i],str[l-1-i]); for(int i=0;str[i];i++) add(str[i]-'a',i+1); for(int i=0;i<tot;i++) c[que[i].len]++; for(int i=1;i<tot;i++) c[i]+=c[i-1]; for(int i=0;i<tot;i++) b[--c[que[i].len]]=&que[i]; for(int i=0;i<tot;i++) que[i].cnt=1; for(int i=tot-1;i>=0;i--){ SAM *p=b[i]; for(int j=0;j<26;j++){ if(p->son[j]) p->cnt+=p->son[j]->cnt; } } scanf("%d",&n); for(int i=0;i<n;i++){ scanf("%s",a[i].word); int l=strlen(a[i].word); for(int j=0;j<l/2;j++) swap(a[i].word[j],a[i].word[l-1-j]); } sort(a,a+n); for(int j=0;j<n;j++){ SAM *p=root; bool flag=true; for(int i=0;a[j].word[i]&&flag;i++){ int s=a[j].word[i]-'a'; if(p->son[s]==NULL) flag=false; else{ if(p->son[s]->f) flag=false; p=p->son[s]; } } if(flag){ ans+=p->cnt; p->f=1; } } printf("%lld\n",ans); return 0;}