198 lines
4.2 KiB
C++
198 lines
4.2 KiB
C++
/* Problem URL: https://codeforces.com/group/YgJmumGtHD/contest/102223/problem/J */
|
|
|
|
#pragma GCC optimize(3)
|
|
#pragma GCC optimize("Ofast,unroll-loops")
|
|
|
|
#include <bits/stdc++.h>
|
|
|
|
using namespace std;
|
|
|
|
#define V vector
|
|
|
|
#define rmin(a, b) a = min(a, b)
|
|
#define rmax(a, b) a = max(a, b)
|
|
|
|
#define rep(i, lim) for (size_t i = 0; i < (lim); i++)
|
|
#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++)
|
|
|
|
#define repv(i, v) for (auto &i : (v))
|
|
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
|
#define sortv(v) sort(v.begin(), v.end())
|
|
#define all(v) (v).begin(), (v).end()
|
|
|
|
using vi = vector<int>;
|
|
using vvi = vector<vi>;
|
|
using vvvi = vector<vvi>;
|
|
using vvvvi = vector<vvvi>;
|
|
|
|
using ll = long long;
|
|
|
|
using vl = vector<ll>;
|
|
using vvl = vector<vl>;
|
|
using vvvl = vector<vvl>;
|
|
using vvvvl = vector<vvvl>;
|
|
|
|
template<class v>
|
|
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
|
os << vec[0];
|
|
for (size_t i = 1; i < vec.size(); i++) {
|
|
os << ' ' << vec[i];
|
|
}
|
|
os << '\n';
|
|
return os;
|
|
}
|
|
|
|
template<class v>
|
|
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
|
for (auto &i : vec) {
|
|
is >> i;
|
|
}
|
|
return is;
|
|
}
|
|
|
|
template<class v>
|
|
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
|
for (auto &i : vec) {
|
|
os << i[0];
|
|
for (size_t j = 1; j < i.size(); j++) {
|
|
os << ' ' << i[j];
|
|
}
|
|
os << '\n';
|
|
}
|
|
return os;
|
|
}
|
|
|
|
template<class v>
|
|
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
|
for (auto &i : vec) {
|
|
for (auto &j : i) {
|
|
is >> j;
|
|
}
|
|
}
|
|
return is;
|
|
}
|
|
|
|
struct hashed_string {
|
|
static vl p;
|
|
static ll m;
|
|
static ll b;
|
|
vl hash;
|
|
|
|
hashed_string(string &a) {
|
|
while (p.size() <= a.size()) {
|
|
p.push_back(((__int128)p.back() * b) % m);
|
|
}
|
|
|
|
hash.resize(a.size() + 1);
|
|
hash[0] = 0;
|
|
|
|
rep(i, a.size()) {
|
|
hash[i + 1] = ((__int128)hash[i] * b + a[i]) % m;
|
|
}
|
|
};
|
|
|
|
ll gethash(int l, int r) {
|
|
return ((hash[r + 1] - (__int128)hash[l] * p[r - l + 1]) % m + m) % m;
|
|
};
|
|
};
|
|
|
|
vl hashed_string::p = {1};
|
|
ll hashed_string::m = (1LL << 61) - 1;
|
|
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
|
|
ll hashed_string::b = uniform_int_distribution<ll>(1, m - 1)(rng);
|
|
|
|
#define MAX_N 5 * (ll)1e4 + 10
|
|
|
|
// tuple<ll, string, size_t, int> msg[MAX_N];
|
|
ll hashm[MAX_N];
|
|
string trans[MAX_N];
|
|
size_t sizes[MAX_N];
|
|
int itr[MAX_N];
|
|
map<ll, int> hashtmp;
|
|
|
|
size_t hashsize;
|
|
ll hashes[MAX_N * 10];
|
|
ll pos[MAX_N * 10];
|
|
|
|
pair<int, size_t> dp[(ll)1e6];
|
|
int aux[(ll)1e6];
|
|
|
|
int main()
|
|
{
|
|
ios::sync_with_stdio(false);
|
|
cin.tie(nullptr);
|
|
|
|
int n;
|
|
cin >> n;
|
|
|
|
rep(j, n) {
|
|
string tmp;
|
|
cin >> tmp;
|
|
|
|
hashed_string act(tmp);
|
|
hashm[j] = act.hash.back();
|
|
cin.ignore();
|
|
|
|
getline(cin, trans[j]);
|
|
sizes[j] = tmp.size();
|
|
itr[j] = j;
|
|
hashtmp[hashm[j]] = j;
|
|
}
|
|
|
|
hashsize = 0;
|
|
repv(i, hashtmp) {
|
|
hashes[hashsize] = i.first;
|
|
pos[hashsize] = i.second;
|
|
hashsize++;
|
|
}
|
|
|
|
string tmp;
|
|
cin >> tmp;
|
|
hashed_string a(tmp);
|
|
|
|
rep(i, tmp.size()) {
|
|
dp[i] = {-2, INT64_MAX >> 1};
|
|
}
|
|
|
|
rep(j, n) {
|
|
if (a.gethash(0, sizes[j] - 1) == hashm[j]) {
|
|
dp[sizes[j] - 1].second = 1;
|
|
dp[sizes[j] - 1].first = -1;
|
|
aux[sizes[j] - 1] = j;
|
|
}
|
|
}
|
|
|
|
rep(i, tmp.size()) {
|
|
if (dp[i].first == -2) {
|
|
continue;
|
|
}
|
|
nrep(s, 1, 51) {
|
|
if (i + s < tmp.size()) {
|
|
ll tmp = a.gethash(i + 1, i + s);
|
|
// auto itr = hashes.find(tmp);
|
|
ll *itr = lower_bound(hashes, hashes + hashsize, tmp);
|
|
if (itr != hashes + hashsize && *itr == tmp && dp[i + s].second > dp[i].second + 1) {
|
|
dp[i + s].second = dp[i].second + 1;
|
|
dp[i + s].first = i;
|
|
aux[i + s] = pos[itr - hashes];
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
stack<int> s;
|
|
|
|
int i = tmp.size() - 1;
|
|
while (i >= 0) {
|
|
s.push(aux[i]);
|
|
i = dp[i].first;
|
|
}
|
|
|
|
while (!s.empty()) {
|
|
string &b = trans[s.top()];
|
|
cout << b << ' ';
|
|
s.pop();
|
|
}
|
|
cout << '\n';
|
|
}
|