Message Flood(字典树)

Message Flood

Time Limit:2000MS  Memory Limit:65536K
Total Submit:38 Accepted:8

Description

Well, how do you feel about mobile phone? Your answer would probably be something like that "It's so convenient and benefits people a lot". However, If you ask Merlin this question on the New Year's Eve, he will definitely answer "What a trouble! I have to keep my fingers moving on the phone the whole night, because I have so many greeting message to send!" Yes, Merlin has such a long name list of his friends, and he would like to send a greeting message to each of them. What's worse, Merlin has another long name list of senders that have sent message to him, and he doesn't want to send another message to bother them Merlin is so polite that he always replies each message he receives immediately). So, before he begins to send message, he needs to figure to how many friends are left to be sent. Please write a program to help him.
Here is something that you should note. First, Merlin's friend list is not ordered, and each name is alphabetic strings and case insensitive. These names are guaranteed to be not duplicated. Second, some senders may send more than one message to Merlin, therefore the sender list may be duplicated. Third, Merlin is known by so many people, that's why some message senders are even not included in his friend list.

Input

There are multiple test cases. In each case, at the first line there are two numbers n and m (1<=n,m<=20000), which is the number of friends and the number of messages he has received. And then there are n lines of alphabetic strings(the length of each will be less than 10), indicating the names of Merlin's friends, one per line. After that there are m lines of alphabetic strings, which are the names of message senders.
The input is terminated by n=0.

Output

For each case, print one integer in one line which indicates the number of left friends he must send.

Sample Input

5 3
Inkfish
Henry
Carp
Max
Jericho
Carp
Max
Carp
0

 

Sample Output

3

 

Source

ZSCPC9pre



代码:
#include<iostream>
#include<string.h>
using namespace std;
int s,len,sum;             // len 字符串长度
char x[11];               //要记录或要查找的字符串
struct stu                  
{
 int k;                // 标记是否存在次字符串
 int a[26];             
}str[200000];
void ws()                 // 结点初始化函数
{
 int i;
 str[s].k=0;
 for(i=0;i<26;i++)
  str[s].a[i]=0;
}
void zds(int i,int j)             // 建立字符串函数
{
 if(j==len)              //当第j等于字符串长度时说明第j-1个字符为最后一个字符
 {
  str[i].k=1;          //标记k=1;
  return ;
 }
 if(x[j]>='A'&&x[j]<='Z')
  x[j]=x[j]+'a'-'A';
 int m=x[j]-'a';
 if(str[i].a[m]==0)       //此字符的下一个字符不存在当前的字典数中,新建一个结点
 {
  ws();
  str[i].a[m]=s;
  s++;
 }
 zds(str[i].a[m],j+1);          //建立下一个字符
}
void cz(int i,int j)                //查找字符串函数
{
 if(j==len)                  // 查找到最后一个字符时结束
 {
  if(str[i].k)             
  {
  str[i].k=0;                //如果此字符存在,k变为0(即在字典中删除此串)
  sum++;                //  如果此字符存在sum加一,
  }
  return ;
 }
 if(x[j]>='A'&&x[j]<='Z')
  x[j]=x[j]+'a'-'A';
 int m=x[j]-'a';
 if(str[i].a[m]==0)return ;             // 如果当前字符的下一个字符不存在,结束查找
 cz(str[i].a[m],j+1);
}
int main()
{
 int m,n,i;
 while(cin>>n)
 {
  if(n==0)break;
  cin>>m;
  s=0;
  ws();
  s=1;
  for(i=0;i<n;i++)
  {
   scanf("%s",x);
   len=strlen(x);
   zds(0,0);
  }
  sum=0;
  for(i=0;i<m;i++)
  {
   scanf("%s",x);
   len=strlen(x);
   cz(0,0);
  }
  n=n-sum;
  printf("%d\n",n);
 }
 return 0;
}

posted on 2009-10-18 13:17 陈烈 阅读(537) 评论(0)  编辑 收藏 引用


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


<2009年10月>
27282930123
45678910
11121314151617
18192021222324
25262728293031
1234567

导航

统计

常用链接

留言簿

随笔档案

搜索

最新评论

阅读排行榜

评论排行榜