Crazy

Модифициран XML код

Nov 4th, 2017
542
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.98 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.InputStreamReader;
  3. import java.util.NoSuchElementException;
  4.  
  5. interface Stack<E> {
  6.  
  7.     // Elementi na stekot se objekti od proizvolen tip.
  8.  
  9.     // Metodi za pristap:
  10.  
  11.     public boolean isEmpty();
  12.     // Vrakja true ako i samo ako stekot e prazen.
  13.  
  14.     public E peek();
  15.     // Go vrakja elementot na vrvot od stekot.
  16.  
  17.     // Metodi za transformacija:
  18.  
  19.     public void clear();
  20.     // Go prazni stekot.
  21.  
  22.     public void push(E x);
  23.     // Go dodava x na vrvot na stekot.
  24.  
  25.     public E pop();
  26.     // Go otstranuva i vrakja elementot shto e na vrvot na stekot.
  27. }
  28.  
  29. class ArrayStack<E> implements Stack<E> {
  30.  
  31.     // Stekot e pretstaven na sledniot nacin:
  32.     //depth e dlabochinata na stekot, a
  33.     // elems[0...depth-1] se negovite elementi.
  34.     private E[] elems;
  35.     private int depth;
  36.  
  37.     @SuppressWarnings("unchecked")
  38.     public ArrayStack(int maxDepth) {
  39.         // Konstrukcija na nov, prazen stek.
  40.         elems = (E[]) new Object[maxDepth];
  41.         depth = 0;
  42.     }
  43.  
  44.  
  45.     public boolean isEmpty() {
  46.         // Vrakja true ako i samo ako stekot e prazen.
  47.         return (depth == 0);
  48.     }
  49.  
  50.  
  51.     public E peek() {
  52.         // Go vrakja elementot na vrvot od stekot.
  53.         if (depth == 0)
  54.             throw new NoSuchElementException();
  55.         return elems[depth - 1];
  56.     }
  57.  
  58.  
  59.     public void clear() {
  60.         // Go prazni stekot.
  61.         for (int i = 0; i < depth; i++) elems[i] = null;
  62.         depth = 0;
  63.     }
  64.  
  65.  
  66.     public void push(E x) {
  67.         // Go dodava x na vrvot na stekot.
  68.         elems[depth++] = x;
  69.     }
  70.  
  71.  
  72.     public E pop() {
  73.         // Go otstranuva i vrakja elementot shto e na vrvot na stekot.
  74.         if (depth == 0)
  75.             throw new NoSuchElementException();
  76.         E topmost = elems[--depth];
  77.         elems[depth] = null;
  78.         return topmost;
  79.     }
  80. }
  81.  
  82.  
  83. public class CheckXML {
  84.  
  85.     public static void main(String[] args) throws Exception {
  86.  
  87.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  88.         String s = br.readLine();
  89.         int n = Integer.parseInt(s);
  90.         String[] redovi = new String[n];
  91.  
  92.         for (int i = 0; i < n; i++)
  93.             redovi[i] = br.readLine();
  94.  
  95.         int valid;
  96.  
  97.         // Vasiot kod tuka
  98.         // Moze da koristite dopolnitelni funkcii ako vi se potrebni
  99.         ArrayStack<String> stek = new ArrayStack<String>(n);
  100.         for (int i = 0; i < n; i++) {
  101.             if (redovi[i].charAt(0) == '['&&redovi[i].charAt(1) != '/')
  102.                 stek.push(redovi[i]);
  103.             else if (redovi[i].charAt(0) == '['&&redovi[i].charAt(1) == '/')
  104.                 if (redovi[i].substring(2, redovi[i].length()).compareTo(stek.peek().substring(1, stek.peek().length())) == 0) {
  105.                     stek.pop();
  106.                 } else break;
  107.         }
  108.         if (stek.isEmpty())
  109.             valid = 1;
  110.         else valid = 0;
  111.  
  112.         System.out.println(valid);
  113.  
  114.         br.close();
  115.     }
  116. }
Advertisement
Add Comment
Please, Sign In to add comment