Description
作为一名网络警察,你的任务是监视电子邮件,看其中是否有一些敏感的关键词。不过,有些狡猾的犯罪嫌疑人会改变某些单词的字母顺序,以逃避检查。请编写一个程序,发现这种调整过顺序的关键词。程序的输入有两行,第一行是关键词列表,第二行是待检查的句子。程序的输出为在该句子中所找到的经过顺序调整的关键词。(单词全部为小写,单词之间以一个空格分隔,每一行的单词个数不限)
Input
第一行是关键词列表,第二行是待检查的句子。
Output
经过顺序调整的关键词。(单词全部为小写,单词之间以一个空格分隔,每一行的单词个数不限)
Sample Input 1
guns mines missiles aameric ssell snug dan iimsssle ot sit neeemis
Sample Output 1
guns missiles
思路:
读取每一行中以空格划分的单词,使用divideSen 和 getSingleWord 函数;如何 判断 关键词和乱序的待检查单词是否相等,使用 isSameLetter 函数。
代码如下
#include<iostream>
#include<vector>
#include<sstream>
using namespace std;
string sort(string &a)//排序函数(必须以&a传参的形式,不然没办法改变)
{
char t;
for (int i = 0; i < a.length(); i++)
{
for (int j = i+1; j < a.length(); j++)
{
if (a[i] >= a[j])
{
t = a[i];
a[i] = a[j];
a[j] = t;
}
}
}
return a;
}
int main()
{
string s,c;
getline(cin, s);//以行的形式输入字符串(即可以读入空格)
getline(cin, c);
vector<string>cha;
istringstream ss(s);
vector<string>arr;
string t;
while(ss>>t)//用字符流以空格分开字符串
{
cha.push_back(t);//存敏感词
arr.push_back(t);//存排序后的敏感词
}
vector<string>sortcha;
istringstream cc(c);
while (cc>>t)
{
sortcha.push_back(t);//存排序后的待检测的词
}
for (int i = 0; i < arr.size(); i++)
{
sort(arr[i]);//排序敏感词
}
for (int i = 0; i < sortcha.size(); i++)
{
sort(sortcha[i]);//排序待检测的词
}
int cnt = 0;
for (int i = 0; i < arr.size(); i++)
{
for (int j = 0; j < sortcha.size(); j++)
{
if (sortcha[j] == arr[i])//相等输出对应位置的敏感词即可
{
if (cnt != 0)
cout << ' ';
cout << cha[i];
cnt++;
}
}
}
return 0;
}