import java.util.*; public class Main{ public static void main(String[] args) { Scanner sc = new Scanner(System.in); String s = sc.nextLine(); StringBuilder sb = new StringBuilder(s); while(sb.indexOf("(") != -1){//只要还有括号,就继续化简 int k = sb.indexOf(")");//右括号下标 int j = k;//左括号下标 StringBuilder sb2 = new StringBuilder();//右括号后面的数 while(sb.charAt(j) != '(') j--;//左括号下标 for(int i = k + 1; i < sb.length(); ++i){ if(sb.charAt(i) >= '0' && sb.charAt(i) <= '9') sb2.append(sb.charAt(i));//右括号后面的数 else break; } String sb3 = f(sb.substring(j + 1, k), sb2.toString());//化简这个括号 sb.replace(j, k + 1 + sb2.length(), sb3);//化简后的内容,替换原内容 } Map<String, Integer> m = g(sb.toString());//统计各原子个数 List<String> v = new ArrayList<>(m.keySet()); Collections.sort(v);//字典序排序 for(String i : v){ System.out.print(i); if(m.get(i) != 1) System.out.print(m.get(i));//单个原子不用写1 } } public static String f(String s1, String s2){//化简括号,以及括号右边的数 StringBuilder r = new StringBuilder(); int n = Integer.valueOf(s2);//括号右边的数(括号里面的数都要乘它) for(int i = 0; i < s1.length(); ++i){ if(s1.charAt(i) >= 'A' && s1.charAt(i) <= 'Z'){ r.append(s1.charAt(i)); i++; while(i < s1.length() && s1.charAt(i) >= 'a' && s1.charAt(i) <= 'z') r.append(s1.charAt(i++)); StringBuilder temp = new StringBuilder(); while(i < s1.length() && s1.charAt(i) >= '0' && s1.charAt(i) <= '9') temp.append(s1.charAt(i++)); if(temp.length() != 0) r.append(Integer.valueOf(temp.toString()) * n); else r.append(n); i--; } } return r.toString(); } public static Map<String, Integer> g(String s){//统计各原子个数 Map<String, Integer> m = new HashMap<>(); for(int i = 0; i < s.length(); ++i){ StringBuilder temp = new StringBuilder(); if(s.charAt(i) >= 'A' && s.charAt(i) <= 'Z'){ temp.append(s.charAt(i)); i++; while(i < s.length() && s.charAt(i) >= 'a' && s.charAt(i) <= 'z') temp.append(s.charAt(i++)); StringBuilder temp2 = new StringBuilder(); while(i < s.length() && s.charAt(i) >= '0' && s.charAt(i) <= '9') temp2.append(s.charAt(i++)); if(m.containsKey(temp.toString())){ int bwt = m.get(temp.toString()); m.put(temp.toString(), bwt + (temp2.length() == 0? 1: Integer.valueOf(temp2.toString()))); }else m.put(temp.toString(), temp2.length() == 0? 1: Integer.valueOf(temp2.toString())); i--; } } return m; } }