解题思路: 其实就是比较每个字符串包含的关键字符串的个数。然后输出最多的那一串或更多串。
#include <iostream>
#include <string>
using namespace std;
int main() {
int keyCount = 0, excuseCount = 0;
int index =1;
while (cin>> keyCount>> excuseCount) {
string key[22];
for (int i =0; i< keyCount; ++i)
cin>>key[i];
string excuse[22];
int count[22]; // 存放每个字符串包含关键词的个数
scanf("\n");
for (int j=0; j< excuseCount; ++j) {
getline(cin, excuse[j]);
count[j] = 0;
string tmp; // 用于存放当前字符串转换为小写的字符串
for (int m =0; m< excuse[j].length(); ++m)
tmp.push_back(tolower(excuse[j][m]));
for (int i =0; i< keyCount; ++i) {
basic_string<char>::size_type index;
index = tmp.find(key[i]);
// 特殊情况处理
if (index>0 &&(isalpha(excuse[j][index-1] ) >0 || (isalpha(excuse[j][index+ key[i].length()] ) >0 )))
continue;
if (index == 0 && (isalpha(excuse[j][index+ key[i].length()] ) >0 ))
continue;
if (index != string::npos)
count[j]++;
}
}
cout<< "Excuse Set #"<< index<< endl;
index++;
// 先找到包含关键词最大数 再输出相同数指向的字符串
int max = count[0];
for (int k =0; k< excuseCount; ++k)
if (count[k] >max)
max = count[k];
for (int l =0; l< excuseCount; ++l)
if (count[l] == max)
cout<< excuse[l]<< endl;
cout<< endl;
}
return 0;
}