-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHead_Tail_Swap.cpp
78 lines (60 loc) · 1.87 KB
/
Head_Tail_Swap.cpp
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
// Umesh is a Mathematician,
// He is given a task to his student Shanker,
// There are N coins in a row, indexed from 0 to N-1, intially all the coins are
// facing "tail". And Umesh has given him a final State-to-Achieve.
// Shanker can achieve the final state by doing a swap operation as follows:
// - Shanker can choose any index i,
// - all the coins has to be swap their faces, from "head" to "tail"
// or "tail to "head" from index 'i' to 'N-1'.
// Shanker is given a binary string S, State-to-Achieve contains [0,1] only.
// "tail" indicates with '0' and "head" indicates with '1'
// Please help Shanker to find the minimum number of swap operations required
// to reach State-to-Achieve.
// Input Format:
// -------------
// A String S, final State-to-Achieve.
// Output Format:
// --------------
// Print an integer, minimum number of swap operations.
// Sample Input-1:
// ---------------
// 10111010
// Sample Output-1:
// ----------------
// 6
// Explanation:
// ------------
// Initial configuration "00000000".
// swap from the first coin: "00000000" -> "11111111"
// swap from the second coin: "11111111" -> "10000000"
// swap from the third coin: "10000000" -> "10111111"
// swap from the sixth coin: "10111111" -> "10111000"
// swap from the seventh coin: "10111000" -> "10111011"
// swap from the eighth coin: "10111011" -> "10111010"
// A total of 6 swap operations required.
// Sample Input-2:
// ---------------
// 11111
// Sample Output-2:
// ----------------
// 1
#include<bits/stdc++.h>
using namespace std;
int main(){
string s;
cin>>s;
int res=0;
bool state=false;
for(auto i:s){
if((i=='0' && state==true) || (i=='1' && state==false)){
res++;
if(state==true){
state=false;
}
else{
state=true;
}
}
}
cout<<res;
}