fork download
  1. import java.util.*;
  2. import java.util.regex.*;
  3.  
  4. class Ideone
  5. {
  6. static Pattern pattern = Pattern.compile("[a-zA-Z0-9]{0,100}<(/?)([ab])>");
  7.  
  8. static void test(String str)
  9. {
  10. System.out.println(str);
  11. LinkedList<String> elements = new LinkedList<String>();
  12. elements.add("");
  13.  
  14. Matcher matcher = pattern.matcher(str);
  15. while (matcher.find())
  16. {
  17. if ("".equals(matcher.group(1)))
  18. {
  19. switch (elements.getLast())
  20. {
  21. case "a": if ("b".equals(matcher.group(2))) break;
  22. case "b": System.out.println(0); return;
  23. }
  24. elements.add(matcher.group(2));
  25. }
  26. else if (!elements.removeLast().equals(matcher.group(2)))
  27. {
  28. System.out.println(0);
  29. return;
  30. }
  31. }
  32. System.out.println(elements.size() == 1 ? 1 : 0);
  33. }
  34.  
  35. public static void main(String[] args)
  36. {
  37. test("<a>ruby<b>php</b>python</a><a><b></b></a><a></a>");
  38.  
  39. test("<a>ruby<b>php</b>python</a><a></b></a><a></a>");
  40. test("<a>ruby<b>php</b>python</a><a><b></a><a></a>");
  41. test("<a>ruby<b>php</b>python</a><a><b></b></a><a>");
  42. test("vvvc<a>ruby<b>php</b>python</a><a><b></b></a><a></a>");
  43. }
  44. }
Success #stdin #stdout 0.11s 320256KB
stdin
Standard input is empty
stdout
<a>ruby<b>php</b>python</a><a><b></b></a><a></a>
1
<a>ruby<b>php</b>python</a><a></b></a><a></a>
0
<a>ruby<b>php</b>python</a><a><b></a><a></a>
0
<a>ruby<b>php</b>python</a><a><b></b></a><a>
0
vvvc<a>ruby<b>php</b>python</a><a><b></b></a><a></a>
1