-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaoj0179.cpp
More file actions
79 lines (78 loc) · 1.71 KB
/
aoj0179.cpp
File metadata and controls
79 lines (78 loc) · 1.71 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
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <algorithm>
#include <numeric>
#include <functional>
#include <cmath>
#include <cstring>
#include <cctype>
#include <sstream>
#include <set>
#include <map>
#include <queue>
#include <complex>
using namespace std;
int main() {
string musi;
while(cin >> musi, musi != "0") {
string tonum;
for(int i=0; i<musi.size(); i++) {
if(musi[i] == 'r')
tonum.push_back('1');
if(musi[i] == 'g')
tonum.push_back('2');
if(musi[i] == 'b')
tonum.push_back('3');
}
queue< pair<string, int> > q;
pair<string, int> p;
p.first = tonum;
p.second = 0;
q.push(p);
bool flg = true;
set<string> memo;
int test = 0;
while(q.size()) {
p = q.front();
q.pop();
int cnt = 0;
for(int i=1; i<p.first.size(); i++) {
if(p.first[i-1] != p.first[i]) {
pair<string, int> p2;
p2 = p;
int n = (int)p.first[i-1] + (int)p.first[i] - '0' - '0';
if(n==3) {
p2.first[i-1] = '3';
p2.first[i] = '3';
}
else if(n==4) {
p2.first[i-1] = '2';
p2.first[i] = '2';
}
else if(n==5) {
p2.first[i-1] = '1';
p2.first[i] = '1';
}
if(memo.find(p2.first) == memo.end()) {
p2.second++;
q.push(p2);
memo.insert(p2.first);
}
}
else cnt++;
}
if(cnt == p.first.size()-1) {
flg = false;
cout << p.second << endl;
break;
}
}
if(flg) cout << "NA" << endl;
}
return 0;
}