1566B - MIN-MEX Cut - CodeForces Solution


bitmasks constructive algorithms dp greedy *800

Please click on ads to support us..

Python Code:

for i in range(int(input())):
    s = input()
    w = list(s)
    a, b, c = 0, 0, 0

    for i in range(len(s)):
        if s[i] == '0':
            if b > 0:
                c += 1
                break
            a += 1

        else:
            if a > 0:
                b += 1

    if w.count('1') == len(s):
        print(0)
    elif c == 1:
        print(2)
    else:
        print(1)

C++ Code:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;
typedef long long ll;
#define f(i, n) for (ll i = 0; i < n; i++)
#define ff first
#define ss second
#define pb push_back
#define mpair make_pair
typedef pair<ll, ll> pll;
typedef vector<ll> vll;
typedef vector<pll> vpll;
typedef map<ll, ll> mp;
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    indexed_set;
struct custom_hash
{
    static uint64_t splitmix64(uint64_t x)
    {
        x += 0x9e3779b97f4a7c15;
        x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
        x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
        return x ^ (x >> 31);
    }

    size_t operator()(uint64_t x) const
    {
        static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
        return splitmix64(x + FIXED_RANDOM);
    }
};
#define Code ios_base::sync_with_stdio(false);
#define By cin.tie(NULL);
#define Priyanshu cout.tie(NULL);

// __builtin_ffs(x) (Find First Set) which returns (the index of the least significant bits of x) + 1.
// __lg(x) which returns the index of the highest set bit.

void solve()
{
    string s;
    cin >> s ;
    int zc=count(s.begin(),s.end(),'0');
    if(zc==0) 
    {
        cout<<0<<endl;
        return;
    }
    int ffi =s.find('0');
    int lfi= s.rfind('0');
    if(lfi-ffi+1==zc)
    {
        cout<<1<<endl;
        return;
    }
    else
    {
        cout<<2<<endl;
        return;
    }
}
int main()
{
    Code By Priyanshu
        ll t;
    cin >> t;
    while (t--)
    {
        solve();
        
    }
   
    return 0;
}


Comments

Submit
0 Comments
More Questions

1335A - Candies and Two Sisters
96B - Lucky Numbers (easy)
1151B - Dima and a Bad XOR
1435B - A New Technique
1633A - Div 7
268A - Games
1062B - Math
1294C - Product of Three Numbers
749A - Bachgold Problem
1486B - Eastern Exhibition
1363A - Odd Selection
131B - Opposites Attract
490C - Hacking Cypher
158B - Taxi
41C - Email address
1373D - Maximum Sum on Even Positions
1574C - Slay the Dragon
621A - Wet Shark and Odd and Even
1395A - Boboniu Likes to Color Balls
1637C - Andrew and Stones
1334B - Middle Class
260C - Balls and Boxes
1554A - Cherry
11B - Jumping Jack
716A - Crazy Computer
644A - Parliament of Berland
1657C - Bracket Sequence Deletion
1657B - XY Sequence
1009A - Game Shopping
1657A - Integer Moves