-
Notifications
You must be signed in to change notification settings - Fork 0
/
Continuous manner of 1's in string cout yes or no.txt
101 lines (76 loc) · 2.14 KB
/
Continuous manner of 1's in string cout yes or no.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
Bear Limak has a string S. Each character of S is a digit '0' or '1'.
Help Limak and check if all the '1' digits form a single non-empty segment (consecutive subsequence) in the string. For each test case, print "YES" or "NO" accordingly.
Input
The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows.
The only line of each test case contains one string S, consisting of digits '0' and '1' only.
Output
For each test case, output a single line containing the answer — "YES" if all the '1' digits form a single non-empty segment, and "NO" otherwise. Don't print the quotes.
Constraints
1 ≤ T ≤ 10
1 ≤ |S| ≤ 105 (here, |S| denotes the length of S)
Subtasks
Subtask #1 (50 points): 1 ≤ |S| ≤ 50
Subtask #2 (50 points): Original constraints.
Sample Input 1
6
001111110
00110011
000
1111
101010101
101111111111
Sample Output 1
YES
NO
NO
YES
NO
NO
Explanation
The answer is "YES" for strings 001111110 and 1111.
The answer is "NO" for 00110011 because the '1' digits form two disjoint segments (while they should all be consecutive, with no '0' digits between them).
The answer is "NO" for 000 because the segment formed by the '1' digits must be non-empty (as written in the statement).
Solution:
#include <iostream>
using namespace std;
int main()
{
int t;
cin >> t;
while(t--)
{
string s;
cin >> s;
int i ;
int count = 0 , index = -1;
for(i = 0 ; i < s.size() ; i++)
{
if(s[i] == '1')
{
if(index == -1)
index = i;
count++;
}
}
bool var = true;
if(index != -1)
{
for(int k = index ; k < index+count ; k++)
{
if(s[k] == '0')
{
var = false;
break;
}
}
}
else
{
var = false;
}
if(var)
cout << "YES" << endl;
else
cout << "NO" << endl;
}
}