Re: 673 - Parentheses Balance
Posted: Fri Feb 15, 2013 6:37 pm
thanks ......... sir............ now its accepted..... 

Code: Select all
//import java.io.File;
//import java.io.FileInputStream;
//import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.Stack;
public class Main {
static final Integer PAREN = new Integer(0);
static final Integer BRACKET = new Integer(1);
public static void main(String[] args) /*throws FileNotFoundException*/ {
// final long startTime = System.currentTimeMillis();
// System.setIn(new FileInputStream(new File("test.txt")));
Scanner sc = new Scanner(System.in);
int n = Integer.parseInt(sc.nextLine().trim());
StringBuilder out = new StringBuilder(n);
while (sc.hasNextLine()) {
String line = sc.nextLine();
if (balance(line)) {
out.append("yes\n");
} else {
out.append("no\n");
}
}
// delete the last '\n'
out.deleteCharAt(out.length() - 1);
System.out.println(out.toString());
//System.out.println(System.currentTimeMillis() - startTime);
}
private static boolean balance(String line) {
char[] chars = line.toCharArray();
Stack<Integer> stack = new Stack<Integer>();
for (char c : chars) {
if (c == '(') {
stack.push(PAREN);
} else if (c == '[') {
stack.push(BRACKET);
} else if (c == ')') {
if (stack.isEmpty())
return false;
if (stack.pop() != PAREN)
return false;
} else if (c == ']') {
if (stack.isEmpty())
return false;
if (stack.pop() != BRACKET)
return false;
}
}
return stack.isEmpty();
}
}
Code: Select all
#include<iostream>
#include<cstdio>
#include<string>
#include<algorithm>
using namespace std;
int main()
{
int T;
string str;
scanf("%d%*c",&T);
while(T--)
{
getline(cin, str);
int i = 0;
while(!str.empty())
{
if(str[i] == ')')
{
i--;
if(str[i] == '(')
{
str.erase(i,2);
continue;
}
else
{
printf("No\n");
break;
}
}
else if(str[i] == ']')
{
i--;
if(str[i] == '[')
{
str.erase(i,2);
continue;
}
else
{
printf("No\n");
break;
}
}
else i++;
}
if(str.empty()) printf("Yes\n");
str.clear();
}
return 0;
}
Code: Select all
1
)
Code: Select all
Tnx... I Got Ac :lol: :lol:
}
Thanks Brianfry! I Got Ac!brianfry713 wrote:You need to read line by line, there may be a blank line and reading a string will skip that.
Code: Select all
#include <stdio.h>
#include <iostream>
#include <math.h>
#include <string.h>
#include <algorithm>
using namespace std;
bool check(char *in)
{
if(strcmp(in,"")==0)
return true;
if(in[0]=='\0')
return true;
else
{
char temp[169];
int i,start,end,j;
start=end=0;
int count;
char ch;
bool f=true;
for(i=0;i<strlen(in);i++)
{
ch=in[i];
start=i;
if(ch==')' || ch==']')
return false;
if(ch=='(')
{
count=1;
for(j=i+1; ;j++)
{
if(j>=strlen(in))
{
return false;
}
if(in[j]==')')
{
count--;
}
else if(in[j]=='(')
{
count++;
}
if(count==0)
{
end=j;
i=j;
break;
}
}
}
else if(ch=='[')
{
count=1;
for(j=i+1; ;j++)
{
if(j>=strlen(in))
{
return false;
}
if(in[j]==']')
{
count--;
}
else if(in[j]=='[')
{
count++;
}
if(count==0)
{
end=j;
i=j;
break;
}
}
}
int k=0;
temp[k]='\0';
int l=0;
for(k=start+1,l;k<end;k++,l++)
{
temp[l]=in[k];
}
temp[l]='\0';
f=f&&check(temp);
}
return f;
}
}
int main()
{
int test;
char input[169];
cin>>test;
while(test--)
{
scanf("%s",&input);
bool k=check(input);
if(k)
cout<<"yes\n";
else
cout<<"no\n";
}
return 0;
}