这题可以用strstr()函数做也可以不用,贴上不用的,用strstr时注意单词不能被包含在一个更长的单词里面,
包含文件:
string.h
函数名: strstr
函数原型:extern char *strstr(char *str1, const char *str2);
语法:* strstr(str1,str2)
str1: 被查找目标 string expression to search.
str2: 要查找对象 The string expression to find.
返回值:若str2是str1的子串,则先确定str2在str1的第一次出现的位置,并返回此位置到str1末尾的所有字符;如果str2不是str1的子串,则返回NULL。(注:若想返回str2在str1第一次出现的位置,不是这个函数)
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<string>
#include<map>
#include<set>
#include<cmath>
#include<vector>
#include<queue>
#include<stack>
using namespace std;
char key[25][25];
char excuse[25][75];
int main()
{
int k,e;
int cas=0;
while(cin>>k>>e)
{
getchar();
cas++;
for(int i=0;i<k;i++)
gets(key[i]);
for(int i=0;i<e;i++)
gets(excuse[i]);
int a[25];
memset(a,0,sizeof(a));
int amax=0;
for(int i=0;i<e;i++)
{
char c[100],len=0;
for(int j=0;j<strlen(excuse[i]);j++)
{
if(isalpha(excuse[i][j]))
{
c[len++]=excuse[i][j];
c[len-1]=tolower(c[len-1]);
}
else
{
c[len]='\0';
for(int x=0;x<k;x++)
if(strcmp(c,key[x])==0)
a[i]++;
len=0;
}
/*if(isalpha(excuse[i][j]))
{
char c[100];
int len=0;
for(;j<strlen(excuse[i]);j++)
{
if(isalpha(excuse[i][j]))
{
c[len++]=excuse[i][j];
c[len-1]=tolower(c[len-1]);
}
else
break;
}
c[len]='\0';
for(int x=0;x<k;x++)
if(strcmp(c,key[x])==0)
a[i]++;
}*/
}
amax=max(amax,a[i]);
}
printf("Excuse Set #%d\n",cas);
for(int i=0;i<e;i++)
if(a[i]==amax&&amax!=0)
printf("%s\n",excuse[i]);
cout<<endl;
}
return 0;
}