Description
Input
Output
Sample Input
5 3 Inkfish Henry Carp Max Jericho Carp Max Carp 0
Sample Output
3
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <queue>
#include <cmath>
#include <stack>
#include <map>
using namespace std;
//建立一個結構體
struct node
{
int flag;//設置一個標記
struct node *next[26];
};
//建樹
struct node *creat()
{
struct node *p;
p=(struct node *)malloc(sizeof(struct node ));//申請內存
for(int i=0;i<26;i++)//循環遍歷26個字母
{
p->next[i]=NULL;//設爲空
}
p->flag=0;//表示沒有被標記
return p;
}
//插入操作
void Insert(struct node *p,char *s)
{
int i,ans,len;
len=strlen(s);
for(i=0;i<len;i++)//循環遍歷整個字符串
{
if(s[i]>='A' && s[i]<='Z')
ans=s[i]-'A';
else
ans=s[i]-'a';
if(p->next[ans]==NULL)
p->next[ans]=creat();//重新建樹
p=p->next[ans];
}
p->flag=1;//設置爲已經標記
}
//查找操作
int Find(struct node *p,char *s)
{
int i,ans,len;
len=strlen(s);//測出字符串長度
for(i=0;i<len;i++)//循環遍歷整個字符串
{
if(s[i]>='A' && s[i]<='Z')
ans=s[i]-'A';
else
ans=s[i]-'a';
if(p->next[ans]==NULL)
return 0;
p=p->next[ans];
}
if(p->flag==1)
{
p->flag=0;
return 1;
}
return 0;
}
//釋放內存
void Delete(struct node *p)
{
int i;
for(i=0;i<26;i++)
{
if(p->next[i]!=NULL)
Delete(p->next[i]);
}
free(p);//釋放
}
int main()
{
int n,m,i,num;
char s[20000][100],s1[100];
struct node *tree;
while(scanf("%d %d",&n,&m),n)//輸入操作
{
tree=creat();
for(i=0;i<n;i++)
{
scanf("%s",s[i]);
}
num=0;
while(m--)
{
scanf("%s",s1);
Insert(tree,s1);
}
for(i=0;i<n;i++)
{
num+=Find(tree,s[i]);
}
printf("%d\n",n-num);
Delete(tree);
}
return 0;
}