Advertisement
dmilosavleski

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

Jan 16th, 2018
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.52 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.InputStreamReader;
  3. import java.util.NoSuchElementException;
  4. import java.util.Stack;
  5.  
  6. public class CheckXML {
  7. //vraka 0 za podatok, 1 za zatvoren tag i -1 za otvoren tag
  8. public static int wtype(String s){
  9. if(s.charAt(0) != '[') return 0;
  10. else if(s.charAt(1) == '/') return 1;
  11. else return -1;
  12. }
  13.  
  14. public static void main(String[] args) throws Exception{
  15.  
  16. BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  17. String s = br.readLine();
  18. int n = Integer.parseInt(s);
  19. String [] redovi = new String[n];
  20.  
  21. for(int i=0;i<n;i++)
  22. redovi[i] = br.readLine();
  23.  
  24. int valid = 1;
  25.  
  26. Stack<String> stek = new Stack<>();
  27.  
  28. for(String a : redovi){
  29. if(wtype(a) == 0) continue;
  30. if(wtype(a) == -1)
  31. stek.push(a.substring(1, a.length()-1));
  32. else { //ako stekot e prazen ili ako prviot element od stekot ne e ednakov so zatvoreniot tag vrati 0
  33. if(stek.isEmpty() || !(a.substring(2, a.length()-1).equals(stek.peek())))
  34. {
  35. valid = 0;
  36. break;
  37. }
  38.  
  39. stek.pop();
  40. }
  41. }
  42.  
  43. if(!(stek.isEmpty())) valid = 0;
  44. // Vasiot kod tuka
  45. // Moze da koristite dopolnitelni funkcii ako vi se potrebni
  46.  
  47. System.out.println(valid);
  48.  
  49. br.close();
  50. }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement