Advertisement
Guest User

B

a guest
Nov 28th, 2014
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.87 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. public class Solution {
  5.     public static BufferedReader in;
  6.     public static PrintWriter out;
  7.     public static StringTokenizer st;
  8.  
  9.     public static void main(String[] args) throws IOException {
  10.         in = new BufferedReader(new InputStreamReader(System.in));
  11.         out = new PrintWriter(new OutputStreamWriter(System.out));
  12.         new Solution().solve();
  13.         out.close();
  14.     }
  15.  
  16.     String line;
  17.  
  18.     public void solve() throws IOException {
  19.         line = next();
  20.         int n = line.length();
  21.  
  22.         Map<Integer, Integer> brackets = new HashMap<>();
  23.         Stack<Integer> leftBrackets = new Stack<>();
  24.  
  25.         for (int i = 0; i < n; i++) {
  26.             if (line.charAt(i) == '(') {
  27.                 leftBrackets.add(i);
  28.             }
  29.             if (line.charAt(i) == ')') {
  30.                 int start = leftBrackets.pop();
  31.                 brackets.put(start, i);
  32.             }
  33.         }
  34.  
  35.         System.out.println(process(1, n - 2));
  36.  
  37.     }
  38.  
  39.     String process(int l, int r) {
  40.         StringBuilder number = new StringBuilder();
  41.         while (Character.isDigit(line.charAt(l))) {
  42.             number.append(line.charAt(l));
  43.             ++l;
  44.         }
  45.  
  46.  
  47.         int n = Integer.parseInt(number.toString());
  48.         StringBuilder res = new StringBuilder();
  49.  
  50.         String process = process(l, r);
  51.         for (int i = 0; i < n; i++) {
  52.             res.append(process);
  53.         }
  54.  
  55.         return res.toString();
  56.     }
  57.  
  58.     String next() throws IOException {
  59.         while (st == null || !st.hasMoreTokens()) {
  60.             st = new StringTokenizer(in.readLine());
  61.         }
  62.  
  63.         return st.nextToken();
  64.     }
  65.  
  66.     int nextInt() throws IOException {
  67.         return Integer.parseInt(next());
  68.     }
  69.  
  70.     long nextLong() throws IOException {
  71.         return Long.parseLong(next());
  72.     }
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement