USACO Letter Game

Code

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
/*
ID: wcr19961
PROG: lgame
LANG: C++11
*/
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <string>
using namespace std;

const int maxn = 10;
const int score[] = {
2, 5, 4, 4, 1, 6, 5, 5, 1, 7,
6, 3, 5, 2, 3, 5, 7, 2, 1, 2,
4, 6, 6, 7, 5, 7
};
int ans;
char tmp[maxn];
vector<string> S;
vector<pair<string, string> > anss;

int getScore(string s) {
int res = 0;
for (int i = 0; i < s.length(); ++i) {
res += score[s[i] - 'a'];
}
return res;
}

void proc(string s1, string s2) {
if (binary_search(S.begin(), S.end(), s1) && binary_search(S.begin(), S.end(), s2)) {
int x = getScore(s1) + getScore(s2);
if (x >= ans) {
if (s2 < s1 || s1 == "") {
swap(s1, s2);
}
if (x > ans) {
ans = x;
anss.clear();
}
anss.push_back(make_pair(s1, s2));
}
}
}

int main() {
freopen("lgame.in", "r", stdin);
freopen("lgame.out", "w", stdout);
FILE* Dict = fopen("lgame.dict", "r");
while (~fscanf(Dict, "%s", tmp) && tmp[0] != '.') {
S.push_back(tmp);
}
S.push_back("");
sort(S.begin(), S.end());
scanf("%s", tmp);
string s = tmp;
sort(s.begin(), s.end());
do {
for (int i = 3; i <= s.length(); ++i) {
proc("", s.substr(0, i));
for (int j = 3; i + j <= s.length(); ++j) {
proc(s.substr(0, i), s.substr(i, j));
}
}
} while (next_permutation(s.begin(), s.end()));
printf("%d\n", ans);
sort(anss.begin(), anss.end());
ans = int(unique(anss.begin(), anss.end()) - anss.begin());
for (int i = 0; i < ans; ++i) {
printf("%s", anss[i].first.c_str());
if (anss[i].second != "") {
printf(" %s", anss[i].second.c_str());
}
puts("");
}
return 0;
}