Code: Select all
2
([]))
bye
Moderator: Board moderators
Code: Select all
2
([]))
Code: Select all
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main()
{
char str[150];
int top = 0,i,n;
char stack[150];
scanf("%d%*c",&n);
while(n--)
{
gets(str);
int left = 0;
int right = 0;
for(i = 0; str[i]; i++)
{
if( (str[i] == '(') || (str[i] == '[') )
{ stack[top++] = str[i];
left++;
}
if( (str[i] == ')') )
{
if( stack[--top] == '(' )
right++;
else
break;
}
if( (str[i] == ']') )
{
if(stack[--top] == '[')
right++;
else
break;
}
}
if(left == right)
printf("Yes\n");
else
printf("No\n");
}
return 0;
}
Code: Select all
4
(])
[)]
[)))]
[[)]
Code: Select all
got Ac
Code: Select all
7
([])
(([()])))
([()[]()])()
([)]
(
()(
[()
Code: Select all
Yes
No
Yes
Yes
No
No
No
Code: Select all
Yes
No
Yes
No
No
No
No
Code: Select all
#include<iostream>
#include<stack>
#include<cstdio>
using namespace std;
int main()
{
int n;
cin>>n;
fflush(stdin);
while(n>0)
{
char str[129];
stack<char> s;
gets(str);
if(strlen(str)!=0)
{
int i=0;
bool flag=true;
while(flag==true && i<strlen(str))
{
if(str[i]=='('||str[i]=='[')
{s.push(str[i]);i++;}
else
{
if(str[i]==']')
{
if(!s.empty())
{
if(s.top()=='[')
{
s.pop();
i++;
}
else
{
flag==false;
break;
}
}
else
{
flag=false;
break;
}
}
if(str[i]==')')
{
if(!s.empty())
{
if(s.top()=='(')
{
s.pop();
i++;
}
else
{
flag==false;
break;
}
}
else
{
flag=false;
break;
}
}
}
}
if(s.empty()&&i==strlen(str))
cout<<"Yes"<<endl;
else
cout<<"No"<<endl;
}
else
cout<<"Yes"<<endl;
n--;
}
return 0;
}
Code: Select all
#include<stdio.h>
#include<string.h>
int main()
{
char c,s[129],stack[129];
int sp=-1,i,j,n,l,k;
scanf("%d",&n);
c=getchar();
for(i=1;i<=n;i++)
{
sp=-1;k=0;
while(1)
{
c=getchar();
s[k++]=c;
if(c=='\n' || c==EOF) {s[k]='\0';break;}
}
l=strlen(s);
for(j=0;j<l;j++)
{c=s[j];
if(c=='[' || c=='(') stack[++sp]=c;
else if(c==')') if(sp>=0 && stack[sp]=='(') sp--;
else break;
else if(c==']') if(sp>=0 && stack[sp]=='[') sp--;
else break;
}
if(j==l && sp==-1) printf("Yes\n"); else printf("No\n");
}
return 0;
}