1poj1598(字符串)

来源:互联网 发布:中标麒麟装软件 编辑:程序博客网 时间:2024/05/21 09:59

http://poj.org/problem?id=1598

Excuses, Excuses!
Time Limit: 1000MSMemory Limit: 10000KTotal Submissions: 3148Accepted: 1091

Description

Judge Ito is having a problemwith people subpoenaed for jury duty giving rather lame excuses inorder to avoid serving. In order to reduce the amount of timerequired listening to goofy excuses, Judge Ito has asked that youwrite a program that will search for a list of keywords in a listof excuses identifying lame excuses. Keywords can be matched in anexcuse regardless of case.

Input

Input to your program willconsist of multiple sets of data. Line 1 of each set will containexactly two integers. The first number (1 <= K<= 20) defines the number of keywords to be used inthe search. The second number (1 <= E<= 20) defines the number of excuses in the set tobe searched. Lines 2 through K+1 each contain exactly one keyword.Lines K+2 through K+1+E each contain exactly one excuse. Allkeywords in the keyword list will contain only contiguous lowercase alphabetic characters of length L (1 <= L<= 20) and will occupy columns 1 through L in theinput line. All excuses can contain any upper or lower casealphanumeric character, a space, or any of the followingpunctuation marks [".,!?] not including the square brackets andwill not exceed 70 characters in length. Excuses will contain atleast 1 non-space character.

Output

For each input set, you are toprint the worst excuse(s) from the list. The worst excuse(s) is/aredefined as the excuse(s) which contains the largest number ofincidences of keywords. If a keyword occurs more than once in anexcuse, each occurrance is considered a separate incidence. Akeyword "occurs" in an excuse if and only if it exists in thestring in contiguous form and is delimited by the beginning or endof 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 #". (Seethe Sample Output). The following line(s) is/are to contain theworst excuse(s) one per line exactly as read in. If there is morethan one worst excuse, you may print them in any order. After eachset 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!

Source

South Central USA 1996
题意:

每次给你K个单词,再给你E行句子,问你哪些句子包含最多单词(给过你的K个单词)

其中,句子中单词是以标点符号和空格分开的,匹配不区分大小写

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
char key[21][100],s[21][100],ch[100];
int num[100];
int main()
{
 int n,m;
 int count=1;
 while(scanf("%d%d",&n,&m)!=EOF)
 {
  getchar();
  int i,j,z;
  int max=0;
  for(i=0;i<n;i++)
  {
   scanf("%s",key[i]);
   getchar();
  }
  memset(num,0,sizeof(num));
  for(i=0;i<m;i++)
  {
   gets(s[i]);
   intlen=strlen(s[i]);
   for(j=0;j<len;j++)
   {
    inta=0;
    while(isalpha(s[i][j]))
    {
     ch[a++]=tolower(s[i][j]);
     j++;
    }
    if(a>0)
    {
     ch[a++]='\0';
     for(z=0;z<n;z++)
     {
      if(strcmp(ch,key[z])==0)
      {
       num[i]++;
       break;
      }
     }
    }
   }
   if(num[i]>max)
    max=num[i];
  }
  printf("Excuse Set#%d\n",count++);
  for(i=0;i<m;i++)
  {
   if(num[i]==max)
    printf("%s\n",s[i]);
  }
  printf("\n");
 }
 return 0;
}

原创粉丝点击