Facebook
From Code, 2 Weeks ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 116
  1. #include <iostream>
  2. #include <string>
  3. #include <stack>
  4. #include <algorithm>
  5. using namespace std;
  6. int main()
  7. {
  8.     ios::sync_with_stdio(0);
  9.     cin.tie(0);
  10.     string input;
  11.     cin >> input;
  12.     int par = 0;
  13.     for (int i = 0; i < input.size(); i++) {
  14.         if (input[i] == '(')
  15.             par++;
  16.         else if (input[i] == ')')
  17.             par--;
  18.         else if(par>0 && (par & 1))
  19.         if (islower(input[i]))
  20.             input[i] = toupper(input[i]);
  21.         else
  22.             input[i] = tolower(input[i]);
  23.     }
  24.     stack<string> ts;
  25.     ts.push("");
  26.     for (int i = 0; i < input.size(); i++) {
  27.         if (input[i] == '(') {
  28.             ts.push("");
  29.         }
  30.         else if (input[i] == ')') {
  31.             reverse(ts.top().begin(),ts.top().end());
  32.             string reversed = ts.top();
  33.             ts.pop();
  34.             string& topElement = ts.top();
  35.             topElement.append(reversed);
  36.         }
  37.         else {
  38.             string& topElement = ts.top();
  39.             topElement.push_back(input[i]);
  40.         }
  41.     }
  42.     cout << ts.top();
  43. }