forked from ngthanhtrung23/CompetitiveProgramming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathK.cpp
More file actions
168 lines (141 loc) · 4.73 KB
/
K.cpp
File metadata and controls
168 lines (141 loc) · 4.73 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#include <bits/stdc++.h>
#define FOR(i,a,b) for(int i=(a),_b=(b); i<=_b; i++)
#define FORD(i,a,b) for(int i=(a),_b=(b); i>=_b; i--)
#define REP(i,a) for(int i=0,_a=(a); i<_a; i++)
#define EACH(it,a) for(__typeof(a.begin()) it = a.begin(); it != a.end(); ++it)
#define DEBUG(x) { cout << #x << " = "; cout << (x) << endl; }
#define PR(a,n) { cout << #a << " = "; FOR(_,1,n) cout << a[_] << ' '; cout << endl; }
#define PR0(a,n) { cout << #a << " = "; REP(_,n) cout << a[_] << ' '; cout << endl; }
#define sqr(x) ((x) * (x))
#define ll long long
#define SZ(X) ((int) ((X).size()))
using namespace std;
int has[300], need[300];
int value[300];
struct Board {
int m, n;
int letter[22][22];
int word[22][22];
char a[22][22];
void read() {
cin >> m >> n;
int nGift; cin >> nGift;
FOR(i,1,m) FOR(j,1,n) {
letter[i][j] = word[i][j] = 1;
}
while (nGift--) {
char typ; cin >> typ;
int mult; cin >> mult;
int k; cin >> k;
while (k--) {
int u, v; cin >> u >> v;
if (typ == 'L') {
letter[u][v] = mult;
}
else {
word[u][v] = mult;
}
}
}
}
void readInit() {
memset(a, '.', sizeof a);
FOR(i,1,m) FOR(j,1,n) cin >> a[i][j];
}
int canPut(string cur) {
// hor
int len = cur.length();
int best = 0;
FOR(i,1,m) {
FOR(j,1,n-len+1) {
// init
FOR(c,'A','Z') need[c] = 0;
// first, see which characters are missing
bool ok = true;
int res = 0;
int mult = 1;
bool atleast = false, atleast2 = false;
REP(x,len) {
if (a[i][j+x] == '.') {
atleast2 = true;
++need[cur[x]];
if (need[cur[x]] > has[cur[x]]) ok = false;
if (a[i-1][j+x] != '.') ok = false;
if (a[i+1][j+x] != '.') ok = false;
}
else {
if (a[i][j+x] != cur[x]) ok = false;
atleast = true;
}
res += value[ cur[x] ] * letter[i][j+x];
mult = mult * word[i][j+x];
}
res *= mult;
ok = ok && atleast && atleast2;
if (a[i][j-1] != '.') ok = false;
if (a[i][j+len] != '.') ok = false;
if (ok) {
best = max(best, res);
}
}
}
// ver
FOR(j,1,n) {
FOR(i,1,m-len+1) {
// init
FOR(c,'A','Z') need[c] = 0;
// first, see which characters are missing
bool ok = true;
int res = 0;
int mult = 1;
bool atleast = false, atleast2 = false;
REP(x,len) {
if (a[i+x][j] == '.') {
atleast2 = true;
++need[cur[x]];
if (need[cur[x]] > has[cur[x]]) ok = false;
if (a[i+x][j-1] != '.') ok = false;
if (a[i+x][j+1] != '.') ok = false;
}
else {
if (a[i+x][j] != cur[x]) ok = false;
atleast = true;
}
res += value[ cur[x] ] * letter[i+x][j];
mult = mult * word[i+x][j];
}
res *= mult;
ok = ok && atleast && atleast2;
if (a[i-1][j] != '.') ok = false;
if (a[i+len][j] != '.') ok = false;
if (ok) {
best = max(best, res);
}
}
}
return best;
}
} board;
int main() {
ios :: sync_with_stdio(false);
freopen("words.in", "r", stdin);
freopen("words.out", "w", stdout);
board.read();
int k; cin >> k;
while (k--) {
string s; cin >> s;
int cost; cin >> cost;
for(char c : s) value[c] = cost;
}
// FOR(c,'A','Z') cout << value[c] << ' '; cout << endl;
board.readInit();
cin >> k; string s; cin >> s;
for(char c : s) ++has[c];
cin >> k;
int res = 0;
while (k--) {
string cur; cin >> cur;
res = max(res, board.canPut(cur));
}
cout << res << endl;
}