-
Notifications
You must be signed in to change notification settings - Fork 2
/
E - Games on a CD.cpp
142 lines (121 loc) · 2.66 KB
/
E - Games on a CD.cpp
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
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define REP(i, a, b) for(int i = (a); i < (b); ++i)
const int MOD[2] = {1000000007, 1000000009};
const int MAXN = 2e6 + 5;
const int P = 131;
int n, k;
int xp[2][MAXN], h[2][MAXN];
string s;
map<pii, int> arr;
int vis[MAXN];
int tim;
int ans[MAXN];
void init()
{
REP(i, 0, 2) {
xp[i][0] = 1;
REP(j, 1, MAXN) {
xp[i][j] = (ll)xp[i][j - 1] * P % MOD[i];
}
}
}
void fastio()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
}
void getPre(const string& str, int k)
{
int len = str.length();
REP(i, 0, 2) {
h[i][0] = 0;
for (int j = 1; j <= len; ++j) {
h[i][j] = ((ll)h[i][j - 1] * P + str[j - 1]) % MOD[i];
}
for (int j = len + 1, m = 0; m < k; ++j, ++m) {
h[i][j] = ((ll)h[i][j - 1] * P + str[m]) % MOD[i];
}
}
}
pii getHash(const string& s)
{
int len = s.length();
int res[2] = {0, 0};
REP(i, 0, 2) {
REP(j, 0, len) {
res[i] = ((ll)res[i] * P + s[j]) % MOD[i];
}
}
return pii(res[0], res[1]);
}
pii getHash(int l, int r)
{
int res[2] = {0, 0};
REP(i, 0, 2) {
res[i] = (h[i][r] - (ll)h[i][l - 1] * xp[i][r - l + 1] % MOD[i] + MOD[i]) % MOD[i];
}
return pii(res[0], res[1]);
}
bool check(int st)
{
tim++;
int l = st, r = st + k - 1;
for (int i = 1; i <= n; ++i, l += k, r += k) {
pii p = getHash(l, r);
if (!arr.count(p)) {
return false;
}
int val = arr[p];
if (vis[val] == tim) {
return false;
}
vis[val] = tim;
ans[i - 1] = val;
}
return true;
}
int main()
{
fastio();
#ifndef ONLINE_JUDGE
ifstream fin("f:\\OJ\\uva_in.txt");
streambuf* cinback = cin.rdbuf(fin.rdbuf());
#endif // ONLINE_JUDGE
init();
while (cin >> n >> k) {
arr.clear();
tim = 1;
cin >> s;
getPre(s, k);
int g;
cin >> g;
REP(i, 0, g) {
cin >> s;
pii p = getHash(s);
arr[p] = i + 1;
}
bool ok = false;
for (int i = 1; i <= k; ++i) {
if (check(i)) {
ok = true;
break;
}
}
if (!ok) {
cout << "NO" << endl;
} else {
cout << "YES" << endl;
REP(i, 0, n) {
cout << ans[i] << (i == n - 1 ? '\n' : ' ');
}
}
}
#ifndef ONLINE_JUDGE
cin.rdbuf(cinback);
#endif // ONLINE_JUDGE
return 0;
}