-
Notifications
You must be signed in to change notification settings - Fork 6
/
chuck_norris.cpp
51 lines (44 loc) · 947 Bytes
/
chuck_norris.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
// Read inputs from stdin. Write outputs to stdout.
#include <iostream>
#include <string>
#include <bitset>
using namespace std;
int main()
{
string text;
getline(cin, text);
// construct the bit string
string bit_string = "";
for(int i=0; i<text.length(); i++) {
char s = text[i];
bitset<7> bs(s);
bit_string += bs.to_string();
}
string out = "";
int is_zero = -1;
string temp = "";
for(string::iterator it = bit_string.begin(); it != bit_string.end(); ++it) {
int new_is_zero = (*it == '0');
if(new_is_zero == is_zero) {
temp += "0";
}
else {
is_zero = new_is_zero;
if(out.length() > 0)
out += " ";
out += temp;
if(is_zero)
temp = "00 0";
else
temp = "0 0";
}
}
// the last char has not been flushed, do it!
if(temp.length() > 0) {
if(out.length() > 0)
out += " ";
out += temp;
}
cout << out << endl;
return 0;
}