-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpancakes.cpp
More file actions
82 lines (70 loc) · 1.48 KB
/
Copy pathpancakes.cpp
File metadata and controls
82 lines (70 loc) · 1.48 KB
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
#include <iostream>
using namespace std;
// Google Code Jam 2016: Qualification Round - Revenge of the Pancakes
string flip(string s) {
for (size_t i = 0; i < s.length(); i++) {
if (s[i] == '+') {
s[i] = '-';
}
else {
s[i] = '+';
}
}
return s;
}
int checkHappyBottom(string s) {
int index;
index = s.length()-1;
for (std::string::reverse_iterator rit=s.rbegin(); rit!=s.rend(); rit++) {
if (s[s.length()-1] == '-') {
index = s.length()-1;
break;
}
if (*rit == '+') {
if (s[index] == s[index - 1]) {
}
else {
break;
}
}
index--;
}
return index;
}
int numberOfFlips(string& s) {
string sub;
int count = 0;
if (s.length() == 1) {
if (s[0] == '-') {
return 1;
}
else {
return 0;
}
}
while(checkHappyBottom(s) != 0) {
if (s[s.length()-1] == '-') {
sub = s;
s = flip(sub);
}
else {
sub = s.substr(0,checkHappyBottom(s));
s = flip(sub);
}
count++;
}
return count;
}
int main()
{
int i = 1;
int testCases;
string input;
cin >> testCases;
while (i <= testCases && cin >> input) {
cout << "Case #" << i << ": ";
cout << numberOfFlips(input) << endl;
i++;
}
return 0;
}