用 '\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
请问多读入一个字符 '@',耗时竟然有这么大的差别吗?