UVA409

 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

Input to your program will consist of multiple sets of data.

 

  • Line 1 of each set will contain exactly two integers. The first number ( tex2html_wrap_inline30 ) defines the number of keywords to be used in the search. The second number ( tex2html_wrap_inline32 ) defines the number of excuses in the set to be searched.
  • Lines 2 through K+1 each contain exactly one keyword.
  • Lines K+2 through K+1+E each contain exactly one excuse.
  • All keywords in the keyword list will contain only contiguous lower case alphabetic characters of length L ( tex2html_wrap_inline42 ) and will occupy columns 1 through L in the input line.
  • All excuses can contain any upper or lower case alphanumeric character, a space, or any of the following punctuation marks [SPMamp".,!?&] not including the square brackets and will not exceed 70 characters in length.
  • Excuses will contain at least 1 non-space character.

Output

For each input set, you are to print the worst excuse(s) from the list.

 

  • The worst excuse(s) is/are defined as the excuse(s) which contains the largest number of incidences of keywords.
  • If a keyword occurs more than once in an excuse, each occurrance is considered a separate incidence.
  • A keyword ``occurs" in an excuse if and only if it exists in the string in contiguous form and is delimited by the beginning or end of the line or any non-alphabetic character or a space.

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.

 

Sample Input

 

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?

 

Sample Output

 

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] != '';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] = '';                //这就是本题最大的【亮点】所在啊!!如果不是字母,空格或者符号,就    a[j] = ''——这样就实现了把一句话中的单词一个个拿出来!!
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
",l++);
58         for(i = 0;i < n;i++)
59         {
60             if(count[i] == max)          //就是如此
61             {
62                 puts(exc[i]);
63             }
64         }
65         printf("
");
66     }
67     return 0;
68 }
 
原文地址:https://www.cnblogs.com/youdiankun/p/3689180.html