1499B - Binary Removals - CodeForces Solution


brute force dp greedy implementation *1000

Please click on ads to support us..

Python Code:

for _ in range(int(input())):
    s = input()
    zero = 0
    one = len(s)
    for i in range(len(s)-1):
        if s[i]==s[i+1] and s[i]=="1":
            one = i+1
            break
    for i in range(1,len(s)):
        if s[-i]==s[-(i+1)] and s[-i]=="0":
            zero = len(s)-i
            break
    if zero>one:
        print("No")
    else:
        print("Yes")

C++ Code:

//code here

#include <iostream>
#include <string>

using ll = long long;

void solve() {
    std::string s;
    std::cin >> s;
    int one = 0;
    int zero = 0;
    bool flag = 0;
    for (int i = 0; i < s.length(); i++) {
        if (s[i] == '1') {
            if (!flag) {
                if (zero) one = 1;
                else one++;
            }
            else {
                one++;
            }
            zero = 0;
        }
        else {
            zero++;
            if (one >= 2) {
                if (zero >= 2) {
                    puts("No");
                    return;
                }
                else {
                    flag = 1;
                }
            }
        }
    }
    puts("Yes");
}

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);

    int T;
    std::cin>>T;

    while (T--) {
        solve();
    }

    return 0;
}

/**
  * @file    :b.cpp
  * @brief   :contest
  * @author  :fakesheep
  * @version :V1.0.0
  * @date    :2023-03-16
  */


Comments

Submit
0 Comments
More Questions

1699B - Almost Ternary Matrix
1545A - AquaMoon and Strange Sort
538B - Quasi Binary
424A - Squats
1703A - YES or YES
494A - Treasure
48B - Land Lot
835A - Key races
1622C - Set or Decrease
1682A - Palindromic Indices
903C - Boxes Packing
887A - Div 64
755B - PolandBall and Game
808B - Average Sleep Time
1515E - Phoenix and Computers
1552B - Running for Gold
994A - Fingerprints
1221C - Perfect Team
1709C - Recover an RBS
378A - Playing with Dice
248B - Chilly Willy
1709B - Also Try Minecraft
1418A - Buying Torches
131C - The World is a Theatre
1696A - NIT orz
1178D - Prime Graph
1711D - Rain
534A - Exam
1472A - Cards for Friends
315A - Sereja and Bottles