Re: 673 - Parentheses Balance
Posted: Thu Oct 23, 2014 9:04 pm
Don't use a package. Use class Main.
Code: Select all
#include<stdio.h>
#include<string.h>
int main()
{
char a[130],c;
int n,j,i,flag,flagn,b;
while(scanf("%d",&n)==1)
{
fflush(stdin);
for(j=1;j<=n;j++)
{
flag=0;
flagn=0;
gets(a);
b=strlen(a);
for(i=0;i<b;i++)
{
if(a[i]=='(')
{
flag++;
}
if(a[i]==')')
{
flag--;
}
if(a[i]=='[')
{
flagn++;
}
if(a[i]==']')
{
flagn--;
}
}
if(flag==0 && flagn==0 && (a[0]=='(' && a[b-1]==')') || (a[0]=='[' && a[b-1]==']'))
{
printf("Yes\n");
}
else
{
printf("No\n");
}
fflush(stdin);
}
}
return 0;
}
Code: Select all
#include<iostream>
#include<stack>
#include<string>
using namespace std;
typedef unsigned int uint;
int main()
{
uint t, flag = 1, len,i;
char c, cc;
string ss;
cin>>t;
while(t)
{
flag=1;stack<char> s;i=0;
cin>>ss;
len = ss.length();
while(i < len)
{
c = ss[i];
if((int)c == 40 || (int)c == 91)
{s.push(c); }
else
{
cc = s.top();
if((int)cc-(int)c <= 5)
{s.pop();}
else
{flag=0;break;}
}
i++;
}
if(!s.empty()) flag=0;
if(flag) cout<<"Yes"<<endl;
else cout<<"No"<<endl;
t--;
}
return 0;
}
Code: Select all
#include<iostream>
#include<stack>
#include<string>
using namespace std;
typedef unsigned int uint;
int main()
{
uint t, flag = 1, len,i;
char c, cc;
cin>>t;
while(t)
{
flag=1;stack<char> s;string ss;i=0;
cin>>ss;
len = ss.length();
while(i < len)
{
c = ss[i];
if((int)c == 40 || (int)c == 91)
{s.push(c);}
else
{
if(!s.empty())
{
cc = s.top();
if(((int)c-(int)cc <= 2) && ((int)c-(int)cc > 0))
{s.pop();}
else
{flag=0;break;}
}
else
{flag=0;break;}
}
i++;
}
if(!s.empty()) {flag=0;}
if(flag) cout<<"Yes"<<endl;
else cout<<"No"<<endl;
t--;
}
return 0;
}
brianfry713 wrote:You need to parse the input line by line as a line may be blank.
Input:Output should be:Code: Select all
4 ([]) (([()]))) ([()[]()])()
Code: Select all
Yes Yes No Yes
Code: Select all
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main(void)
{
int n, len, count, front, back;
bool matching;
char backTracker;
bool getFront = false;
cin >> n;
fflush(stdin);
string line;
while(n!=0)
{
getline(cin,line);
line.erase(std::remove(line.begin(), line.end(), ' '), line.end());
count = 0;
matching = true;
len = line.length();
if(line.empty())
matching = true;
else if((len%2!=0) | (line[0]==')') |
(line[0]==']') | (line[len-1]=='(') | (line[len-1]=='['))
matching = false;
else
{
front = len;
while(front>0)
{
if(line[front-1]=='(')
{
backTracker = ')';
getFront = true;
}
else if(line[front-1]=='[')
{
backTracker = ']';
getFront = true;
}
if(getFront)
{
back = front + 1;
while(1)
{
if(line[back-1]==')'| line[back-1]==']')
{
if(line[back-1]!=backTracker)
{
matching = false;
break;
}
else
{
line[back-1]='v'; // already visited
break;
}
}
else
{
back++;
if(back>len)
{
break;
matching = false;
}
}
}
}
getFront = false;
if(!matching)
break;
front--;
}
}
if(matching)
cout << "Yes" << endl;
else
cout << "No" << endl;
n--;
line.clear();
}
return 0;
}
Code: Select all
#include <iostream>
#include <stack>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
using namespace std;
int main()
{
char a[150];
int i,j,p,n;
scanf("%d",&n);
getchar();
while(n--)
{
stack<char> st;
gets(a);
//if(a=="") {cout<<"Yes"<<endl;continue;}
p=0,j=0;
int count1=0,count2=0;
for(int i=0;a[i]!='\0';i++)
{
if(a[i]=='[' || a[i]=='(' || a[i]=='{') {count1++;st.push(a[i]);}
else
{
if(a[i]==']') if(st.top()!='[') {p=1;j=1;break;}
else if(a[i]=='}') if(st.top()!='{') {p=1;j=1;break;}
else if(a[i]==')') if(st.top()!='(') {p=1;j=1;break;}
count2++;
st.pop();
}
}
//cout<<a[0]<<a[1];
//cout<<count1<<" "<<count2;
if(!p && !j && count1==count2) cout<<"Yes"<<endl;
else cout<<"No"<<endl;
fflush(stdin);
}
return 0;
}
Code: Select all
#include<iostream>
#include<string>
#include<stack>
using namespace std;
int main()
{
int n;
cin>>n;
getchar();
int i=0;
while(i<n)
{
stack<char>st;
string str,news;
getline(cin,str);
for(int j=0;j<str.length();j++)
{
if(str[j]==' ')
{
continue;
}
else
{
news+=str[j];
}
}
str=news;
for(int j=0;j<str.length();j++)
{
if(str[j]=='(')
{
st.push('(');
}
else if((str[j]==')'))
{
if(!st.empty()&&st.top()=='(')
{
st.pop();
}
else
{
st.push(str[j]);
}
}
else if(str[j]=='[')
st.push('[');
else if(str[j]==']')
{
if(!st.empty()&&st.top()=='[')
{
st.pop();
}
else
st.push(str[j]);
}
}
if(st.empty())
cout<<"Yes"<<endl;
else
cout<<"NO"<<endl;
i++;
}
return 0;
}
Code: Select all
#include <bits/stdc++.h>
using namespace std;
int main()
{
//cout << "Hello world!" << endl;
freopen("in.txt","r",stdin);
int t;
char str[130];
cin >> t;
getchar();
while(t--)
{
stack<char> stk;
gets(str);
bool isTrue = true;
if(strlen(str) %2 ==1){
cout << "No" << endl;
continue;
}
for(int i=0;str[i];i++)
{
if(str[i]== '('){
stk.push(str[i]);
continue;}
else if(str[i] == '['){
stk.push(str[i]);
continue;
}
if(str[i] == ']'){
if(stk.empty()){
isTrue = false;
break;
}
char ch = stk.top();
stk.pop();
if(ch != '['){
isTrue = false;
break;
}
}else if(str[i] == ')'){
if(stk.empty()){
isTrue = false;
break;
}
char ch = stk.top();
stk.pop();
if(ch != '('){
isTrue = false;
break;
}
}
}
if(isTrue == true)cout << "Yes" << endl;
else cout << "No" << endl;
//cout << str << endl;
}
return 0;
}
Code: Select all
1
((()
Code: Select all
No
Code: Select all
ios_base::sync_with_stdio(false);
Code: Select all
cout << "Yes" << endl;
Code: Select all
cout << "Yes\n";
Code: Select all
freopen("in.txt","r",stdin);