Advertisement
sindi29

ModificiramXML

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