During the study of the Martians Petya clearly understood that the Martians are absolutely lazy. They like to sleep and don't like to wake up.
Imagine a Martian who has exactly n eyes located in a row and numbered from the left to the right from 1 to n. When a Martian sleeps, he puts a patch on each eye (so that the Martian morning doesn't wake him up). The inner side of each patch has an uppercase Latin letter. So, when a Martian wakes up and opens all his eyes he sees a string s consisting of uppercase Latin letters. The string's length is n.
"Ding dong!" — the alarm goes off. A Martian has already woken up but he hasn't opened any of his eyes. He feels that today is going to be a hard day, so he wants to open his eyes and see something good. The Martian considers only m Martian words beautiful. Besides, it is hard for him to open all eyes at once so early in the morning. So he opens two non-overlapping segments of consecutive eyes. More formally, the Martian chooses four numbers a, b, c, d, (1 ≤ a ≤ b < c ≤ d ≤ n) and opens all eyes with numbers i such that a ≤ i ≤ b or c ≤ i ≤ d. After the Martian opens the eyes he needs, he reads all the visible characters from the left to the right and thus, he sees some word.
Let's consider all different words the Martian can see in the morning. Your task is to find out how many beautiful words are among them.
The first line contains a non-empty string s consisting of uppercase Latin letters. The strings' length is n (2 ≤ n ≤ 105). The second line contains an integer m (1 ≤ m ≤ 100) — the number of beautiful words. Next m lines contain the beautiful words pi, consisting of uppercase Latin letters. Their length is from 1 to 1000. All beautiful strings are pairwise different.
Print the single integer — the number of different beautiful strings the Martian can see this morning.
ABCBABA
2
BAAB
ABBA
1
Let's consider the sample test. There the Martian can get only the second beautiful string if he opens segments of eyes a = 1, b = 2 andc = 4, d = 5 or of he opens segments of eyes a = 1, b = 2 and c = 6, d = 7.
题意:给出一个长的字符串str 和m 个小的字符串,然后让你找两个部分 str[a~b],str[c~d] ,a < c ,使得两部分连接起来等于某个给出的字符串,
问一共可以组成多少个给出的字符串。
对于每个小的字符串,我们dp[j] 表示后缀j 和长的字符串匹配位置(最后面的)
这个可以用后缀自动机搞,因为要求最后面的,所以需要对建好的自动机经行拓扑排序。
调了N久,真累
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<cmath> #include<queue> #include<vector> #include<set> #include<stack> #include<map> #include<ctime> #include<bitset> #define LL long long #define ll long long #define INF 4365899 #define maxn 100052 #define eps 1e-6 #define mod 1000000007 using namespace std; struct SAM { SAM *pre,*son[26] ; int len ,g ,Max; }que[maxn*2],*root,*tail,*b[maxn*2]; int tot ; void add(int c ,int l) { SAM *p = tail,*np=&que[tot++] ; np->len=l;tail=np ; np->g=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; nq->g=l; np->pre=q->pre=nq; while(p&&p->son[c]==q) p->son[c]=nq,p=p->pre; } } } void init(int n ) { tot=0; for(int i = 0 ; i < n ;i++) { que[i].g = 0 ; que[i].pre=NULL; memset(que[i].son,0,sizeof(que[i].son)) ; } root=tail=&que[tot++] ; } struct SAM1 { SAM1 *pre,*son[26] ; int len ,g ,Max; }que1[maxn*2],*root1,*tail1,*b1[maxn*2]; int tot1 ; void add1(int c ,int l) { SAM1 *p = tail1,*np=&que1[tot1++] ; np->len=l;tail1=np ; np->g=l; while(p&&p->son[c]==NULL)p->son[c]=np,p=p->pre ; if(p==NULL) np->pre = root1 ; else { SAM1 *q = p->son[c] ; if(p->len+1==q->len)np->pre = q ; else { SAM1 *nq = &que1[tot1++] ; *nq=*q ; nq->len = p->len+1; nq->g=l; np->pre=q->pre=nq; while(p&&p->son[c]==q) p->son[c]=nq,p=p->pre; } } } void init1(int n ) { tot1=0; for(int i = 0 ; i < n ;i++) { que1[i].g = 0 ; que1[i].pre=NULL; memset(que1[i].son,0,sizeof(que1[i].son)) ; } root1=tail1=&que1[tot1++] ; } struct node { char a[1010] ; }qe[110]; char a[maxn] ; int dp[110][1010],C[maxn]; bool vi[maxn]; int main() { int i,n,m,j,k; int cnt ,ans,len ; while(scanf("%s",a+1) != EOF) { n = strlen(a+1) ; init1(n*2) ; for( i = 1 ; i <= n ;i++) add1(a[i]-'A',i) ; reverse(a+1,a+n+1) ; a[n+1]='