Advertisement
CyberN00b

Untitled

Sep 7th, 2022
734
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.26 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. #define LINT long long int
  3. #define ULINT unsigned long long int
  4. #define INF 99999999999
  5.  
  6. using namespace std;
  7.  
  8. template<typename tmp>
  9. void input(vector<tmp>& vc) {
  10.     for (tmp& x : vc)
  11.         cin >> x;
  12. }
  13. template<typename tmp>
  14. void output(vector<tmp>& vc, string endll = " ", string endd = "\n") {
  15.     for (tmp& x : vc)
  16.         cout << x << endll;
  17.     cout << endd;
  18. }
  19. int f(string s) {
  20.     vector<pair<int, int> > vc;
  21.     int c = 0, tmp, index = 1;
  22.     for (int i = 0; i < s.size(); ++i) {
  23.         if (s[i] == '{') {
  24.             c++;
  25.             vc.push_back({i + 1, 1});
  26.         } else
  27.             if (s[i] == '}') {
  28.                 c--;
  29.                 vc.push_back({i + 1, -1});
  30.             } else
  31.                 if (s[i] == '=') {
  32.                     tmp = i;
  33.                     index++;
  34.                     break;
  35.                 }
  36.     }
  37.  
  38.     if (abs(c) > 1)
  39.         return -1;
  40.     if (c != 0) {
  41.         if (c > 0) {
  42.             int sum = 0;
  43.             for (int i = vc.size() - 1; i >= 0; --i) {
  44.                 sum += vc[i].second;
  45.                 if (sum == c)
  46.                     return vc[i].first;
  47.             }
  48.         } else {
  49.             int sum = 0;
  50.             for (auto& x : vc) {
  51.                 sum += x.second;
  52.                 if (sum == c)
  53.                     return x.first;
  54.             }
  55.         }
  56.  
  57.     }
  58.     vc.clear();
  59.     c = 0;
  60.     for (int i = tmp + 1; i < s.size(); ++i) {
  61.         if (s[i] == '{') {
  62.             c++;
  63.             vc.push_back({i + 1, 1});
  64.         } else
  65.             if (s[i] == '}') {
  66.                 c--;
  67.                 vc.push_back({i + 1, -1});
  68.             }
  69.     }
  70.  
  71.     if (abs(c) > 1)
  72.         return -1;
  73.     if (c != 0) {
  74.         if (c > 0) {
  75.             int sum = 0;
  76.             for (int i = vc.size() - 1; i >= 0; --i) {
  77.                 sum += vc[i].second;
  78.                 if (sum == c)
  79.                     return vc[i].first;
  80.             }
  81.         } else {
  82.             int sum = 0;
  83.             for (auto& x : vc) {
  84.                 sum += x.second;
  85.                 if (sum == c)
  86.                     return x.first;
  87.             }
  88.         }
  89.     } else
  90.     return -1;
  91.  
  92. }
  93.  
  94. int main(){
  95.     string s;
  96.     getline(cin, s);
  97.     cout << f(s);
  98. }
  99.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement