Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class Main {
- private static String work(String s) {
- int max = 0, cnt = 0, beg = -1;
- for (int i = 0; i < s.length(); ++i) {
- if (s.charAt(i) == '(') {
- ++cnt;
- } else if (s.charAt(i) == ')') {
- --cnt;
- }
- if (cnt > max) {
- max = cnt;
- }
- }
- cnt = 0;
- for (int i = 0; i < s.length(); ++i) {
- if (s.charAt(i) == '(') {
- beg = i;
- ++cnt;
- }
- if (s.charAt(i) == ')') {
- if (cnt == max) {
- int end = i;
- i = beg - 1;
- s = s.substring(0, beg) + s.substring(end + 1);
- }
- --cnt;
- }
- }
- return s;
- }
- public static void main(String args[]) {
- Scanner in = new Scanner(System.in);
- String input = "", res = "";
- if (in.hasNextLine()) {
- input = in.nextLine();
- } else {
- System.err.print("Нет строки");
- System.exit(1);
- }
- int beg = -1, cnt = 0;
- for (int i = 0; i < input.length(); ++i) {
- if (input.charAt(i) == '(') ++cnt;
- if (input.charAt(i) == ')') --cnt;
- if (input.charAt(i) != '(' && input.charAt(i) != ')' && cnt == 0) {
- res += input.charAt(i);
- } else if (input.charAt(i) == '(' && cnt == 1) {
- beg = i;
- } else if (input.charAt(i) == ')' && cnt == 0) {
- res += work(input.substring(beg, i + 1));
- }
- }
- System.out.println(res);
- in.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment