-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathOCR.cpp
More file actions
132 lines (107 loc) · 1.79 KB
/
Copy pathOCR.cpp
File metadata and controls
132 lines (107 loc) · 1.79 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
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
int N, Q;
char a[501][11];
double prob_first[501];
double prob_next[501][501];
double prob_trans[501][501];
int M;
char b[101][11];
int idx[101];
int res[101][501];
double dp[101][501];
double solve(int pos, int prev)
{
if(pos == M)
return 0.0;
double &ret = dp[pos][prev];
if(ret != 1.0)
return ret;
ret = -1e200;
for(int i=0; i < N; i++)
{
double prob_now = prob_trans[i][idx[pos]] + prob_next[prev][i];
prob_now += solve(pos+1, i);
if(ret < prob_now)
{
ret = prob_now;
res[pos][prev] = i;
}
}
return ret;
}
void pre_solve()
{
double &ret = dp[0][0];
for(int i=0; i < M; i++)
for(int j=0; j < N; j++)
dp[i][j] = 1.0;
ret = -1e200;
for(int i=0; i < N; i++)
{
double prob_now = prob_trans[i][idx[0]] + prob_first[i] + solve(1, i);
if(ret < prob_now)
{
ret = prob_now;
res[0][0] = i;
}
}
}
void print(int pos, int prev)
{
if(pos == M)
return;
printf("%s ", a[res[pos][prev]]);
print(pos+1, res[pos][prev]);
}
int main()
{
scanf("%d%d",&N,&Q);
for(int i=0; i < N; i++)
scanf("%s",a[i]);
for(int i=0; i < N; i++)
{
scanf("%lf",&prob_first[i]);
prob_first[i] = log(prob_first[i]);
}
for(int i=0; i < N; i++)
{
for(int j=0; j < N; j++)
{
scanf("%lf",&prob_next[i][j]);
prob_next[i][j] = log(prob_next[i][j]);
}
}
for(int i=0; i < N; i++)
{
for(int j=0; j < N; j++)
{
scanf("%lf",&prob_trans[i][j]);
prob_trans[i][j] = log(prob_trans[i][j]);
}
}
while(Q--)
{
scanf("%d",&M);
for(int i=0; i < M; i++)
{
scanf("%s",b[i]);
for(int j=0; j < N; j++)
{
if(strcmp(b[i], a[j]) == 0)
{
idx[i] = j;
break;
}
}
}
pre_solve();
print(0,0);
puts("");
}
return 0;
}