public static void main(String[] args) { Scanner sc = new Scanner(System.in); while(sc.hasNext()) { String str = sc.next(); Deque<Character> stackCh = new ArrayDeque<>();//存 '<' Deque<StringBuilder> stackSB = new ArrayDeque<>();//存<>里面的字符串 int len = str.length(); int cur = 0; while (cur < len) { if (str.charAt(cur) == '<') { stackCh.push('<'); if (str.charAt(cur + 1) == '/') { if (stackSB.isEmpty()) { break; } cur += 2; StringBuilder sec = stackSB.poll(); int sbLen = sec.length(); String tmpStr = str.substring(cur, cur + sbLen); if (judgeStr(sec.toString(), tmpStr)) { cur += sbLen; if (str.charAt(cur) == '>') { cur++; continue; } else { break; } } else { break; } } else { cur++; char ch = str.charAt(cur); StringBuilder sb = new StringBuilder(); while (cur < len &;&; ((ch >= 'A' &;&; ch <= 'Z') || (ch >= 'a' &;&; ch <= 'z'))) { sb.append(ch); cur++; ch = str.charAt(cur); } if (sb.isEmpty()) break; stackSB.push(sb); } } else if (str.charAt(cur) == '>') { if (stackCh.isEmpty()) { break; } stackCh.poll(); cur++; } else { break; } } if (cur == len) { if(!stackSB.isEmpty()) { System.out.println("NO"); } else { System.out.println("YES"); } } else { System.out.println("NO"); } } } private static boolean judgeStr(String sec, String s) { int len1 = sec.length(), len2 = s.length(); if(len1 != len2) return false; for(int i = 0;i < len1;i++) { if(sec.charAt(i) != s.charAt(i)) { return false; } } return true; }
点赞 评论

相关推荐

02-14 16:34
门头沟学院 Java
点赞 评论 收藏
分享
牛客网
牛客网在线编程
牛客网题解
牛客企业服务