-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1039.cpp
81 lines (73 loc) · 1.25 KB
/
1039.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
79
80
81
/*
这道题教给我一个严重的教训:不要想当然!不要想太多!我居然天真的以为一定要按顺序添加是最优。
其实就是保持头脑清楚。这实在太难了。
AAABA -> ABAABA
*/
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int find(string& s)
{
int ans = 0;
string t1(s), t2;
string::size_type i = 0, len = s.size();
if (len < 2) {
return 0;
}
for (i = 1; i < len-1; ++i) {
if (s[i] == s[i-1] || s[i] == s[i+1]) {
t1[i] = '.';
++ans;
}
}
if (s[0] == s[1]) {
t1[0] = '.';
++ans;
}
if (s[len-1] == s[len-2]) {
t1[len-1] = '.';
++ans;
}
for (i = 0; i < len; ++i) {
if (t1[i] != '.') {
t2 += t1[i];
}
}
if (t2 == s) {
return 0;
} else {
return ans + find(t2);
}
}
int simple_right()
{
int n;
cin >> n;
for (int i=0; i<n; ++i){
int ans = 0;
string s;
cin >> s;
for (string::size_type i=0; i < s.size(); ++i){
string t;
int temp;
t = s;
t.insert(i, "A");
if ((temp = find(t)) > ans) {
ans = temp;
}
t = s;
t.insert(i, "B");
if ((temp = find(t)) > ans) {
ans = temp;
}
t = s;
t.insert(i, "C");
if ((temp = find(t)) > ans) {
ans = temp;
}
}
cout << ans << endl;
}
return 0;
}