UVA 409 Excuses, Excuses!

 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 (  ) defines the number of keywords to be used in the search. The second number (  ) 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 (  ) 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!



题目大意:输入有两东西:keyword还有 excuses;   把keyword出现在excuses频率最高的excuses输出,    如果频率最高的有多个,那么全部输出(顺序不定).

思路:今天做的第二道字符串题目.(⊙o⊙)…这里有几个trick:

(1)如果某个keyword出现多次在excuses中,那么是计算多次而不是只计算一次

(2)excuses在匹配单词和keyword的时候是不论字母大小写的,这个的英文意思不是很确定在具体的哪一句(囧囧):有可能是这一句:

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.

(3)输出的时候是输入原来的excuses,而不是变成小写的excuses

我是直接暴力求解,把excuses的单词拆下来,然后一个个和keyword进行比较.翻博客的时候看到一位大神用二分来写,牛B..霸气侧漏啊



AC Program:

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<set>
using namespace std;
char key[25][25];
char exe[75];
char excuses[25][75];
int cnt[100];
int k,e;
int find (char tmp[])//暴力单词匹配 
{
   int sum=0;
   for(int i=0;i<k;i++)
   {
     //scanf("%s",key[i]);        
     if(strcmp(tmp,key[i])==0)
        sum++;
   }   
   return sum;
}
int main()
{
int cas=1;
//freopen("f.out.txt","w",stdout);
while(~scanf("%d%d",&k,&e))
{
   for(int i=0;i<k;i++)
   {
     scanf("%s",key[i]);        
   }
   int max=0; 
   getchar();//用gets的时候最好都要仔细斟酌一番getchar 
   for(int i=0;i<e;i++)
   {
       gets(excuses[i]); 
       /*      //一开始以为最后输出也是小写,后来调试的时候才发现不是 
       int len=strlen(excuses[i]); 
       for(int j=0;j<len;j++)//注意在比较之前化成小写   
          if(excuses[i][j]<='Z' && excuses[i][j]>='A')
               excuses[i][j]+=32;
       */
   }                   
   memset(cnt,0,sizeof(cnt));//note!!
   for(int i=0;i<e;i++)      
   {
      strcpy(exe,excuses[i]);
      int len=strlen(exe);
      char tmp[75];
      int kg=0;
      for(int j=0;j<len;j++)
      {
        if(exe[j]<='z' && exe[j]>='a')
             tmp[kg++]=exe[j];
        else if (exe[j]<='Z' &&exe[j]>='A')       
             tmp[kg++]=exe[j]+32;//转换小写 
        else 
            {
               tmp[kg]='\0';//这里是把后面的之前已经出现过字符的都给灭了 
               if(kg!=0)
                   cnt[i]+=find(tmp);                     
               if(cnt[i]>max)
                   max=cnt[i];//设定一个最大值,然后把和最大值相等的都输出来,nice 
               kg=0;
            }
      }      
   }
   printf("Excuse Set #%d\n",cas++); 
   for(int i=0;i<e;i++)
   {
      if(cnt[i]==max)
          printf("%s\n",excuses[i]);        
   }
   printf("\n");
}
 
//system("pause");
return 0;}






你可能感兴趣的:(UVA 409 Excuses, Excuses!)