Excuses, Excuses! |
Judge Ito is having a problem with people subpoenaed for jury duty giving rather lame excuses in order to avoid serving. In order to reduce the amount of time required listening to goofy excuses, Judge Ito has asked that you write a program that will search for a list of keywords in a list of excuses identifying lame excuses. Keywords can be matched in an excuse regardless of case.
Input to your program will consist of multiple sets of data.
SPMamp
".,!?&] not including the square
brackets and will not exceed 70 characters in length.For each input set, you are to print the worst excuse(s) from the list.
For each set of input, you are to print a single line with the number of the set immediately after the string ``Excuse Set #". (See the Sample Output). The following line(s) is/are to contain the worst excuse(s) one per line exactly as read in. If there is more than one worst excuse, you may print them in any order.
After each set of output, you should print a blank line.
5 3
dog
ate
homework
canary
died
My dog ate my homework.
Can you believe my dog died after eating my canary... AND MY HOMEWORK?
This excuse is so good that it contain 0 keywords.
6 5
superhighway
crazy
thermonuclear
bedroom
war
building
I am having a superhighway built in my bedroom.
I am actually crazy.
1234567890.....,,,,,0987654321?????!!!!!!
There was a thermonuclear war!
I ate my dog, my canary, and my homework ... note outdated keywords?
Excuse Set #1 Can you believe my dog died after eating my canary... AND MY HOMEWORK? Excuse Set #2 I am having a superhighway built in my bedroom. There was a thermonuclear war!
题目大意:输入两个数m,n。m个关键字,n个借口。如果哪个借口中包含的关键字最多,就输出那个借口。(如果一样多,都输出)
1 #include<stdio.h> //题目很长,不认识的单词居多,读起来费劲,但收获挺大的。有时思路对了,但表达不出来,其实很简单的,对吧? 2 #include<string.h> 3 #include<ctype.h> 4 char key[25][25],exc[100][100]; 5 int count[100],m,n; 6 int f_count(char s[]) 7 { 8 int i,j = 0,count = 0; 9 char a[100]; 10 for(i = 0;s[i] != ‘\0‘;i++) 11 { 12 if(isalpha(s[i])) //学会用isalpha,判断字符s[i]是否为大写字母或小写字母 13 { 14 a[j] = tolower(s[i]); //如果是,都转换成小写字母(因为题目中说了All keywords in the keyword list will contain 15 j++; //only contiguous lower case alphabetic characters 所有关键词都是小写字母) 16 } 17 else 18 { 19 a[j] = ‘\0‘; //这就是本题最大的【亮点】所在啊!!如果不是字母,空格或者符号,就 a[j] = ‘\0‘——这样就实现了把一句话中的单词一个个拿出来!! 20 for(j = 0;j < m;j++) 21 { 22 if(strcmp(a,key[j]) == 0) //然后看是不是关键词 23 { 24 count++; 25 } 26 } 27 j = 0; //j = 0,接着再拿出下一个单词 28 } 29 } 30 return count; 31 } 32 int main() 33 { 34 int l = 1; 35 while(scanf("%d%d",&m,&n)!=EOF) 36 { 37 getchar(); //别忘了 38 int i,max = 0; 39 memset(count,0,sizeof(count)); //这个很容易忽略,一定要把count数组重置0,否则就如input最后提示的note outdated keywords(注意过时的关键词了) 40 for(i = 0;i < m;i++) 41 { 42 gets(key[i]); 43 } 44 for(i = 0;i < n;i++) 45 { 46 gets(exc[i]); 47 } 48 for(i = 0;i < n;i++) 49 { 50 count[i] = f_count(exc[i]); //思路十分清晰,有条理,count一定要用数组,才能多组都最大时输出多组 51 } 52 for(i = 0;i < n;i++) 53 { 54 if(max < count[i]) 55 max = count[i]; 56 } 57 printf("Excuse Set #%d\n",l++); 58 for(i = 0;i < n;i++) 59 { 60 if(count[i] == max) //就是如此 61 { 62 puts(exc[i]); 63 } 64 } 65 printf("\n"); 66 } 67 return 0; 68 }
UVA409 - Excuses, Excuses!,布布扣,bubuko.com
原文:http://www.cnblogs.com/youdiankun/p/3689180.html