Advertisement
BorjanCrvenkov

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

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