UVA Solution for 673 Parentheses Balance - Java

Esta es una propuesta de solución para el ejercicio de UVA 673 Parentheses Balance en el lenguaje de programación Java.

Java codec solution for "673 Parentheses Balance"

Esta es una propuesta de solución sencilla. Aunque su tiempo o time limit no es tan bueno, es suficiente para obtener el accepted.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;

public class Main {


static class Pila  extends ArrayList{

public void agregar(Object elemento) {
if(elemento != null){
this.add(elemento);}
}

/*
* Elimina el primer elemento de la pila.
* @see estructura.Estructura#eliminar()
*/
public Object eliminar() {
Object temp = this.get(0);
if(this.size() > 0){
this.remove(this.size()-1);
}
return temp;
}

public boolean esVacio() {
return this.isEmpty();
}

/*
* Este metodo recupera primer elemento de la pila.
* @see estructura.Estructura#top()
*/

public Object top() {
Object datoAuxiliar = null;
if(this.size() > 0){
datoAuxiliar = this.get(this.size()-1);
}
return datoAuxiliar;
}

}


public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int line = Integer.parseInt(br.readLine());
while (line-- != 0) {
boolean bad = false;
char[] arChars = br.readLine().toCharArray();
Pila pila = new Pila ();
for (char c : arChars) {
if (c == '[' || c == '(') {
pila.agregar(c);
} else {
if (c == ']') {
if (pila.esVacio() || (char)pila.top() == '(')
bad |= true;
} else if (c == ')') {
if (pila.esVacio() || (char)pila.top() == '[')
bad |= true;
}
if (!pila.isEmpty())
pila.eliminar();
}
}
if (!pila.isEmpty())
bad = true;
System.out.println(bad ? "No" : "Yes");
}
}
}
Espero que te sea de gran ayuda. No olvides calificar este articulo.

Recomendado:   VAR EN C# - COMO USAR VAR EN VISUAL C#
Subir