Mitrezzz

АПС Лаб 4 Модифициран XML код

Nov 19th, 2018
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.51 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.InputStreamReader;
  3. import java.util.NoSuchElementException;
  4. import java.io.BufferedReader;
  5. import java.io.*;
  6.  
  7.  
  8. import java.io.InputStreamReader;
  9.  
  10.  
  11. import java.text.ParseException;
  12.  
  13.  
  14. import java.util.NoSuchElementException;
  15.  
  16.  
  17. interface Stack<E> {
  18.  
  19.  
  20.     public boolean isEmpty ();
  21.    
  22.  
  23.     public E peek ();
  24.        
  25.  
  26.     public void clear ();
  27.        
  28.  
  29.     public void push (E x);
  30.    
  31.  
  32.     public E pop ();
  33.        
  34. }
  35.  
  36.  class ArrayStack<E> implements Stack<E> {
  37.    
  38.  
  39.     private E[] elems;
  40.     private int depth;
  41.  
  42.     @SuppressWarnings("unchecked")
  43.     public ArrayStack (int maxDepth) {
  44.        
  45.         elems = (E[]) new Object[maxDepth];
  46.         depth = 0;
  47.     }
  48.  
  49.  
  50.     public boolean isEmpty () {
  51.        
  52.         return (depth == 0);
  53.     }
  54.  
  55.  
  56.     public E peek () {
  57.      
  58.         if (depth == 0)
  59.             throw new NoSuchElementException();
  60.         return elems[depth-1];
  61.     }
  62.  
  63.  
  64.     public void clear () {
  65.        
  66.         for (int i = 0; i < depth; i++)  elems[i] = null;
  67.         depth = 0;
  68.     }
  69.  
  70.  
  71.     public void push (E x) {
  72.         elems[depth++] = x;
  73.     }
  74.  
  75.  
  76.     public E pop () {
  77.         if (depth == 0)
  78.             throw new NoSuchElementException();
  79.         E topmost = elems[--depth];
  80.         elems[depth] = null;
  81.         return topmost;
  82.     }
  83. }
  84.  
  85.  
  86. public class CheckXML {
  87.    
  88.     public static void main(String[] args) throws Exception{
  89.          
  90.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));  
  91.         String s = br.readLine();
  92.         int n = Integer.parseInt(s);
  93.         String [] redovi = new String[n];
  94.    
  95.         for(int i=0;i<n;i++)
  96.             redovi[i] = br.readLine();
  97.        
  98.         int valid=1;
  99.         ArrayStack<String> nov=new ArrayStack<String>(n);
  100.         String st=new String();
  101.         String novs=new String();
  102.         for(int j=0;j<n;j++){
  103.             int dolzina=redovi[j].length();
  104.             st=redovi[j].substring(0,2);
  105.            
  106.             if(st.equals("[/"))
  107.             {
  108.                 novs=nov.pop();
  109.                 int dolzina1=novs.length();
  110.                 if(!(redovi[j].substring(2,dolzina-1).equals(novs.substring(1,dolzina1-1)))){
  111.                 valid=0;
  112.                     j=n;
  113.                 }
  114.             }
  115.             else{
  116.                 st=redovi[j].substring(0,1);
  117.                 if(st.equals("[")){
  118.                 nov.push(redovi[j]);
  119.                 }
  120.             }
  121.         }
  122.         System.out.println(valid);
  123.        
  124.         br.close();
  125.     }
  126. }
Add Comment
Please, Sign In to add comment