673 - Parentheses Balance
Moderator: Board moderators
-
- Guru
- Posts: 5947
- Joined: Thu Sep 01, 2011 9:09 am
- Location: San Jose, CA, USA
Re: 673 WA!!
A blank line should output Yes.
Check input and AC output for thousands of problems on uDebug!
-
- Guru
- Posts: 5947
- Joined: Thu Sep 01, 2011 9:09 am
- Location: San Jose, CA, USA
Re: 673 WA!!
Input )( should output No.
Check input and AC output for thousands of problems on uDebug!
-
- New poster
- Posts: 11
- Joined: Sun Dec 09, 2012 11:18 pm
Re: 673 WA!!
Code: Select all
#include<iostream>
#include<stack>
#include<vector>
#include<string>
#include<fstream>
#include<cstdlib>
/*farnaws,C++,673,08/12/2012*/
using namespace std;
string verifier(string input_line)
{
stack <char> braces;
int flag=0,count_left_first_brace=0,count_left_third_brace=0;
for(int i=0; i<input_line.size(); i++)
{
if(input_line[i]=='(' || input_line[i]=='[')
{
braces.push(input_line[i]);
if(input_line[i]=='(')
{
count_left_first_brace++;
}
else if(input_line[i]=='[')
{
count_left_third_brace++;
}
}
else if(input_line[i]==')' || input_line[i]==']')
{
if(!(braces.empty()==true))
{
if((input_line[i]==')' && braces.top()=='(') || (input_line[i]==']' && braces.top()=='[') )
{
braces.pop();
}
}
if(input_line[i]==')')
{
count_left_first_brace--;
}
else if(input_line[i]==']')
{
count_left_third_brace--;
}
}
else if(input_line.size()==0){
flag=1;
}
}
if((braces.empty() && !(count_left_third_brace<0 || count_left_first_brace<0)) || flag==1 )
{
return "Yes";
}
else
{
return "No";
}
}
int main()
{
ifstream file_input("C:/Users/Nawshad/Desktop/input.txt");
string read_file;
vector<string> file_contents;
int limit=0;
if(file_input.is_open())
{
while(file_input.good())
{
getline(file_input,read_file);
file_contents.push_back(read_file);
limit=atoi(file_contents[0].c_str());
}
}
else
{
cout<<"File cant be open!"<<endl;
}
ofstream file_output("C:/Users/Nawshad/Desktop/output.txt");
if(file_output.is_open())
{
for(int i=1; i<=limit; i++ )
{
file_output<<verifier(file_contents[i])<<endl;
}
}
else
{
cout<<"File cant be open!"<<endl;
}
return 0;
}
-
- New poster
- Posts: 11
- Joined: Sun Dec 09, 2012 11:18 pm
673 WA, whats wrong??
Code: Select all
#include<iostream>
#include<stack>
#include<vector>
#include<string>
#include<fstream>
#include<cstdlib>
/*farnaws,C++,673,08/12/2012*/
using namespace std;
string verifier(string input_line)
{
stack <char> braces;
int flag=0, count_left_first_brace=0,count_left_third_brace=0;
cout<<"input size:"<<input_line.size()<<endl;
//cout<<braces.empty()<<count_left_first_brace<<count_left_third_brace<<flag<<endl;
for(int i=0; i<input_line.size(); i++)
{
if(input_line[i]=='(' || input_line[i]=='[')
{
braces.push(input_line[i]);
if(input_line[i]=='(')
{
count_left_first_brace++;
}
else if(input_line[i]=='[')
{
count_left_third_brace++;
}
}
else if(input_line[i]==')' || input_line[i]==']')
{
if(!(braces.empty()==true))
{
if((input_line[i]==')' && braces.top()=='(') || (input_line[i]==']' && braces.top()=='[') )
{
braces.pop();
}
}
if(input_line[i]==')')
{
count_left_first_brace--;
}
else if(input_line[i]==']')
{
count_left_third_brace--;
}
}
}
cout<<"Input line is_empty:"<<input_line.empty()<<endl;
//cout<<braces.size()<<count_left_first_brace<<count_left_third_brace<<flag<<endl;
if((braces.size()==0 && count_left_third_brace>=0 && count_left_first_brace>=0) || input_line.empty())
{
return "Yes";
}
else
{
return "No";
}
}
int main()
{
ifstream file_input("C:/Users/Nawshad/Desktop/input.txt");
string read_file;
vector<string> file_contents;
int limit=0;
int count=0;
if(file_input.is_open())
{
while(file_input.good())
{
getline(file_input,read_file);
cout<<read_file;
file_contents.push_back(read_file);
limit=atoi(file_contents[0].c_str());
count++;
}
}
else
{
cout<<"File cant be open!"<<endl;
}
cout<<"count:"<<count<<endl;
cout<<"File Content Size:"<<file_contents.size()<<endl;
ofstream file_output("C:/Users/Nawshad/Desktop/output.txt");
if(file_output.is_open())
{
for(int i=0; i<limit; i++ )
{
file_output<<verifier(file_contents[i+1])<<endl;
}
}
else
{
cout<<"File cant be open!"<<endl;
}
return 0;
}
-
- New poster
- Posts: 11
- Joined: Sun Dec 09, 2012 11:18 pm
Re: 673 WA, whats wrong??
Code: Select all
#include<iostream>
#include<stack>
#include<string>
#include<cstdio>
/*farnaws,C++,673,08/12/2012*/
using namespace std;
string verifier(string input_line)
{
stack <char> braces;
int flag=0, count_left_first_brace=0,count_left_third_brace=0;
for(int i=0; i<input_line.size(); i++)
{
if(input_line[i]=='(' || input_line[i]=='[')
{
braces.push(input_line[i]);
if(input_line[i]=='(')
{
count_left_first_brace++;
}
else if(input_line[i]=='[')
{
count_left_third_brace++;
}
}
else if(input_line[i]==')' || input_line[i]==']')
{
if(!(braces.empty()==true))
{
if((input_line[i]==')' && braces.top()=='(') || (input_line[i]==']' && braces.top()=='[') )
{
braces.pop();
}
}
if(input_line[i]==')')
{
count_left_first_brace--;
}
else if(input_line[i]==']')
{
count_left_third_brace--;
}
}
}
if((braces.size()==0 && count_left_third_brace>=0 && count_left_first_brace>=0) || input_line.empty())
{
return "Yes";
}
else
{
return "No";
}
}
int main()
{
char line[128];
int limit;
cin>>limit;
getchar();
for(int i=0; i<limit; i++)
{
cin.getline(line,sizeof(line));
string str=line;
cout<<verifier(str)<<endl;
}
return 0;
}
-
- Guru
- Posts: 5947
- Joined: Thu Sep 01, 2011 9:09 am
- Location: San Jose, CA, USA
-
- Guru
- Posts: 5947
- Joined: Thu Sep 01, 2011 9:09 am
- Location: San Jose, CA, USA
Re: 673 WA, whats wrong??
Increase the line array to at least size 129.
Check input and AC output for thousands of problems on uDebug!
-
- New poster
- Posts: 11
- Joined: Sun Dec 09, 2012 11:18 pm
Re: 673 WA, whats wrong??
Worked!!!
-
- New poster
- Posts: 1
- Joined: Thu Dec 27, 2012 11:33 am
Re: 673 Parentheses Balance - WA ?
its confusing..from where i could read the input line by line.brianfry713 wrote:check your code with an empty string for an input, you need to read the input line by line.
![8)](./images/smilies/icon_cool.gif)
-
- Guru
- Posts: 5947
- Joined: Thu Sep 01, 2011 9:09 am
- Location: San Jose, CA, USA
Re: 673 Parentheses Balance - WA ?
instead of scanf("%s") use something like gets()
Check input and AC output for thousands of problems on uDebug!
Re: 673 Parentheses Balance - WA ?
WHY WA.........
![:roll:](./images/smilies/icon_rolleyes.gif)
Code: Select all
#include<stdio.h>
#include<string.h>
int main()
{ int te;
scanf("%d",&te);
getchar();
while(te--)
{
char s[200],t;
int i,j,l,fc=0;
gets(s);
l=strlen(s);
for(i=0;i<l-1;i++)
{
if((s[i]=='(' && s[i+1]==']')||(s[i]=='[' && s[i+1]==')'))
{
printf("No\n");
fc=1;
break;
}
}
if(fc==0)
{
for(i=0;i<l;i++)
{
if(s[i]=='(')
t=')';
else if(s[i]=='[')
t=']';
for(j=i;j<l;j++)
{
if(s[j]==t)
{
s[j]='0';
s[i]='0';
t='\0';
break;
}
}
}
int f=0;
for(i=0;i<l;i++)
{
if(s[i]!='0')
{
printf("No\n");
f=1;
break;
}
}
if(f==0)
printf("Yes\n");
f=0;
}
}
return 0;
}
-
- Guru
- Posts: 5947
- Joined: Thu Sep 01, 2011 9:09 am
- Location: San Jose, CA, USA
Re: 673 Parentheses Balance - WA ?
Input:AC output:
Code: Select all
1
([[])]
Code: Select all
No
Check input and AC output for thousands of problems on uDebug!
Re: 673 - Parentheses Balance
online judge says its RUN TIME ERROR .............PLEASE ANYONE HELP ME.............
import java.util.*;
public class Main{
public static void main(String [] args){
Scanner k = new Scanner(System.in);
while(true)
{
int d = k.nextInt();
k.nextLine();
for(int c = 0;c<d;c++)
{
String s = k.nextLine();
s = s.replace(" ","");
Stack<Character> a = new Stack<Character>();
int t;
for(t = 0;t<s.length();t++)
{
if(s.charAt(t)=='('||s.charAt(t)=='{'||s.charAt(t)=='[')
{
a.push(s.charAt(t));
}
else
{
if(s.charAt(t)==')')
{
if(!(a.isEmpty()))
{
if(a.pop()!='(')
{
break;
}
}
else
{
break;
}
}
else if(s.charAt(t)=='}')
{
if(!(a.isEmpty()))
{
if(a.pop()!='{')
{
break;
}
}
else
{
break;
}
}
else
{
if(!(a.isEmpty()))
{
if(a.pop()!='[')
{
break;
}
}
else
{
break;
}
}
}
}
if(a.isEmpty() && t==s.length())
{
System.out.println("Yes");
}
else
{
System.out.println("No");
}
}
}
}
}
![:(](./images/smilies/icon_frown.gif)
import java.util.*;
public class Main{
public static void main(String [] args){
Scanner k = new Scanner(System.in);
while(true)
{
int d = k.nextInt();
k.nextLine();
for(int c = 0;c<d;c++)
{
String s = k.nextLine();
s = s.replace(" ","");
Stack<Character> a = new Stack<Character>();
int t;
for(t = 0;t<s.length();t++)
{
if(s.charAt(t)=='('||s.charAt(t)=='{'||s.charAt(t)=='[')
{
a.push(s.charAt(t));
}
else
{
if(s.charAt(t)==')')
{
if(!(a.isEmpty()))
{
if(a.pop()!='(')
{
break;
}
}
else
{
break;
}
}
else if(s.charAt(t)=='}')
{
if(!(a.isEmpty()))
{
if(a.pop()!='{')
{
break;
}
}
else
{
break;
}
}
else
{
if(!(a.isEmpty()))
{
if(a.pop()!='[')
{
break;
}
}
else
{
break;
}
}
}
}
if(a.isEmpty() && t==s.length())
{
System.out.println("Yes");
}
else
{
System.out.println("No");
}
}
}
}
}
Re: 673 - Parentheses Balance
Have you tried your program against an input file? Calling k.nextInt() throws a java.util.NoSuchElementException exception when the end of the input is reached.raj wrote:online judge says its RUN TIME ERROR .............PLEASE ANYONE HELP ME.............![]()
Try removing the while(true) loop, which is not necessary.