2024团体设计天梯赛之L1-101 别再来这么多猫娘了

9efbcbc3d25747719da38c01b3fa9b4f.gif

 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;
}

 

相关推荐

  1. 第19届PTA天梯 这么

    2024-04-23 22:42:02       13 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-04-23 22:42:02       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-04-23 22:42:02       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-04-23 22:42:02       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-04-23 22:42:02       20 阅读

热门阅读

  1. C# 四种定时器的用法

    2024-04-23 22:42:02       15 阅读
  2. jsoncpp解析文件

    2024-04-23 22:42:02       14 阅读
  3. 骑砍2霸主MOD开发(4)-游戏场景(scene)制作

    2024-04-23 22:42:02       17 阅读
  4. Python面试高频题

    2024-04-23 22:42:02       18 阅读
  5. Edge的使用心得与深度探索

    2024-04-23 22:42:02       28 阅读
  6. 利用vue3SeamlessScroll 简单实现列表的无限循环滚动

    2024-04-23 22:42:02       13 阅读