给定一个字符串 s 和一个敏感词列表,请将 s 中所有出现的敏感词替换为 ***。
敏感词替换不区分大小写,但替换后统一用 *** 表示。
输入描述
第一行输入字符串 s。
第二行输入一个整数 n,表示敏感词数量。
接下来 n 行,每行输入一个敏感词。
输出描述
输出替换后的字符串。
示例 1
输入
text
Hello World
2
hello
world
输出
text
*** ***
C++ 解法
cpp
#include <bits/stdc++.h>
using namespace std;
string toLower(string s) {
for (char& c : s) c = tolower(c);
return s;
}
int main() {
string s;
getline(cin, s);
int n;
cin >> n;
cin.ignore();
vector<string> words(n);
for (int i = 0; i < n; i++) {
getline(cin, words[i]);
}
string lowerS = toLower(s);
for (auto& w : words) {
string lw = toLower(w);
int pos;
while ((pos = lowerS.find(lw)) != string::npos) {
s.replace(pos, w.size(), "***");
lowerS.replace(pos, w.size(), "***");
}
}
cout << s << endl;
return 0;
}