forked from ngthanhtrung23/CompetitiveProgramming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathC.cpp
More file actions
83 lines (74 loc) · 1.8 KB
/
C.cpp
File metadata and controls
83 lines (74 loc) · 1.8 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
#include <bits/stdc++.h>
using namespace std;
int n, m, len, t, nex[222][26], tr[1010][222];
string words[222];
long double f[1010][222], p[1010][26];
int ok(int i, char c, int k)
{
for (int j = 1; j < len; j++)
if (words[k][j - 1] != words[i][j])
return 0;
return words[k][len - 1] == c;
}
int main()
{
ios::sync_with_stdio(0);
freopen("decoding.in", "r", stdin);
freopen("decoding.out", "w", stdout);
cin >> m >> len >> t;
for (int i = 0; i < m; i++)
cin >> words[i];
cin >> n;
for (int i = 0; i < n; i++)
for (int j = 0; j < t; j++)
cin >> p[i][j];
for (int i = 0; i < m; i++)
for (int j = 0; j < t; j++)
{
nex[i][j] = -1;
for (int k = 0; k < m; k++)
if (ok(i, char('a' + j), k))
nex[i][j] = k;
}
for (int i = 0; i < m; i++)
{
f[len - 1][i] = 1;
for (int j = 0; j < len; j++)
f[len - 1][i] *= p[j][words[i][j] - 'a'];
}
for (int i = len; i < n; i++)
for (int j = 0; j < m; j++)
for (int c = 0; c < t; c++)
if (nex[j][c] >= 0 && f[i][nex[j][c]] < f[i - 1][j] * p[i][c])
{
f[i][nex[j][c]] = f[i - 1][j] * p[i][c];
tr[i][nex[j][c]] = j;
}
long double best = 0;
int bestJ = -1;
for (int j = 0; j < m; j++)
if (f[n - 1][j] > best)
{
bestJ = j;
best = f[n - 1][j];
}
if (bestJ < 0) cout << "---\n";
else
{
string ans = "";
for (int i = n - 1; i >= len; i--)
{
int j = tr[i][bestJ];
for (int c = 0; c < t; c++)
if (nex[j][c] == bestJ)
{
ans += char('a' + c);
break;
}
bestJ = j;
}
cout << words[bestJ];
reverse(ans.begin(), ans.end());
cout << ans << endl;
}
}