c语言中的小小白-CSDN博客c语言中的小小白关注算法,c++,c语言,贪心算法,链表,mysql,动态规划,后端,线性回归,数据结构,排序算法领域.https://blog.csdn.net/bhbcdxb123?spm=1001.2014.3001.5343
给大家分享一句我很喜欢我话:
知不足而奋进,望远山而前行!!!
铁铁们,成功的路上必然是孤独且艰难的,但是我们不可以放弃,远山就在前方,但我们能力仍然不足,所有我们更要奋进前行!!!
今天我们更新了猫娘内容,
🎉 欢迎大家关注🔍点赞👍收藏⭐️留言📝
题目介绍:
首先我们来看一下这道题的题目内容:
这就是这个题的题目,在满足违禁词不超过阈值的情况下,将违禁词全部替换成<censored>,但这种情况下我们需要考虑,如过这个<censored>是违禁词怎么办,我们如果在找的过程中就替换的话会出现重复替换的情况,这样会造成超时或者其他的一些影响,所以这里我们应该用一些特殊的符号先来代替这个违禁词,后续再将违禁词更换为<censored>。再一种情况就是如果大于等于违禁词,就要输出个数与那句话,所以同时我们还要记录着违禁词的个数
下面我们来看一下代码吧:
本题代码:
C++版本
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int n, m;
string s, S[110];
int main()
{
cin >> n;
cin.ignore();
for (int i = 0; i < n; i++)getline(cin, S[i]);
cin >> m;
if (m == 0) cout << 0 << endl << "He Xie Ni Quan Jia!";
else
{
cin.ignore();
int cnt = 0;
getline(cin, s);
for (int i = 0; i < n; i++)
{
while (s.find(S[i]) != -1)
{
int pos = s.find(S[i]);
cnt++;
s.erase(pos, S[i].length());
s.insert(pos, "^-^");
}
}
if (cnt >= m) cout << cnt << endl << "He Xie Ni Quan Jia!";
else
{
// 找出违禁词的位置然后替换
while (s.find("^-^") != -1)
{
int t = s.find("^-^");
s.erase(t, 3);
s.insert(t, "<censored>");
}
cout << s;
}
}
return 0;
}
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int n, m;
string s, S[110];
int main()
{
cin >> n;
cin.ignore();
for (int i = 0; i < n; i++)getline(cin, S[i]);
cin >> m;
if (m == 0) cout << 0 << endl << "He Xie Ni Quan Jia!";
else
{
cin.ignore();
int cnt = 0;
getline(cin, s);
for (int i = 0; i < n; i++)
{
while (s.find(S[i]) != -1)
{
int pos = s.find(S[i]);
cnt++;
s.erase(pos, S[i].length());
s.insert(pos, "^-^");
}
}
if (cnt >= m) cout << cnt << endl << "He Xie Ni Quan Jia!";
else
{
// 找出违禁词的位置然后替换
while (s.find("^-^") != -1)
{
int t = s.find("^-^");
s.erase(t, 3);
s.insert(t, "<censored>");
}
cout << s;
}
}
return 0;
}
C语言版本:
#include <stdio.h>
#include <string.h>
#define MAX_WORDS 110
#define MAX_LENGTH 1000
int n, m;
char s[MAX_LENGTH];
char S[MAX_WORDS][MAX_LENGTH];
void replaceSubstring(char *str, const char *sub, const char *replace) {
char temp[MAX_LENGTH];
char *p;
while ((p = strstr(str, sub)) != NULL) {
strcpy(temp, p + strlen(sub));
*p = '\0';
strcat(str, replace);
strcat(str, temp);
}
}
int main() {
scanf("%d", &n);
getchar(); // Consume the newline character after n
for (int i = 0; i < n; i++) {
fgets(S[i], MAX_LENGTH, stdin);
strtok(S[i], "\n"); // Remove newline character from the end
}
scanf("%d", &m);
getchar(); // Consume the newline character after m
if (m == 0) {
printf("0\nHe Xie Ni Quan Jia!\n");
} else {
fgets(s, MAX_LENGTH, stdin);
strtok(s, "\n"); // Remove newline character from the end
int cnt = 0;
for (int i = 0; i < n; i++) {
char *pos = s;
while ((pos = strstr(pos, S[i])) != NULL) {
cnt++;
replaceSubstring(s, S[i], "^-^");
pos += strlen("^-^");
}
}
if (cnt >= m) {
printf("%d\nHe Xie Ni Quan Jia!\n", cnt);
} else {
replaceSubstring(s, "^-^", "<censored>");
printf("%s\n", s);
}
}
return 0;
}#include <stdio.h>
#include <string.h>
#define MAX_WORDS 110
#define MAX_LENGTH 1000
int n, m;
char s[MAX_LENGTH];
char S[MAX_WORDS][MAX_LENGTH];
void replaceSubstring(char *str, const char *sub, const char *replace) {
char temp[MAX_LENGTH];
char *p;
while ((p = strstr(str, sub)) != NULL) {
strcpy(temp, p + strlen(sub));
*p = '\0';
strcat(str, replace);
strcat(str, temp);
}
}
int main() {
scanf("%d", &n);
getchar(); // Consume the newline character after n
for (int i = 0; i < n; i++) {
fgets(S[i], MAX_LENGTH, stdin);
strtok(S[i], "\n"); // Remove newline character from the end
}
scanf("%d", &m);
getchar(); // Consume the newline character after m
if (m == 0) {
printf("0\nHe Xie Ni Quan Jia!\n");
} else {
fgets(s, MAX_LENGTH, stdin);
strtok(s, "\n"); // Remove newline character from the end
int cnt = 0;
for (int i = 0; i < n; i++) {
char *pos = s;
while ((pos = strstr(pos, S[i])) != NULL) {
cnt++;
replaceSubstring(s, S[i], "^-^");
pos += strlen("^-^");
}
}
if (cnt >= m) {
printf("%d\nHe Xie Ni Quan Jia!\n", cnt);
} else {
replaceSubstring(s, "^-^", "<censored>");
printf("%s\n", s);
}
}
return 0;
}