要注意二点 。
这组数据
1
6
she
he
he
say
shr
her
yasherhs
出现重复的,也要算。所以这里答案为4; 这一组
1
6
she
he
he
say
shr
her
yasherhe
查询单词中he出现过,所以后面的he不能记录,所以答案为4;
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct trie
{
trie *next[];
int flag;
int ok;
};
trie *root;
void init()
{
root=(trie*)malloc(sizeof(trie));
for(int i=;i<;i++)
root->next[i]=NULL;
root->flag=;//标记是否有单词
root->ok=;//标记该单词是否已经被使用
}
void insert(char *str)
{
int i,j,len=strlen(str);
trie *p=root,*q;
for(i=;i<len;i++)
{
int id=str[i]-'a';
if(p->next[id]==NULL)
{
q=(trie*)malloc(sizeof(trie));
for(j=;j<;j++)
q->next[j]=NULL;
q->flag=;
q->ok=;
p->next[id]=q;
}
p=p->next[id];
if(i==len-)
p->flag++;
}
}
int query(char *str)
{
int i,j,len=strlen(str);
int ans=,flag; for(i=;i<len;i++)
{
trie *p=root;
flag=;
for(j=i;j<len;j++)
{
int id=str[j]-'a';
if(p->next[id]==NULL)
{
flag=;
break;
}
p=p->next[id];
if(p->flag>&&!p->ok)//flag标记是否有单词,ok标记该单词是否被使用
{
p->ok=;
ans+=p->flag;
}
}
if(!flag)
i+=j-i;
}
return ans;
}
void freetrie(trie *p)
{
for(int i=;i<;i++)
{
if(p->next[i]!=NULL)
freetrie(p->next[i]);
}
free(p);
}
char temp[];
int main()
{
int i,j,n,t;;
scanf("%d",&t);
while(t--)
{
init();
char str[]; scanf("%d",&n);
for(i=;i<n;i++)
{
scanf("%s",str);
insert(str);
}
scanf("%s",temp);
int ans=query(temp);
printf("%d\n",ans);
freetrie(root);//之前没释放内存超时了。。
}
}