标签:style blog http color os io 数据 for
3 [(]) (]) ([[]()])
No No Yes
#include <iostream> #include <stack> using namespace std; int main() { int N; cin>>N; while(N--) { stack<char> s; char st[10005]; int f=1,i=0; cin>>st; for(;st[i]&&f;i++) if(st[i]==‘(‘||st[i]==‘[‘)s.push(st[i]); else switch(st[i]) { case ‘)‘: if(!s.empty()&&s.top()==‘(‘)s.pop(); else f=0; break; case ‘]‘: if(!s.empty()&&s.top()==‘[‘)s.pop(); else f=0; break; } if(f&&s.empty())cout<<"Yes\n"; else cout<<"No\n"; } return 0; }
#include <iostream>
#include <stack>
using namespace std;
int main()
{
int N;
cin>>N;
while(N--)
{
stack<char> s;
char st[10005];
int f=1,i=0;
cin>>st;
for(;st[i]&&f;i++)
if(st[i]==‘(‘||st[i]==‘[‘)s.push(st[i]);
else
switch(st[i])
{
case ‘)‘:
if(!s.empty()&&s.top()==‘(‘)s.pop();
else f=0;
break;
case ‘]‘:
if(!s.empty()&&s.top()==‘[‘)s.pop();
else f=0;
break;
}
if(f&&s.empty())cout<<"Yes\n";
else cout<<"No\n";
}
return 0;
}
#include <iostream> #include <cstring> #include <cstdio> #include <stack> using namespace std; int main() { int n,i,bz; char s[10008]; stack <char> my; cin>>n; gets(s); while (n--) { gets(s); for (i=0;i<strlen(s);i++) { if ( s[i]== ‘)‘) if ( !my.empty() && my.top()==‘(‘ ) { my.pop(); continue; } if ( s[i]== ‘]‘ ) if ( !my.empty() && my.top()==‘[‘ ) { my.pop(); continue; } my.push(s[i]); } if ( my.empty() ) cout<<"Yes\n"; else cout<<"No\n"; while (!my.empty()) my.pop(); } return 0; }
#include <iostream>
#include <cstring>
#include <cstdio>
#include <stack>
using namespace std;
int main()
{
int n,i,bz;
char s[10008];
stack <char> my;
cin>>n;
gets(s);
while (n--)
{
gets(s);
for (i=0;i<strlen(s);i++)
{
if ( s[i]== ‘)‘)
if ( !my.empty() && my.top()==‘(‘ )
{ my.pop(); continue; }
if ( s[i]== ‘]‘ )
if ( !my.empty() && my.top()==‘[‘ )
{ my.pop(); continue; }
my.push(s[i]);
}
if ( my.empty() ) cout<<"Yes\n";
else cout<<"No\n";
while (!my.empty()) my.pop();
}
return 0;
}
标签:style blog http color os io 数据 for
原文地址:http://www.cnblogs.com/2014acm/p/3887121.html