1303A - Erasing Zeroes - CodeForces Solution


implementation strings *800

Please click on ads to support us..

Python Code:

for _ in range(int(input())):
    s = input()
    for k, c in enumerate(s):
        if c == '1':
            break
    ans = t = 0
    for i in range(k + 1, len(s)):
        if s[i] == '0':
            t += 1
        else:
            ans += t
            t = 0
    print(ans)

C++ Code:

#include<bits/stdc++.h>
using namespace std;

int main()
{
    int t,f,cnt;
    cin >> t;
    while(t--)
    {
        string s;
        cin >> s;
        f=cnt=0;
        stack<char>st;
        for(int i=0;i<s.size();i++)
        {
            if(st.empty())
            {
                st.push(s[i]);
                if(s[i]=='1')
                    f=1;
            }
            else
            {
                if(f)
                {
                    while(st.top()<s[i])
                    {
                        st.pop();
                        cnt++;
                    }
                    st.push(s[i]);
                }
                else
                {
                    st.push(s[i]);
                    if(s[i]-'0'==1)
                        f=1;
                }
            }
        }
        cout << cnt << endl;
    }
    return 0;
}


Comments

Submit
0 Comments
More Questions

1353D - Constructing the Array
1269C - Long Beautiful Integer
1076A - Minimizing the String
913C - Party Lemonade
1313A - Fast Food Restaurant
681A - A Good Contest
1585F - Non-equal Neighbours
747A - Display Size
285A - Slightly Decreasing Permutations
515C - Drazil and Factorial
1151E - Number of Components
1151F - Sonya and Informatics
556A - Case of the Zeros and Ones
867A - Between the Offices
1569A - Balanced Substring
260A - Adding Digits
1698C - 3SUM Closure
1029B - Creating the Contest
1421A - XORwice
1029A - Many Equal Substrings
1675D - Vertical Paths
1271C - Shawarma Tent
805A - Fake NP
1163A - Eating Soup
787A - The Monster
807A - Is it rated
1096A - Find Divisible
1430C - Numbers on Whiteboard
1697B - Promo
208D - Prizes Prizes more Prizes