前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >2024团体设计天梯赛之L1-101 别再来这么多猫娘了

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

作者头像
用户11036582
发布2024-04-25 18:53:48
1900
发布2024-04-25 18:53:48
举报

题目介绍:

首先我们来看一下这道题的题目内容:

这就是这个题的题目,在满足违禁词不超过阈值的情况下,将违禁词全部替换成<censored>,但这种情况下我们需要考虑,如过这个<censored>是违禁词怎么办,我们如果在找的过程中就替换的话会出现重复替换的情况,这样会造成超时或者其他的一些影响,所以这里我们应该用一些特殊的符号先来代替这个违禁词,后续再将违禁词更换为<censored>。再一种情况就是如果大于等于违禁词,就要输出个数与那句话,所以同时我们还要记录着违禁词的个数

下面我们来看一下代码吧:

本题代码:

C++版本

代码语言:javascript
复制
#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语言版本:

代码语言:javascript
复制
#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;
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2024-04-25,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 题目介绍:
  • 本题代码:
    • C++版本
      • C语言版本:
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档