Facing problem while reading string as an input in 1494A — ABC String

Revision en1, by klokov__vokolk, 2021-11-20 13:38:58
#include<bits/stdc++.h>
 
using namespace std;
 
 
 
#define fastio() ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define MX 1e6
#define MOD 1000000007
#define MOD1 998244353
#define INF 1e18
#define nline "\n"
#define pb push_back
#define ppb pop_back
#define mp make_pair
#define ff first
#define ss second
#define PI 3.141592653589793238462
#define set_bits __builtin_popcountll
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
 
typedef long long ll;
typedef unsigned long long ull;
typedef long double lld;
// typedef tree<pair<int, int>, null_type, less<pair<int, int>>, rb_tree_tag, tree_order_statistics_node_update > pbds; // find_by_order, order_of_key
 
#ifndef ONLINE_JUDGE
#define debug(x) cerr << #x <<" "; _print(x); cerr << endl;
#else
#define debug(x)
#endif
 
void _print(ll t) {cerr << t;}
void _print(int t) {cerr << t;}
void _print(string t) {cerr << t;}
void _print(char t) {cerr << t;}
void _print(lld t) {cerr << t;}
void _print(double t) {cerr << t;}
void _print(ull t) {cerr << t;}
 
template <class T, class V> void _print(pair <T, V> p);
template <class T> void _print(vector <T> v);
template <class T> void _print(set <T> v);
template <class T, class V> void _print(map <T, V> v);
template <class T> void _print(multiset <T> v);
template <class T, class V> void _print(pair <T, V> p) {cerr << "{"; _print(p.ff); cerr << ","; _print(p.ss); cerr << "}";}
template <class T> void _print(vector <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T> void _print(set <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T> void _print(multiset <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T, class V> void _print(map <T, V> v) {cerr << "[ "; for (auto i : v) {_print(i); cerr << " ";} cerr << "]";}
 
bool comp1(pair<int, int> a, pair<int, int> b){ return a.ff < b.ff;}
bool comp2(pair<int, int> a, pair<int, int> b){ return a.ss < b.ss;}

bool isValidSequence (string str) {
    stack<char> s;

    for (int i = 0; i < str.size(); i++) {
        if (str[i] == '(') {
            s.push('(');
        } else if (str[i] == ')' && s.top() == '(') {
            s.pop();
        } else if (str[i] == ')' && s.top() == ')') {
            s.push(')');
        }
    }

    if (s.size()) {
        return false;
    }

    return true;
}

void solve() {
    string str;
    // this is point where i am facing the problem and not able to figure out what am i doing wrong.
    cin>>str;

    int n = str.length();

    char first = str[0];
    char last = str[n - 1];

    if (first == last) {
        cout<<"NO"<<"\n";
        return;
    }

    vector<string> vec(2);

    for (int i = 0; i < n; i++) {
        if (str[i] == first) {
            vec[0].pb('(');
        } else if (str[i] == last) {
            vec[0].pb(')');
        } else {
            vec[0].pb('(');
        }
    }

    for (int i = 0; i < n; i++) {
        if (str[i] == first) {
            vec[1].pb('(');
        } else if (str[i] == last) {
            vec[1].pb(')');
        } else {
            vec[1].pb(')');
        }
    }

    for (auto var : vec) {
        if (isValidSequence(var)) {
            cout<<"YES"<<"\n";
            return;
        }
    }

    cout<<"NO"<<"\n";
    return;
}

int main() {
 
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("error.txt", "w", stderr);
    freopen("output.txt", "w", stdout);
#endif 

    int t;
    cin>>t;

    while (t--) {
        solve();
    }
    return 0;
}

I don't know what's wrong is going on but for input t = 1 string s = ABBB I am not able to read the string and tried a lot to figure out the error but couldn't solve it please help

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en1 English klokov__vokolk 2021-11-20 13:38:58 4020 Initial revision (published)