Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.InputStreamReader;
- import java.util.NoSuchElementException;
- class SLLNode<E> {
- protected E element;
- protected SLLNode<E> succ;
- public SLLNode(E elem, SLLNode<E> succ) {
- this.element = elem;
- this.succ = succ;
- }
- @Override
- public String toString() {
- return element.toString();
- }
- }
- interface Stack<E> {
- public boolean isEmpty ();
- public E peek ();
- public void clear ();
- public void push (E x);
- public E pop ();
- }
- class ArrayStack<E> implements Stack<E> {
- private E[] elems;
- private int depth;
- @SuppressWarnings("unchecked")
- public ArrayStack (int maxDepth) {
- elems = (E[]) new Object[maxDepth];
- depth = 0;
- }
- public boolean isEmpty () {
- return (depth == 0);
- }
- public E peek () {
- if (depth == 0)
- throw new NoSuchElementException();
- return elems[depth-1];
- }
- public void clear () {
- for (int i = 0; i < depth; i++) elems[i] = null;
- depth = 0;
- }
- public void push (E x) {
- elems[depth++] = x;
- }
- public E pop () {
- if (depth == 0)
- throw new NoSuchElementException();
- E topmost = elems[--depth];
- elems[depth] = null;
- return topmost;
- }
- }
- public class CheckXML {
- public static void main(String[] args) throws Exception{
- BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
- String s = br.readLine();
- int n = Integer.parseInt(s);
- String [] redovi = new String[n];
- for(int i=0;i<n;i++)
- redovi[i] = br.readLine();
- int valid=-1;
- // Vasiot kod tuka
- // Moze da koristite dopolnitelni funkcii ako vi se potrebni
- ArrayStack <String> stack = new ArrayStack<>(n);
- for(int i=0;i< n;i++){
- if(redovi[i].charAt(0)=='['&&redovi[i].charAt(1)=='/'){
- String pop1= stack.pop();
- String pop2=redovi[i];
- for(int j=1;j<pop1.length();j++){
- if(pop2.length()!=pop1.length()+1){
- valid=0;
- break;
- }
- if(pop1.charAt(j)==pop2.charAt(j+1)){
- valid=1;
- }else{
- valid=0;
- break;
- }
- }
- }else if(redovi[i].charAt(0)=='['){
- stack.push(redovi[i]);
- }
- if(valid==0){
- break;
- }
- }
- System.out.println(valid);
- br.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement