SDUT1500_Message Flood(字典树)
2014-02-18 18:24
423 查看
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 tokeep 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(the length of each willbe 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届中山大学程序设计竞赛预选赛解题报告
这题在当初学字符串的时候做的,又是英文题,题意大体是一个人手上有一个名单,在新年那一天他需要给名单上的人发短信,然而有小伙伴先给他发短信了(可以发多次信息),他一收到短信就回复小伙伴,问题是除去给他发信息的小伙伴,他还需要给几个人发信息。
一开始就以为是模拟的题目,直接敲代码
#include<stdio.h> #include<string.h> #include<stdlib.h> int cmp(const void *p1,const void *p2)//对于char类型的排序 { //降序排序 //return strcmp((char *)p2,(char *)p1); //升序排序 return strcmp((char *)p1,(char *)p2); } char name[20020][100],send[20020][100]; int main() { int n,m; int i,j; while(scanf("%d",&n)!=EOF&&n) { scanf("%d%*c",&m); int c=0,l=0; for(i=0;i<n;i++) { scanf("%s",name[i]); } for(i=0;i<m;i++) { scanf("%s",send[i]); } qsort(send,m,sizeof(send[0]),cmp); j=0; for(i=0;i<m;i++) { if(strcmp(send[i],send[i+1])!=0) { strcpy(send[j++],send[i]); l++; } } for(i=0;i<l;i++) for(j=0;j<n;j++) { if(strcmp(name[j],send[i])==0) c++; } printf("%d\n",n-c); } } /************************************** Problem id : SDUT OJ 1500 User name : acm2013叶思俊 Result : Time Limit Exceeded Take Memory : 0K Take Time : 1510MS Submit Time : 2014-02-10 15:12:17 **************************************/这题给的时间是1500ms,两个名单都是20000- 的,双重循环都4亿次的节奏,还用到快排。。。超时。。。
还用了链表写了次。。。
#include<stdio.h> #include<stdlib.h> #include<string.h> struct node { char name[100]; node *next; }; int main() { int n,m; int i,j; while(scanf("%d",&n)!=EOF&&n) { int c=0; scanf("%d%*c",&m); node *head1=new node; head1->next=NULL; node *head2=new node; head2->next=NULL; node *tail1=head1,*tail2=head2; for(i=0;i<n;i++) { node *p=new node; scanf("%s",p->name); p->next=NULL; tail1->next=p; tail1=p; } for(i=0;i<m;i++) { node *p=new node; scanf("%s",p->name); p->next=NULL; tail2->next=p; tail2=p; } node *q=head2->next; while(q!=NULL) { node *p=head1; while(p->next!=NULL) { if(strcmp(q->name,p->next->name)==0) { p->next=p->next->next; c++; } else { p=p->next; } } q=q->next; } printf("%d\n",n-c); } }
同样超时。。。今天学了字典树,是个好东西。。。
要注意的是以哪个名单建字典树。。。
以名单一的建树,你对名单二查找时还要注意重复名字的。。。
所以以名单二建字典树。。。
#include <stdio.h> #include <string.h> #include <algorithm> struct node { int v; node *next[26]; }; node *newnode() { node *p=new node; p->v=0; for(int i=0;i<26;i++) { p->next[i]=NULL; } return p; } void insertnode(node *root,char *str) { node *p=root; int l=strlen(str); for(int i=0;i<l;i++) { int t; if(str[i]>='a') t=str[i]-'a'; else t=str[i]-'A'; if(p->next[t]==NULL) { p->next[t]=newnode(); } p=p->next[t]; } p->v=1; } int find(node *root,char *str) { node *p=root; int l=strlen(str); for(int i=0;i<l;i++) { int t; if(str[i]>='a') t=str[i]-'a'; else t=str[i]-'A'; if(p->next[t]==NULL) return 0; else p=p->next[t]; } if(p->v==1) return 1; return 0; } int main() { int n,m; int i,j,k; char name[30000][15],send[100000]; while(~scanf("%d",&n)&&n) { int c=0; node *root=newnode(); scanf("%d%*c",&m); for(i=0;i<n;i++) { scanf("%s",name[i]); } for(i=0;i<m;i++) { scanf("%s",send); insertnode(root,send); } for(i=0;i<n;i++) { if(!find(root,name[i])) c++; } printf("%d\n",c); } }
相关文章推荐
- 软件设计是怎样炼成的(5)——规划系统的骨架(架构设计)(下篇)
- 软件设计是怎样炼成的(5)——规划系统的骨架(架构设计)(下篇)
- GC_EXTERNAL_ALLOC freed 与 GC_EXPLICIT freed 是什么?
- 信封问题(转载)
- matcom使用方法
- 给定整数数组,相邻两个之间之差为1或者-1,求给定数在数组中的位置
- 线程安全类的设计
- Cocos2d-x学习之---关于CCTableView中reloadData()操作,删除第一个/最后一个cell后tableview位置未刷新的问题
- 人在事业中
- Drupal中的分类(Taxonomy)用法 (转)
- Cocos2d-x学习之---关于CCTableView中reloadData()操作,删除第一个/最后一个cell后tableview位置未刷新的问题
- [个人开发者如何赚钱二]从自己最熟悉的方面入手,获取小利
- strcpy strcmp memcpy源码
- 黑马程序员-JAVA高级(网络编程)PART1
- Cocos2dx版本与CocosBuilder版本匹配问题
- Linux下安装Oracle11gR2 INS-20802 oracle net configuration assistant failed
- 数字地,模拟地,电源地
- ecshop之transport和jquery冲突之完美解决方案
- 在iOS 7中使用storyboard(part 1)
- 直接插入排序