网络警察
时间限制: 1S类别: 字符串->较难
问题描述 :
作为一名网络警察,你的任务是监视电子邮件,看其中是否有一些敏感的关键词。不过,有些狡猾的犯罪嫌疑人会改变某些单词的字母顺序,以逃避检查。请编写一个程序,发现这种调整过顺序的关键词。
输入说明 :
输入有两行,第一行是关键词列表,第二行是待检查的句子。
单词全部为小写,单词之间以一个空格分隔,每一行的单词个数不限
输出说明 :
输出为在该句子中所找到的经过顺序调整的关键词
按照在关键词列表中的先后顺序输出
方法一(简单易懂 0v<):
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
#include <algorithm>
using namespace std;
bool wordInStr(const string& word, const string& str)
{
for (char c : word)
{
if (str.find(c) == string::npos||word.size()!=str.size())
{
return 0;
}
}
return 1;
}
int main()
{
vector<string> words;
string word,s;
vector<string> str;
int count=0;
// 读取单词直到遇到换行符
while (cin >> word)
{
words.push_back(word);
if (cin.peek() == '\n')
break;
}
// 清除换行符
cin.ignore(numeric_limits<streamsize>::max(), '\n');
// 读取字符串s
while (cin >> s)
{
str.push_back(s);
if (cin.peek() == '\n')
break;
}
// 遍历每个单词
for (const auto& w : words)
{
for(const auto&ss:str)
{
if (wordInStr(w, ss))
{
++count;
if(count>1) cout<<" ";
cout << w;
}
}
}
cout<<endl;
return 0;
}
方法二(逻辑清晰但不好写UwU):
#include <iostream>
#include <sstream>
#include <vector>
#include <algorithm>
#include <iterator>
using namespace std;
//anagram变形词
vector<string> findAnagrams(const vector<string>& keywords, const string& sentence)
{
vector<string> foundKeywords;
istringstream ss(sentence);
string word;
// 处理每一个关键词
for (const auto& keyword : keywords)
{
// 排序,将输入的关键词变成字典序
string sortedKeyword = keyword;
sort(sortedKeyword.begin(), sortedKeyword.end());
// 用一个新的ss来保存,而不是直接在原关键词上改变,否则输出是错误的单词。
ss.clear();
ss.seekg(0, ios::beg);
/*ss.seekg(0, ios::beg)是用来设置输入流 ss 的位置指针。这个函数调用会将指针移动到流的开始处。
seekg 函数是 istream 类的成员,用于设置输入位置(get position)。它接受两个参数:
第一个参数是偏移量,这里是 0,表示相对于第二个参数指定的位置没有偏移。
第二个参数是基准位置。ios::beg 表示基准位置是流的开始处。*/
while (ss >> word)
{
string sortedWord = word;
sort(sortedWord.begin(), sortedWord.end());//将第二行的输入单词排序
if (sortedWord == sortedKeyword)
{
foundKeywords.push_back(keyword);
break; // 找到匹配的就停止检验
}
}
}
return foundKeywords;
}
int main()
{
string keywordsLine, sentence;
getline(cin, keywordsLine);
getline(cin, sentence);
// 读入单词
istringstream iss(keywordsLine);
vector<string> keywords((istream_iterator<string>(iss)),istream_iterator<string>());
//用法见备注
// 在句子中找到变形词
vector<string> foundKeywords = findAnagrams(keywords, sentence);
// 输出结果
for (int i = 0; i < foundKeywords.size(); ++i)
{
cout << foundKeywords[i];
if (i < foundKeywords.size() - 1)
{
cout << " ";
}
}
cout <<endl;
return 0;
}
备注:
在C++中,`vector<string> keywords((istream_iterator<string>(iss)), istream_iterator<string>());` 这行代码用来初始化一个字符串向量(`vector<string>`),其内容是通过从一个输入字符串流(`istringstream`)中读取的字符串。
这里涉及到几个C++的高级特性:
1. **`istream_iterator<string>`**:这是一个模板类,用于从一个输入流中读取连续的 `string` 元素。它是标准库中的一部分,用于迭代输入流中的数据。
2. **构造函数**:`istream_iterator<string>(iss)` 创建了一个 `istream_iterator` 对象,它被绑定到 `istringstream` 对象 `iss` 上。这个迭代器读取 `iss` 中的字符串,直到遇到空白字符为止(默认情况下,空白字符包括空格、换行符和制表符等)。
3. **结束哨兵**:`istream_iterator<string>()` 创建了一个默认的、没有绑定到任何流的 `istream_iterator` 对象。按照惯例,这是一个"结束哨兵",表示流的末尾。
4. **范围构造函数**:`vector` 提供了一个范围构造函数,它可以接受一对迭代器,作为一个范围来初始化 `vector` 的内容。第一个迭代器指向范围的开始,第二个迭代器指向范围的结束。
结合这些特性,`vector<string> keywords((istream_iterator<string>(iss)), istream_iterator<string>());` 这行代码实际上告诉 `vector`,它应该从 `iss` 流的当前位置开始读取字符串,直到遇到流的末尾。所有读取的字符串都被存储在 `vector` 中。
这是一种非常紧凑的方式,用来将一个流中的所有单词读入到一个 `vector<string>` 中。这种方法避免了显式地编写循环来从流中读取并存储每个单词。