Advertisement
Guest User

Untitled

a guest
Feb 15th, 2015
306
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.94 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. #define endl            '\n'
  4.  
  5. using namespace std;
  6.  
  7. string rec(string s) {
  8.     if (s.size() == 0) return "";
  9.     int balance = 0;
  10.     vector<string> V;
  11.     string word;
  12.     for (int i = 0; i < (int)s.size(); i++) {
  13.         if (s[i] == '|' && balance == 0) {
  14.             V.push_back(word);
  15.             word = "";
  16.             continue;
  17.         }
  18.         if (s[i] == '(') balance++;
  19.         else if (s[i] == ')') balance--;
  20.         word += s[i];
  21.     }
  22.     if (word.size()) V.push_back(word);
  23.     if (V.size() == 1) {
  24.         string res;
  25.         word = "";
  26.         balance = 0;
  27.         for (int i = 0; i < (int)s.size(); i++) {
  28.             if (s[i] == '(') balance++;
  29.             else if (s[i] == ')') {
  30.                 balance--;
  31.             }
  32.             if (balance == 0) {
  33.                 if (word.size()) res += rec(word);
  34.                 word = "";
  35.                 if (s[i] != '(' && s[i] != ')' && s[i] != '$') res += s[i];
  36.             } else {
  37.                 if (s[i] != '(' || balance != 1) word += s[i];
  38.             }
  39.         }
  40.         return res;
  41.     }
  42.     string res;
  43.     for (int i = 0; i < 555; i++) {
  44.         res += 'z';
  45.     }
  46.     for (int i = 0; i < (int)V.size(); i++) {
  47.         string ss = rec(V[i]);
  48.         if (ss.size() < res.size() || (ss.size() == res.size() && ss < res)) res = ss;
  49.     }
  50.     return res;
  51. }
  52.  
  53. string clean(string s) {
  54.     string res;
  55.     reverse(s.begin(), s.end());
  56.     for (int i = 0; i < (int)s.size(); i++) {
  57.         if (s[i] != '*') {
  58.             res += s[i];
  59.         } else {
  60.             res += '$';
  61.             if (s[i+1] != ')') {
  62.                 i++;
  63.                 continue;
  64.             } else {
  65.                 int balance = 0;
  66.                 for ( i++; i < (int)s.size(); i++) {
  67.                     if (s[i] == '(') balance++;
  68.                     else if (s[i] == ')') balance--;
  69.                     if (balance == 0) break;
  70.                 }
  71.             }
  72.         }
  73.     }
  74.     reverse(res.begin(), res.end());
  75.     return res;
  76. }
  77.  
  78. int main( void ) {
  79.     int T;
  80.     cin >> T;
  81.     for (int tt = 0; tt < T; tt++) {
  82.         string s, ans;
  83.         string ss;
  84.         cin >> s;
  85.         for (int i = 0; i < s.size(); i++) {
  86.             if (ss.size() > 0 && ss[ss.size()-1] == '*' && s[i] == '*') continue;
  87.             ss += s[i];
  88.         }
  89.         s = clean(ss);
  90.         ans = rec(s);
  91.         if (ans.size() == 0) ans = "$";
  92.         cout << ans << endl;
  93.     }
  94.     return 0;
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement