UVA 409 (13.08.01)

来源:互联网 发布:淘宝店铺破损补寄 编辑:程序博客网 时间:2024/04/30 08:22

 Excuses, Excuses! 

Judge Ito is having a problem with people subpoenaed for jury dutygiving rather lame excuses in order to avoid serving. In order toreduce the amount of time required listening to goofy excuses, Judge Itohas asked that you write a program that will search for a list ofkeywords in a list of excuses identifying lame excuses. Keywords can bematched in an excuse regardless of case.

Input

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

  • Line 1 ofeach set will contain exactly two integers. The first number ( tex2html_wrap_inline30 )defines the number of keywords to be used in the search. The secondnumber ( tex2html_wrap_inline32 ) defines the number of excuses in the set to besearched.
  • Lines 2 through K+1 each contain exactly one keyword.
  • LinesK+2 through K+1+E each contain exactly one excuse.
  • All keywords in thekeyword list will contain only contiguous lower case alphabeticcharacters of lengthL ( tex2html_wrap_inline42 ) and will occupy columns 1 throughL in the input line.
  • All excuses can contain any upper or lower casealphanumeric character, a space, or any of the following punctuationmarks [SPMamp".,!?&] not including the square brackets and will not exceed 70characters 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 thelargest number of incidences of keywords.
  • If a keyword occurs more thanonce in an excuse, each occurrance is considered a separate incidence.
  • A keyword ``occurs" in an excuse if and only if it exists in the stringin contiguous form and is delimited by the beginning or end of the lineor any non-alphabetic character or a space.

For each set of input, you are to print a single line with the number ofthe set immediately after the string ``Excuse Set #". (See the SampleOutput). 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 worstexcuse, you may print them in any order.

After each set of output, you should print a blank line.

Sample Input

5 3dogatehomeworkcanarydiedMy 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 5superhighwaycrazythermonuclearbedroomwarbuildingI 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 #1Can you believe my dog died after eating my canary... AND MY HOMEWORK?Excuse Set #2I am having a superhighway built in my bedroom.There was a thermonuclear war!题意: 给出k个单词, e行句子对于每个句子, 看看里面有多少个单词是和前面给出的k个单词是一样的, 统计之最后再比较一次, 哪个句子是出现上述单词数最多的, 打印出来若有出现多个句子都是最多的, 按先后的顺序~做法:AC的代码里有注释, 应该能看清楚思路~另外我用了三重的for循环, 代码不够优美~将就着看, 以后再优化~AC代码:
#include<stdio.h>#include<string.h>#include<ctype.h>using namespace std;char word[25][100];char words[25][100];int ans[25];int main() {int k, e;int len, pos;int cas = 0;char ch, tmp[100];while(scanf("%d%d%c", &k, &e, &ch) != EOF) {for(int i = 0; i < k; i++)gets(word[i]);for(int i = 0; i < k; i++) {len = strlen(word[i]);for(int j = 0; j < len; j++)if(isupper(word[i][j]))word[i][j] += 32; //化为小写}for(int i = 0; i < e; i++)gets(words[i]);memset(ans, 0, sizeof(ans)); //每次都要清零, 后面用来检查哪句出现单词最多for(int i = 0; i < e; i++) { //针对每一句话进行搜索len = strlen(words[i]);pos = 0;for(int l = 0; l < len; l++) { //对当下这句话中的每个字符进行判断if(isupper(words[i][l]))tmp[pos++] = words[i][l] + 32;else if(islower(words[i][l]))tmp[pos++] = words[i][l];else { //这里是遇到不是字母的字符了, 说明当前这句找到一个完整单词tmp[pos] = '\0';for(int j = 0; j < k; j++) { //针对当前这句已搜到的单词, 与单词比, 一样的话, 单词数+1, 即ans数组++if(strcmp(tmp, word[j]) == 0)ans[i]++;}pos = 0;}}}printf("Excuse Set #%d\n", ++cas);int Max = 0;for(int i = 0; i < e; i++) {if(ans[i] > Max)Max = ans[i];}for(int i = 0; i < e; i++) {if(ans[i] == Max)puts(words[i]);}printf("\n"); //注意空行不漏~}return 0;}
原创粉丝点击