//4_7_20: Excuses,Excuses! 寻找在借口中的最多关键字 POJ1598 ZOJ1315
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
char enter,key[25][25],sen[25][100],temp[100],word[100];
int key_cnt[25],sen_cnt[25];
int main()
{
int i,j,k,K,E,max,cnt,flag,count = 1,length;
while(scanf("%d%d%c",&K,&E,&enter) != EOF)
{
memset(sen_cnt,0,sizeof(sen_cnt));
for(i = 0;i < K;i ++) cin.getline(key[i],20);
for(i = 0;i < E;i ++) //输入借口串
{
cin.getline(sen[i],100);
strcpy(temp,sen[i]);
j = 0;
while(sen[i][j])
{
if(sen[i][j] >= 'A' && sen[i][j] <= 'Z') sen[i][j] += 32;
j ++;
}
memset(key_cnt,0,sizeof(key_cnt)); //对于每个借口串中的关键字多少不同
j = cnt = flag = 0; //表示还不是单词
length = strlen(sen[i]);
while(j <= length)
{
if(sen[i][j] >= 'a' && sen[i][j] <= 'z')
{
if(flag == 0)
{
word[cnt++] = sen[i][j++];
flag = 1;
}
else word[cnt++] = sen[i][j++];
}
else
{
if(flag == 0) j++;
else
{
word[cnt] = '\0';
for(k = 0;k < K;k ++)
if(strcmp(word,key[k]) == 0)
{
key_cnt[k]++;
break;
}
cnt = flag = 0;
j ++;
}
}
}
for(j = 0;j < K;j ++) sen_cnt[i] += key_cnt[j];
strcpy(sen[i],temp);
}
printf("Excuse Set #%d\n",count++);
max = sen_cnt[0];
for(i = 1;i < E;i ++) if(sen_cnt[i] > max) max = sen_cnt[i];
for(i = 0;i < E;i ++) if(sen_cnt[i] == max) printf("%s\n",sen[i]);
printf("\n");
}
return 0;
}
/*测试结果:通过POJ1598 ZOJ1315检测
6 3
dog
ate
homework
canary
died
home
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.
Excuse Set #1
Can you believe my dog died after eating my canary... AND MY HOMEWORK?
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.
Excuse Set #2
Can you believe my dog died after eating my canary... AND MY HOMEWORK?
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?
Excuse Set #3
I am having a superhighway built in my bedroom.
There was a thermonuclear war!
^Z
请按任意键继续. . .
*/
POJ1598 ZOJ1315 Excuses,Excuses!
最新推荐文章于 2021-09-16 11:19:49 发布