Message Flood

 1 #include<map>
 2 #include<stdio.h>
 3 #include<ctype.h>
 4 #include<string.h>
 5 #include <iostream>
 6 #include <algorithm>
 7 using namespace std;
 8 int main()
 9 {
10     char str[25];
11     int i,j,m,n,len;
12     while(scanf("%d",&n)&&n)
13     {
14         map<string,int>mapp;//放在while里面,每次重新申请,免去了清空map的步骤
15         scanf("%d",&m);
16         for(j=0; j<n; j++)
17         {
18             scanf("%s",str);
19             len=strlen(str);
20             for(i=0; i<len; i++)
21                 str[i]=tolower(str[i]);
22             mapp.insert(pair<string,int>(str,1));//插入
23         }
24         map<string,int>::iterator iter;//声明迭代器
25         while(m--)
26         {
27             scanf("%s",str);
28             len=strlen(str);
29             for(i=0; i<len; i++)
30                 str[i]=tolower(str[i]);
31             iter=mapp.find(str);//查找
32             if(iter!=mapp.end())//返回值在结束标志处,说明未找到
33             {
34                 mapp.erase(iter);//删除
35                 n--;
36             }
37         }
38         printf("%d\n",n);
39     }
40     return 0;
41 }
View Code

 

 

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(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.

输出

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

感觉这题太没有爱了,好多方法可以做
1.二维数组
2.结构体
3.还有什么字典树
4.链表也可以
5.还有map
之所以说他没有爱,是因为除了字典树、map以外,其他费费尽心机写的代码都会超时…………Orz; 终于用map交上了,表示自己不懂,摸索着写的,可以去网上搜一下,有很详尽的对于map的解释。

差点忘了,要全部转成大写或小写,题目要求不区分大小写的
上代码:

其他的方法自己写了……都超时,

字典树表示不会…………有能力再试试吧!!

 

 

大神果然有,我一个同学(郑兄)用了,二分查找加快排,成功了!!!!!!!!!

不说了,自己看吧……

 

 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<stdlib.h>
 4 void strUpr( char *str )
 5 {
 6     int i, len = strlen(str);
 7 
 8     for ( i = 0; i < len; i++ )
 9     {
10         if( str[i] <= 'z' && str[i] >= 'a' )
11             str[i] = str[i] - 'a' + 'A';
12     }
13 }
14 void sort(char a[][20],int l,int r)
15 {
16     char  x[20];
17     strcpy(x,a[l]);
18     int i=l,j=r;
19     if(l>=r)return ;
20     while(i<j)
21     {
22         while(i<j&&strcmp(a[j],x)>=0)j--;
23         strcpy(a[i],a[j]);
24         while(i<j&&strcmp(a[i],x)<=0)i++;
25         strcpy(a[j],a[i]);
26     }
27     strcpy(a[i],x);
28     sort(a,l,i-1);
29     sort(a,i+1,r);
30 }
31 int binsearch(char a[][20],int s,int t,char key[])
32 {
33     int low=s,high=t,mid;
34     if(s<=t)
35     {
36         mid=low+(high-low)/2;
37         if(strcmp(a[mid],key)==0)
38             return mid;
39         if(strcmp(a[mid],key)>0)
40             return binsearch(a,low,mid-1,key);
41         else
42             return binsearch(a,mid+1,high,key);
43     }
44     return -1;
45 }
46 int main()
47 {
48     int m;
49     while(scanf("%d",&m)&&m!=0)
50     {
51         char f[20001][20],g[20001][20];
52         if(m==0)break;
53         else
54         {
55             int sum=0;//sum1为了标记重复元素的个数
56             int n;
57             scanf("%d",&n);
58             int i;
59             for(i=0; i<=m-1; i++)
60             {
61                 scanf("%s",f[i]);
62                 strUpr(f[i]);
63             }
64             for(i=0; i<=n-1; i++)
65             {
66                 scanf("%s",g[i]);
67                 strUpr(g[i]);
68             }
69             sort(g,0,n-1);//快速排序
70             for(i=0; i<=m-2; i++)
71             {
72                 if(strcmp(f[i],f[i+1])!=0)//相邻元素不相同,去重操作
73                 {
74                     if(binsearch(g,0,n-1,f[i])!=-1)//找到
75                         sum=sum+1;
76                 }//else continue;//相邻元素相同的话,不操作,进入下一次循环
77             }
78             if(binsearch(g,0,n-1,f[i])!=-1)//没找到
79                 sum=sum+1;
80             printf("%d\n",m-sum);
81         }
82     }
83     return 0;
84 }
View Code

 

 

 

posted @ 2013-07-30 13:37  孔凡凯凯  阅读(214)  评论(0编辑  收藏  举报