You are given a string consisting of parentheses () and [].
A string of this type is said to be correct:
(a) if it is the empty string
(b) if A and B are correct, AB is correct,
(c) if A is correct, (A) and [A] is correct.
Write a program that takes a sequence of strings of this type and check their correctness.
Your program can assume that the maximum string length is 128.
Input
The file contains a positive integer n and a sequence of n strings of parentheses ‘()’ and ‘[]’, one stringa line.OutputA sequence of ‘Yes’ or ‘No’ on the output file.
Sample Input
3
([])
(([()])))
([()[]()])()
Sample Output
Yes
No
Yes
题意 很好理解
1 如果 是空 则合法。
2 如果 A与B合法 ,则AB合法。
3 如果A合法 (A)[A]合法。
用栈来模拟这个过程。
每次取一个元素
如果与栈顶匹配,就pop栈顶,如果不匹配,压入栈中
到最后,如果为空,则合法
错误记录
1.RE
把 stack < >q定义在了主函数内。运行内存爆了
2.WA
未考虑情况(1) 即为空行的情况
错误代码
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <map>
#include <set>
#include <vector>
#include <iostream>
#include <algorithm>
#include <queue>
#include <stack>
#include <sstream>
#include <string>
#define maxn 100000000
using namespace std;
char s[ maxn];
stack < char > q;
int main () {
int t;
scanf ( "%d" ,& t);
while ( t--) {
while (! q. empty ()) q. pop ();
cin>> s;
int flag= 0 ;
for ( int i = 0 ; s[ i] ; i++) {
if ( s[ i]== '(' || s[ i]== '[' )
q. push ( s[ i]);
else {
if (! q. empty ()&&(( s[ i]== ')' && q. top ()== '(' )||( s[ i]== ']' && q. top ()== '[' )))
q. pop ();
else {
flag= 1 ;
break ;
}
}
}
if (! q. empty ()) flag = 1 ;
if ( flag) printf ( "No \n " );
else printf ( "Yes \n " );
}
}
经过修改后
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <map>
#include <set>
#include <vector>
#include <iostream>
#include <algorithm>
#include <queue>
#include <stack>
#include <sstream>
#include <string>
#define maxn 100000000
using namespace std;
string s;
stack < char > q;
int main () {
int t;
scanf ( "%d" ,& t);
getchar ();
while ( t--) {
while (! q. empty ()) q. pop ();
getline ( cin, s);
int flag= 0 ;
for ( int i = 0 ; s[ i] ; i++) {
if ( s[ i]== '(' || s[ i]== '[' )
q. push ( s[ i]);
else {
if (! q. empty ()&&(( s[ i]== ')' && q. top ()== '(' )||( s[ i]== ']' && q. top ()== '[' )))
q. pop ();
else {
flag= 1 ;
break ;
}
}
}
if (! q. empty ()) flag = 1 ;
if ( flag) printf ( "No \n " );
else printf ( "Yes \n " );
}
}
通过 getline 函数来实现 100ms
简化
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <map>
#include <set>
#include <vector>
#include <iostream>
#include <algorithm>
#include <queue>
#include <stack>
#include <sstream>
#include <string>
#define maxn 100000000
using namespace std;
stack < char > q;
int main () {
int t;
scanf ( "%d%*c" ,& t);
while ( t--) {
while (! q. empty ()) q. pop ();
char s;
int flag= 0 ;
while (( s= getchar ()) != ' \n ' ) {
if ( s== '(' || s== '[' )
q. push ( s);
else {
if ( q. empty ())
q. push ( s);
else if ( s== ']' && q. top ()== '[' )
q. pop ();
else if ( s== ')' && q. top ()== '(' )
q. pop ();
else q. push ( s);
}
}
if (! q. empty ()) flag = 1 ;
if ( flag) printf ( "No \n " );
else printf ( "Yes \n " );
}
}
通过 getchar()来读取 高效而便于编程, 时间 20ms
事实上大量输入的情况下 cin 还是挺慢的