为什么用 '\n' 做结束符就会 TLE 呢?
查看原帖
为什么用 '\n' 做结束符就会 TLE 呢?
335552
Christophe_楼主2021/7/20 20:43

用 '\n’:

#include<cstdio>
using namespace std;
char *s = new (char), t;
int top = 0;
int main(void) {
	while ((t = getchar()) != '\n') {
		if (t == '(' || t == ')') s[++top] = t;
		if (s[top] == ')' && s[top - 1] == '(') top -= 2;
	}
	printf(top == 0 ? "YES" : "NO");
	return 0;
}

总时间:8.41s 每个测试点:1.2s

用 '@':

#include<cstdio>
using namespace std;
char *s = new (char), t;
int top = 0;
int main(void) {
	while ((t = getchar()) != '@') {
		if (t == '(' || t == ')') s[++top] = t;
		if (s[top] == ')' && s[top - 1] == '(') top -= 2;
	}
	printf(top == 0 ? "YES" : "NO");
	return 0;
}

总时间:24ms 每个测试点:2ms

请问多读入一个字符 '@',耗时竟然有这么大的差别吗?

2021/7/20 20:43
加载中...