This repository was archived by the owner on Jan 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathstitch_torn_wiki.cpp
More file actions
147 lines (109 loc) · 3.01 KB
/
stitch_torn_wiki.cpp
File metadata and controls
147 lines (109 loc) · 3.01 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
// https://www.hackerrank.com/challenges/stitch-the-torn-wiki
#include <iostream>
#include <cstdio>
#include <vector>
using namespace std;
int ans[200000], a[100001][26], lst, n;
char s[200000];
vector<int> q[100001];
double v[1000], e[300][300];
void add(int k, char *s, int i) {
int ch = *s;
if (ch >= 'A' && ch <= 'Z') ch -='A'-'a';
if (ch >= 'a' && ch <= 'z') {
if (!a[k][ch-'a'])
a[k][ch-'a'] = ++lst;
add(a[k][ch-'a'], s+1, i);
} else {
if (k) q[k].push_back(i);
if (*s) add(0, s + 1, i);
return;
}
}
void eval(int k, char *s, int r) {
int ch = *s;
if (ch >= 'A' && ch <= 'Z') ch -='A'-'a';
if (ch >= 'a' && ch <= 'z') {
if (!a[k][ch-'a'])
a[k][ch-'a'] = ++lst;
eval(a[k][ch-'a'], s+1, r);
} else {
if (k) {
double tot = 0;
for (int i = 0; i < n; i++) v[i] = 0;
for (int i = 0; i < q[k].size(); i++) {
v[q[k][i]]++;
tot += 1.0;
}
if (q[k].size())
for (int i = 0; i < n; i++)
e[i+1][r+1] += v[i]/tot;
}
if (*s) eval(0, s+1, r);
}
}
void hungarian() {
vector<int> u (n+1), v (n+1), p (n+1), way (n+1);
for (int i = 1; i <= n; ++i) {
p[0] = i;
int j0 = 0;
vector<double> minv (n+1, 1e18);
vector<char> used (n+1, false);
do {
used[j0] = true;
int i0 = p[j0], j1;
double delta = 1e18;
for (int j = 1; j <= n; ++j)
if (!used[j]) {
double cur = e[i0][j]-u[i0]-v[j];
if (cur < minv[j])
minv[j] = cur, way[j] = j0;
if (minv[j] < delta)
delta = minv[j], j1 = j;
}
for (int j = 0; j <= n; ++j)
if (used[j])
u[p[j]] += delta, v[j] -= delta;
else
minv[j] -= delta;
j0 = j1;
} while (p[j0] != 0);
do {
int j1 = way[j0];
p[j0] = p[j1];
j0 = j1;
} while (j0);
}
for (int j = 1; j <= n; ++j)
ans[p[j]] = j;
}
int main() {
scanf("%d", &n);
while(getchar()!='\n');
for (int i = 0; i < n; i++) {
int t = 0;
for (int ch = getchar(); ch != '\n'; ch = getchar())
s[t++] = ch;
s[t++] = '.';
s[t++] = 0;
add(0, s, i);
}
while (getchar()!='\n');
for (int i = 0; i < n; i++) {
int t = 0;
for (int ch = getchar(); ch != '\n'; ch = getchar()) {
if (ch == EOF) break;
s[t++] = ch;
}
s[t++] = '.';
s[t++] = 0;
eval(0, s, i);
}
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
e[i][j] = 1e5 - e[i][j];
hungarian();
for (int i = 1; i <= n; i++)
printf("%d\n", ans[i]);
return 0;
}