1398B - Substring Removal Game - CodeForces Solution


games greedy sortings *800

Please click on ads to support us..

Python Code:

for _ in ' '*int(input()): print(sum(map(len, sorted(input().split('0'))[::-2])))

  	 	     	 	  			  		 				 	 	

C++ Code:

#include<stdio.h>
#include<string.h>
int main()
{
    int n;
    scanf("%d",&n);
    int i,j,k;
    while(n--)
    {
        char a[101];
        j=0;
        int b[100]={0};
        scanf(" %s",a);
        int b1=strlen(a);
        int o=0,f=0;
        i=0;
        while(a[i]!='1')
        {
            i++;
            if(i==b1)
            {
                f=1;
                break;
            }
        }
        if(f==1)
        {
            printf("0\n");
            continue;
        }
        for(;i<b1;i++)
        {
            if(a[i]=='1')
            {
                b[j]++;
                o++;
            }
            else if( a[i]=='0' && a[i+1]=='1')
            {
                j++;
            }
        }
        b[j+1]='\0';
        int s=j+1;
        // printf("%d\n",s);
        for(i=0;i<s;i++)
        {
            for(j=0;j<s-1;j++)
            {
                if(b[j]<b[j+1])
                {
                    int temp=b[j];
                    b[j]=b[j+1];
                    b[j+1]=temp;
                }
            }
        }
        int a1=0;
        for(i=0;i<s;i=i+2)
        {
            a1=a1+b[i];
        }

        printf("%d\n",a1);
    }
}


Comments

Submit
0 Comments
More Questions

Number of triangles
AND path in a binary tree
Factorial equations
Removal of vertices
Happy segments
Cyclic shifts
Zoos
Build a graph
Almost correct bracket sequence
Count of integers
Differences of the permutations
Doctor's Secret
Back to School
I am Easy
Teddy and Tweety
Partitioning binary strings
Special sets
Smallest chosen word
Going to office
Color the boxes
Missing numbers
Maximum sum
13 Reasons Why
Friend's Relationship
Health of a person
Divisibility
A. Movement
Numbers in a matrix
Sequences
Split houses