409 Excuses, Excuses!

来源:互联网 发布:python输入ctrl c 编辑:程序博客网 时间:2024/06/03 23:07
Excuses,Excuses! 

Judge Ito is having a problem with people subpoenaed for juryduty giving rather lame excuses in order to avoid serving. In orderto reduce the amount of time required listening to goofy excuses,Judge Ito has asked that you write a program that will search for alist 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 firstnumber ( tex2html_wrap_inline30 ) defines the number of keywords to be used in thesearch. The second number ( tex2html_wrap_inline32 ) defines the number of excuses in the set to besearched.
  • Lines 2 through K+1 each contain exactly onekeyword.
  • Lines K+2 through K+1+E each containexactly one excuse.
  • All keywords in the keyword list will contain only contiguouslower case alphabetic characters of length L ( tex2html_wrap_inline42 ) and will occupy columns 1 through L in theinput line.
  • All excuses can contain any upper or lower case alphanumericcharacter, a space, or any of the following punctuation marks[SPMamp".,!?&] not including thesquare 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) fromthe list.

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

For each set of input, you are to print a single line with thenumber of the set immediately after the string ``Excuse Set#". (See the Sample Output). The following line(s) is/are tocontain the worst excuse(s) one per line exactly as read in. Ifthere is more than one worst excuse, you may print them in anyorder.

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( 1<=k<=20 )表示查询中的关键词数目,第二个整数E( 1<=E<=20 )表示要查找的借口个数。从第二行到第k+1行,每行有一个关键字,从第k+2k+1+E行,每行有一个借口。关键字列表中的所有关键字都是长度为L(1<=L<=20  )连续的小写字母,也就是占据第一列到第L列。

输出:对于每组输入数据,输出列表中的最坏借口。最坏借口:包含最多数目的关键字。
 
注意:(1)所有的借口可以是任何的小写、大写字母,数字,空格,标点符号(不包括方括号[]square brackets),长度不会超过70.
2)借口至少包含一个非空字符。
3如果在一个借口中,一个关键字多次出现,那么每次出现都应计数
4A 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.
 
 
代码:
#include<stdio.h>
#include<string.h>
#include<ctype.h>
int main()
{
 int i,j,len,k,e,count[20],max,n;
 char key[20][30],exu[20][80],words[80],*c;
 n=1;
 while(scanf("%d%d",&k,&e)==2)
 {
  getchar();
  for(i=0;i<k;i++)
   gets(key[i]);
  for(i=0;i<e;i++)
   gets(exu[i]);
  memset(count,0,sizeof(count));
  max=0;
  for(i=0;i<e;i++)
  {
   len=strlen(exu[i]);
   memset(words,'\0',80);
   for(j=0;j<len;j++)
    if(isalpha(exu[i][j]))words[j]=tolower(exu[i][j]);
    else words[j]=exu[i][j];
   for(j=0;j<k;j++)
   {
    c=words;
    while((c=strstr(c,key[j]))!=NULL)
    {
     if( (*c==words[0] && ! isalpha(*(c+strlen(key[j]))) ) || (!isalpha(*(c-1)) && *c==words[len-1] ) ||
      (!isalpha(*(c-1)) && !isalpha(*(c+strlen(key[j]))) )  )
         count[i]++;
     c+=strlen(key[j]);
    }
   }
   if(count[i]>max)max=count[i];
  }
  printf("Excuse Set #%d\n",n++);
        for(i=0;i<e;i++)           
   if(count[i]==max)  
    printf("%s\n", exu[i]);          
  printf("\n");
 }
    return 0;
}
/*  三个函数的 运用 一是 判断 是否为 字母的函数 
二 是 把字母 转换 为小写的 函数
三是 字符串 查找 函数 strstr   由于题目的具体 要求 需要 修改一下 此函数 返回的指针 使其可以 继续 查找下去
 另外 需要 注意的是 由题意 得   每一个匹配的 单词 必须要是 完全 匹配 即 单词 前后 只能存在 非字母 字符 或者空格
 因此 需加一个 if 判断 条件 确定 count[i] 是否 自加