SDUTOJ 1500 ——Message Flood 字典树或map
2014-11-17 15:09
281 查看
Message Flood
Time Limit: 1500MS Memory limit: 65536K
题目描述
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.
输入
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(thelength 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.
输出
For each case, print one integer in one line which indicates the number of left friends he must send.
示例输入
5 3 Inkfish Henry Carp Max Jericho Carp Max Carp 0
示例输出
3
来源
第9届中山大学程序设计竞赛预选赛Map写法:
#include <stdio.h>
#include <string>
#include <string.h>
#include <map>
using namespace std;
char str[20010][13];
int main()
{
int n,m;
char s[13];
while(scanf("%d",&n),n != 0)
{
int js = 0;
map <string,int> mp;
scanf("%d",&m);
for(int i = 0;i < n;i++)
{
scanf("%s",str[i]);
int len = strlen(str[i]);
for(int j = 0;j < len;j++)
{
if(str[i][j] >= 'A' && str[i][j] <= 'Z')
str[i][j] += 32;
}
mp[str[i]] = 1;
}
for(int i = 0;i < m;i++)
{
scanf("%s",s);
int len = strlen(s);
for(int j = 0;j < len;j++)
{
if(s[j] >= 'A' && s[j] <= 'Z')
s[j] += 32;
}
mp[s] = 0;
}
for(int i = 0;i < n;i++)
{
if(mp[str[i]] == 1)
js++;
}
printf("%d\n",js);
}
return 0;
}
字典树写法:
#include <stdio.h>
#include <string.h>
char str[20010][12];
struct node
{
int flag;
struct node *next[26];
};
void creat(char *s,node *&head)
{
node *p = head;
int len = strlen(s);
int zh;
for(int i = 0; i < len; i++)
{
if(s[i] >= 'A' && s[i] <= 'Z')
s[i] += 32;
zh = s[i] - 'a';
if(p->next[zh] == NULL)
{
node *q = new node;
for(int j = 0; j < 26; j++)
{
q->next[j] = NULL;
}
q->flag = 0;
p->next[zh] = q;
p = q;
}
else
p = p->next[zh];
}
p->flag = 1;
}
bool nfind(char *s,node *&head)
{
node *p = head;
int len = strlen(s);
int zh;
for(int i = 0; i< len ; i++)
{
if(s[i] >= 'A' && s[i] <= 'Z')
s[i] += 32;
zh = s[i] - 'a';
if(p->next[zh] == NULL)
{
return false;
}
else
p = p->next[zh];
}
if(p->flag == 1)
return true;
return false;
}
void realse(node *p)
{
for(int i = 0;i < 26;i++)
{
if(p->next[i])
realse(p->next[i]);
}
delete p;
}
int main()
{
int n,m;
char s[13];
while(scanf("%d",&n),n != 0)
{
node *head = new node;
for(int i = 0; i < 26; i++)
{
head->next[i] = NULL;
}
scanf("%d",&m);
for(int i = 0; i < n; i++)
{
scanf("%s",str[i]);
}
for(int i = 0; i < m; i++)
{
scanf("%s",s);
creat(s,head);
}
int js = 0;
for(int i = 0; i < n; i++)
{
if(nfind(str[i],head))
{
js++;
}
}
printf("%d\n",n - js);
realse(head);
}
return 0;
}
相关文章推荐
- oj1500(Message Flood)字典树
- SDUT 1500 Message Flood(字典树 && 模板)
- SDUT1500_Message Flood(字典树)
- 字典树sdutoj 1500模板
- SDUTOJ 3059 C#中类的继承性 ——字典树(哈希用法),map
- 【1500】Message Flood (SDUTOJ)
- 【1500】Message Flood (SDUTOJ)
- STL 之map解决 Message Flood(原字典树问题)
- STL --字典树-- Message Flood-----map
- [字典树、map] HDU 1800 - Flying to the Mars
- HDU_1251 统计难题 【字典树||map】
- HDU - 1251(map或者字典树)
- SDUTOJ 1500
- HDU 1800 Flying to the Mars 字符串哈希||字典树||map
- A - Message Flood(字典树)
- poj 2503 Babelfish(字典树或map或哈希或排序二分)
- SDUT 1500 Message Flood
- poj2503 map构建字典树
- SDUTOJ 3039 迷之好奇 静态字典树
- 统计难题(字典树或map容器)