You are not logged in. Please login at www.codechef.com to post your questions!

×

# SEGM01 - Editorial

Author: Kamil Debowski
Tester: Lewin Gan
Editorialist: Misha Chorniy

Cakewalk

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.

This question is marked "community wiki".

asked 29 Apr '17, 18:11

6★mgch
4451436
accept rate: 20%

19.8k350498541

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[1000005]; 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; }

answered 08 Jun '17, 16:41

92
accept rate: 0%

 0 @harshitsaini your code is not printing anything if string doesn't contain any 1's. try this input 1 00000 answered 08 Jun '17, 16:59 1.7k●2●10 accept rate: 14%
 0 @hruday968 Thanx for the bug...got it corrected. answered 08 Jun '17, 17:03 9●2 accept rate: 0% Anytime bro! (08 Jun '17, 17:04)

# 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.

answered 06 Jun '18, 15:43

1★arbi973
1
accept rate: 0%

 toggle preview community wiki:
Preview

### Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

Markdown Basics

• *italic* or _italic_
• **bold** or __bold__
• image?![alt text](/path/img.jpg "title")
• numbered list: 1. Foo 2. Bar
• to add a line break simply add two spaces to where you would like the new line to be.
• basic HTML tags are also supported
• mathemetical formulas in Latex between \$ symbol

Question tags:

×1,688
×82
×43
×8

question asked: 29 Apr '17, 18:11

question was seen: 859 times

last updated: 06 Jun '18, 15:43