1861C - Queries for the Array - CodeForces Solution


data structures dfs and similar strings trees

Please click on ads to support us..

C++ Code:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const ll MOD = 998244353;
void solve() {
    string s; cin >> s;
    int sorted = 0, len = 0, state = 1, pos = 0,nxt=0;
    // for(;pos<s.size();pos++) {
    //     if(s[pos]=='+' || s[pos]=='-') break;
    //     if(s[pos]=='1') continue;
    //     if(s[pos]=='0') {
    //         cout<<"NO"<<endl;
    //         return;
    //     }
    // }
    // for (;pos < s.size();pos++) {
    //     if (s[pos] == '+') {
    //         len++;
    //     } else if (s[pos] == '-') {
    //         len--;
    //     } else if (s[pos] == '0') {
    //         state=0;
    //         break;
    //     } else {
    //         state=1;
    //         sorted=len;
    //         break;
    //     }
    // }
    // pos++;
    for (;pos < s.size();pos++) {
        if (s[pos] == '+') {
            len++;
            if (len < 2) {
                sorted = len;
                // state=1;
            }
            if(state) nxt++;
        } else if (s[pos] == '-') {
            if (len == nxt) state = 1;
            len--;
            sorted = min(sorted, len);
            if(state) nxt=min(nxt,len);    
        } else if (s[pos] == '0') {
            if (sorted == len) {
                cout << "NO" << endl;
                return;
            }
            state = 0;
            nxt=min(nxt,len);
        } else {
            if (len>=nxt && !state) {
                // cerr<<nxt<<endl;
                cout << "NO" << endl;
                return;
            }
            state = 1;
            sorted = len;
            nxt=sorted;
        }
    }
    // cerr<<sorted<<" "<<len<<endl;
    cout << "YES" << endl;
}
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t = 1;
    cin >> t;
    while (t--) solve();
    return 0;
}


Comments

Submit
0 Comments
More Questions

1635A - Min Or Sum
474A - Keyboard
1343A - Candies
1343C - Alternating Subsequence
1325A - EhAb AnD gCd
746A - Compote
318A - Even Odds
550B - Preparing Olympiad
939B - Hamster Farm
732A - Buy a Shovel
1220C - Substring Game in the Lesson
452A - Eevee
1647B - Madoka and the Elegant Gift
1408A - Circle Coloring
766B - Mahmoud and a Triangle
1618C - Paint the Array
469A - I Wanna Be the Guy
1294A - Collecting Coins
1227A - Math Problem
349A - Cinema Line
47A - Triangular numbers
1516B - AGAGA XOOORRR
1515A - Phoenix and Gold
1515B - Phoenix and Puzzle
155A - I_love_username
49A - Sleuth
1541A - Pretty Permutations
1632C - Strange Test
673A - Bear and Game
276A - Lunch Rush