#P2197. 第1题-和谐敏感词
-
1000ms
Tried: 86
Accepted: 39
Difficulty: 2
所属公司 :
京东
时间 :2024年10月19日
第1题-和谐敏感词
题目描述:
小塔开发了一款聊天软件,用户在聊天过程中可能会使用一些敏感词。为了维护聊天的和谐,设计一个程序来替换用户输入中的敏感词。程序的输入包括一个整数 n 表示敏感词的数量,接着是一段待处理的字符串 S,以及接下来的 n 行每行一个敏感词。若待处理文本中包含敏感词的子串,则将该子串替换为 *,其余字符不变。最终输出处理后的字符串。
思路:
数据范围很小直接暴力枚举所有敏感词一个一个区匹配即可.
代码说明
-
输入部分:
- 首先读取一个整数
n,表示敏感词的数量。 - 接着读取需要处理的字符串
S。 - 然后读取
n个敏感词,存储在sensitive_words向量中。
- 首先读取一个整数
-
初始化标记数组:
- 创建一个布尔向量
mask,长度与字符串S相同,初始值均为false。该数组用于标记字符串S中哪些字符需要被替换。
- 创建一个布尔向量
-
匹配敏感词:
- 对于每一个敏感词,使用暴力枚举的方法遍历字符串
S,寻找与敏感词匹配的子串。 - 使用
substr函数提取与敏感词长度相同的子串,比较是否相同。如果找到匹配,将对应的位置在mask中标记为true。
- 对于每一个敏感词,使用暴力枚举的方法遍历字符串
-
生成输出字符串:
- 创建一个空字符串
result,用于存储处理后的结果。 - 遍历字符串
S,根据mask中的标记决定是保留原字符还是替换为*。将结果逐字符构建到result中。
- 创建一个空字符串
-
输出结果:
- 最后输出处理后的字符串
result。
- 最后输出处理后的字符串
复杂度分析
由于敏感词的数量 n 最大为 10,而每个敏感词的长度最大为 5,字符串 S 的长度最大为 10^4。因此,代码的时间复杂度为 O(n * m * |S|),其中 m 是敏感词的最大长度(最多为 5)。在实际情况下,考虑到 n 较小,这种暴力枚举的方法在可接受的时间范围内。
cpp
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main(){
int n;
cin >> n; // 读取敏感词的数量
string S;
cin >> S; // 读取需要处理的字符串
vector<string> sensitive_words(n);
for(int i = 0; i < n; ++i){
cin >> sensitive_words[i]; // 读取每一个敏感词
}
int len_S = S.length();
// 初始化标记数组,默认所有位置不需要替换
vector<bool> mask(len_S, false);
// 遍历每一个敏感词,标记需要替换的位置
for(const auto &word : sensitive_words){
int len_word = word.length();
if(len_word == 0) continue; // 忽略空字符串
for(int i = 0; i <= len_S - len_word; ++i){
// 判断子串是否匹配敏感词
if(S.substr(i, len_word) == word){
for(int j = i; j < i + len_word; ++j){
mask[j] = true; // 标记需要替换的位置
}
}
}
}
// 生成替换后的字符串
string result = "";
for(int i = 0; i < len_S; ++i){
if(mask[i]){
result += '*';
}
else{
result += S[i];
}
}
cout << result << endl; // 输出结果
return 0;
}
java
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt(); // 读取敏感词的数量
scanner.nextLine(); // 读取行结束符
String S = scanner.nextLine(); // 读取需要处理的字符串
String[] sensitiveWords = new String[n]; // 存储敏感词
for (int i = 0; i < n; i++) {
sensitiveWords[i] = scanner.nextLine(); // 读取每一个敏感词
}
int lenS = S.length();
// 初始化标记数组,默认所有位置不需要替换
boolean[] mask = new boolean[lenS];
// 遍历每一个敏感词,标记需要替换的位置
for (String word : sensitiveWords) {
int lenWord = word.length();
for (int i = 0; i <= lenS - lenWord; i++) {
// 判断子串是否匹配敏感词
if (S.substring(i, i + lenWord).equals(word)) {
for (int j = i; j < i + lenWord; j++) {
mask[j] = true; // 标记需要替换的位置
}
}
}
}
// 生成替换后的字符串
StringBuilder result = new StringBuilder();
for (int i = 0; i < lenS; i++) {
if (mask[i]) {
result.append('*'); // 如果需要替换,添加 '*'
} else {
result.append(S.charAt(i)); // 否则添加原字符
}
}
System.out.println(result.toString()); // 输出结果
scanner.close(); // 关闭扫描器
}
}
python
def main():
n = int(input()) # 读取敏感词的数量
S = input() # 读取需要处理的字符串
sensitive_words = [input() for _ in range(n)] # 读取每一个敏感词
len_S = len(S)
# 初始化标记数组,默认所有位置不需要替换
mask = [False] * len_S
# 遍历每一个敏感词,标记需要替换的位置
for word in sensitive_words:
len_word = len(word)
for i in range(len_S - len_word + 1):
# 判断子串是否匹配敏感词
if S[i:i + len_word] == word:
for j in range(i, i + len_word):
mask[j] = True # 标记需要替换的位置
# 生成替换后的字符串
result = []
for i in range(len_S):
if mask[i]:
result.append('*') # 如果需要替换,添加 '*'
else:
result.append(S[i]) # 否则添加原字符
print(''.join(result)) # 输出结果
if __name__ == "__main__":
main()
题目内容
小红开发了一个聊天软件,起初大家都很融洽的聊天。
但是有一天,有些人说了一些不好的话,很容易引起争吵,于是小红希望实现一个和谐神器对大家说的话进行处理。
和谐神器实现这样的功能:对于大家说的话(字符串),小红给出一个敏感词库,
对于大家说的话,如果任何一个字符在形式上属于一个子串,其形式与任何一个敏感词库中的词汇相同(同样也是字符串),
那么这个子串会被和谐符号 替换下面要求你帮助小红来实现一下对字符串的处理。
输入描述
第 1 行一个数字 n ,表示敏感词库中的文本数。
第 2 行一个字符串 S ,表示要被处理的文本,为简化处理,字典为全体小写英文字母的集合。
接下来第 3 到 n+2 行,第 i+2 行一个字符串 t ;表示一个敏感词汇,字典也是全体小写英文字母的集合。
1≤n≤10,1≤∣S∣≤104,1≤∣ti∣≤5。
输出描述
一行一个字符串,代表处理后的字符串,理论上字典应为全体小写英文字母和 '*' 构成的集 合。
样例1
输入
3
iakioi
ki
io
qwq
输出
ia***i