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.
Subtask 1
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)
Subtask 2
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).
#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.
Please help me in finding the mistake.
It is only passing first subtask(even though the time complexity is O(n) ). @hruday968@suhas_manju@king_786@mgch@errichto
Thanks for your time.
#include <bits/stdc++.h>
using namespace std;
int main() {
int t=0;
cin>>t;
for(int e=0;e<t;e++){
string s;
cin>>s;
int n=s.size(),flag=0,one=0 ;
for(int i=0;i<n-1;i++){
if(s[i]=='1')one=1;
if(one){
if(s[i]=='0'&&s[i+1]=='1')flag++;
}
}
if(flag||one==0) cout<<"NO\n";
else cout<<"YES\n";
}
}