publicclass{ publicstaticvoidmain(String[] args){ Scanner scanner = new Scanner(System.in); String line = scanner.nextLine(); int len = line.length(); LinkedList<String> stack1 = new LinkedList<>();
//输出gfgfgfedgfgfgfedcba for (int i = 0; i < len; i++) { String temp = line.charAt(i) + ""; if (")]}".contains(temp)) {
//得到上一个(括号之前的字母 StringBuilder dup = new StringBuilder(); while (!"({[".contains(stack1.getFirst())) { dup.insert(0,stack1.removeFirst()); } stack1.removeFirst();
//注意此时的坑,因为数字可能是多位数,因此需要得到所有的数字 StringBuilder numStr = new StringBuilder(); while (!stack1.isEmpty() && stack1.getFirst().matches("[0-9]")) { numStr.insert(0, stack1.removeFirst()); } int num = Integer.parseInt(numStr.toString());
//按照(前的数字进行重复,并且再次压入栈 StringBuilder str = new StringBuilder(); while (num > 0) { str.append(dup); --num; } for (int k = 0; k < str.length(); k++) { stack1.addFirst(str.charAt(k) + ""); } } else { stack1.addFirst(temp); } } StringBuilder result = new StringBuilder(); while (!stack1.isEmpty()) { result.append(stack1.removeFirst()); } System.out.println(result.toString()); } }