n个子串,一个母串,问有多少个子串在母串中出现,包括被反转的子串。
只要把母串正着反着各自匹配一遍就行。
同样用的hdu 2222的模版
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
struct node{
int num;
node *next[26],*fail;
node(){
num=0;
fail=NULL;
for(int i=0;i<26;i++)
next[i]=NULL;
}
}*q[6000010];
char ss[5555555],s[5555555],S[5555555];
void insert(char s[],node *root){
int len=strlen(s);
node *p=root;
for(int i=0;i<len;i++){
if(p->next[s[i]-'A']==NULL)
p->next[s[i]-'A']=new node();
p=p->next[s[i]-'A'];
}
p->num++;
}
void acmove(node *root){
int head=0,tail=1;
root->fail=NULL;
q[0]=root;
while(head!=tail){
node *now=q[head++];
node *p=NULL;
for(int i=0;i<26;i++){
if(now->next[i]!=NULL){
if(now==root) now->next[i]->fail=root;
else{
p=now->fail;
while(p!=NULL){
if(p->next[i]!=NULL){
now->next[i]->fail=p->next[i];
break;
}
p=p->fail;
}
if(p==NULL)
now->next[i]->fail=root;
}
q[tail++]=now->next[i];
}
}
}
}
int find(node *root){
int i=0,numb=0,len=strlen(ss);
node *p=root;
while(ss[i]){
while(p->next[ss[i]-'A']==NULL&&p!=root)
p=p->fail;
p=p->next[ss[i]-'A'];
p=(p==NULL)?root:p;
node *now=p;
while(now!=root&&now->num!=0){
numb+=now->num;
now->num=0;
now=now->fail;
}
i++;
}
return numb;
}
int main(){
int cas,n;
node *root;
scanf("%d",&cas);
while(cas--){
int N;
root=NULL;
root=new node();
scanf("%d",&N);
for(int i=0;i<N;i++){
scanf("%s",s);
insert(s,root);
}
acmove(root);
int j=0;char c;
getchar();
while(c=getchar(),c!='\n')
{
if(c=='[')
{
scanf("%d",&n);
c=getchar();
for(int i=0;i<n;i++)
ss[j++]=c;
getchar();
}
else ss[j++]=c;
}
ss[j]='\0';
int w=find(root);
reverse(ss,ss+strlen(ss));
w+=find(root);
printf("%d\n",w);
}
return 0;
}