跳到正文

题解

P7008-题解

Edwinlau约 2 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2026年3月14日
篇幅
约 2 分钟
题目
P7008

版本

  1. v2当前
  2. v1

P7008 题解

这应该是这道题唯一一篇C语言题解


题目意思很简单,就是在多个字符串中去除指定的字符串,依次输出结果。 这是一道模拟题。 首先观察数据和时间范围,发现 n <= 100 & |s| <= 100,且时限是 1s,考虑暴力。 我们可以把声响序列全部读进来,然后变成字符串数组。当然你也可以直接 getchar 读进来,然后逐个匹配。 最后放上代码

C
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#define MAXN 102
char buf[MAXN], buf1[MAXN], buf2[MAXN], buf3[MAXN], s[MAXN][1024];
bool flag[MAXN];  //是否不是狐狸的叫声
#define equal_string(x, y) (!strcmp((x), (y)))
int main() {
	int T;
	scanf("%d", &T);
	gets(buf);
	while (T--) {
		memset(flag, 0, sizeof flag);
		gets(buf);
		int len = strlen(buf), cnt = 1;
		strcpy(s[1], "");
		for (int i = 0; i < len; i++) {
			char struct_[2] = { buf[i], '\0'};
			if (buf[i] == ' ')  cnt++, strcpy(s[cnt], "");
			else  strcat(s[cnt], struct_);
		}
		while (scanf("%s %s %s", buf1, buf2, buf3), equal_string(buf2, "goes"))  {
			for (int i = 1; i <= cnt; i++)  {
				if (equal_string(s[i], buf3))  {
					flag[i] = 1;
				}
			}
		}
		for (int i = 1; i <= cnt; i++)  {
			if (!flag[i]) {
				printf("%s ", s[i]);
			}
		}
		gets(buf);
		putchar('\n');
	}
	return 0;
}
原文