Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- #define LINT long long int
- #define ULINT unsigned long long int
- #define INF 99999999999
- using namespace std;
- template<typename tmp>
- void input(vector<tmp>& vc) {
- for (tmp& x : vc)
- cin >> x;
- }
- template<typename tmp>
- void output(vector<tmp>& vc, string endll = " ", string endd = "\n") {
- for (tmp& x : vc)
- cout << x << endll;
- cout << endd;
- }
- int f(string s) {
- vector<pair<int, int> > vc;
- int c = 0, tmp, index = 1;
- for (int i = 0; i < s.size(); ++i) {
- if (s[i] == '{') {
- c++;
- vc.push_back({i + 1, 1});
- } else
- if (s[i] == '}') {
- c--;
- vc.push_back({i + 1, -1});
- } else
- if (s[i] == '=') {
- tmp = i;
- index++;
- break;
- }
- }
- if (abs(c) > 1)
- return -1;
- if (c != 0) {
- if (c > 0) {
- int sum = 0;
- for (int i = vc.size() - 1; i >= 0; --i) {
- sum += vc[i].second;
- if (sum == c)
- return vc[i].first;
- }
- } else {
- int sum = 0;
- for (auto& x : vc) {
- sum += x.second;
- if (sum == c)
- return x.first;
- }
- }
- }
- vc.clear();
- c = 0;
- for (int i = tmp + 1; i < s.size(); ++i) {
- if (s[i] == '{') {
- c++;
- vc.push_back({i + 1, 1});
- } else
- if (s[i] == '}') {
- c--;
- vc.push_back({i + 1, -1});
- }
- }
- if (abs(c) > 1)
- return -1;
- if (c != 0) {
- if (c > 0) {
- int sum = 0;
- for (int i = vc.size() - 1; i >= 0; --i) {
- sum += vc[i].second;
- if (sum == c)
- return vc[i].first;
- }
- } else {
- int sum = 0;
- for (auto& x : vc) {
- sum += x.second;
- if (sum == c)
- return x.first;
- }
- }
- } else
- return -1;
- }
- int main(){
- string s;
- getline(cin, s);
- cout << f(s);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement