# SEGM01 - Editorial

Practice

Contest

Author: Kamil Debowski

Tester: Lewin Gan

Editorialist: Misha Chorniy

Cakewalk

# Pre-Requisites:

None

## Problem Statement

Given a string S. Each character of S is a digit ‘0’ or ‘1’. You need to check if all the ‘1’ digits form a single non-empty segment(consecutive subsequence) in the string.

Length of S in this subtask will be not more than 50. If segment of '1’s exists, it should be consecutive. Let’s iterate over left bound of segment and right bound of segment, and check if only '1’s lies inside this segment, and only '0’s are out of the segment. Complexity of such algorithm is: O(S^3)

Define L - leftmost position of digit ‘1’ in S, R - rightmost position of digit ‘1’ in S. Corner case is: when all characters in S are '0’s, then answer is “NO” as the segment from '1’s is empty. If exists at least one digit ‘1’ in S then all characters between L and R must be equal ‘1’, otherwise subsegment from '1’s is not consecutive. How to check it? Using one simple “if”, if(R-L+1 = C), where C is frequency of '1’s in S. Total complexity of this algorithm is O(S).

## Solution:

Setter’s solution can be found here

Tester’s solution can be found here

Please feel free to post comments if anything is not clear to you.

I cant find error in my code…it gives correct answer for half of the test cases …
https://www.codechef.com/viewsolution/14151559

Here is my solution…

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

int main()
{
int n;scanf("%d",&n);
while(n–)
{
char s;
scanf("%s",s);
bool trig=false,done=false; int i;
for(i=0;i<strlen(s);i++)
{
if(trig){if(s[i]==‘0’){trig=false;}}
else{if(s[i]==‘1’&&done){puts(“NO”);break;}else if(s[i]==‘1’){trig=true;done=true;}}
}
if(i==strlen(s)&&done){puts(“YES”);}
}
return 0;
}

@harshitsaini

your code is not printing anything if string doesn’t contain any 1’s.

try this input

1

00000

@hruday968

Thanx for the bug…got it corrected.

Anytime bro!

#include<stdio.h>
int main(){
int t;
long int s,q,r;
scanf("%d", &t);
while(t–){
scanf("%ld",&s);
if(s==0)
printf(“NO \n”);
else{
q=s;
while(q%10==0)
q/=10;
r=1;
while(r==1){
r=q%10;
q=q/10;
}
if(q!=0)
printf(“NO \n”);
else
printf(“YES \n”);
}
}
return 0;
}

what is wrong with this solution? Even codechef compiler is giving the expected output (as of the test cases mentioned) but on submitting, I receive WA error.