daily pastebin goal
37%
SHARE
TWEET

Bracket expressions (1 loop less)

a-dobrev Feb 4th, 2018 (edited) 73 Never
Upgrade to PRO!
ENDING IN00days00hours00mins00secs
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4.  
  5. public class Main {
  6.  
  7.     public static void main(String[] args) throws IOException {
  8.         BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
  9.         String input = in.readLine();
  10.  
  11.         boolean[] markedAsUsed = new boolean[input.length()];
  12.         int indexOpen = -1;
  13.         int indexClose = -1;
  14. //        int count = 0;
  15.         int count = 1;
  16. //        for (int i = 0; i < input.length(); i++) {
  17. //            if (input.charAt(i) == ')') {
  18. //                count ++;
  19. //            }
  20. //        }
  21.         for (int k = 0; k < count; k++) {
  22.             boolean programDone = false;
  23.             if (k == 0) {
  24.                 count = 0;
  25.             }
  26.             for (int i = 0; i < input.length(); i++) {
  27.                 if (input.charAt(i) == ')' && markedAsUsed[i] == false && programDone == false) {
  28.                     indexClose = i;
  29.                     markedAsUsed[i] = true;
  30.                     programDone = true;
  31.                 }
  32.                 if (input.charAt(i) == ')' && k == 0) {
  33.                     count++;
  34.                 }
  35.             }
  36.             for (int j = 0; j < indexClose; j++) {
  37.                 if (input.charAt(j) == '(' && markedAsUsed[j] == false) {
  38.                 indexOpen = j;
  39.                 }
  40.             }
  41.             markedAsUsed[indexOpen] = true;
  42.             System.out.println(input.substring(indexOpen, indexClose + 1));
  43.         }
  44.     }
  45. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top