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;
- import java.io.BufferedReader;
- import java.io.*;
- import java.io.InputStreamReader;
- import java.text.ParseException;
- import java.util.NoSuchElementException;
- 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;
- ArrayStack<String> nov=new ArrayStack<String>(n);
- String st=new String();
- String novs=new String();
- for(int j=0;j<n;j++){
- int dolzina=redovi[j].length();
- st=redovi[j].substring(0,2);
- if(st.equals("[/"))
- {
- novs=nov.pop();
- int dolzina1=novs.length();
- if(!(redovi[j].substring(2,dolzina-1).equals(novs.substring(1,dolzina1-1)))){
- valid=0;
- j=n;
- }
- }
- else{
- st=redovi[j].substring(0,1);
- if(st.equals("[")){
- nov.push(redovi[j]);
- }
- }
- }
- System.out.println(valid);
- br.close();
- }
- }
Add Comment
Please, Sign In to add comment