Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- #define endl '\n'
- #define FIO ios_base::sync_with_stdio(false);cin.tie(NULL)
- #define HM 1000000007
- #define HA 100000007
- #define HB 101
- int hext(int h, int ch) {
- return (1ll * h * HA + ch + HB) % HM;
- }
- int main() {
- FIO;
- int N;
- string S;
- cin >> S >> N;
- unordered_map<int, unordered_map<int, vector<string>>> T;
- for (int i = 0; i < N; i++) {
- string ti;
- cin >> ti;
- int hsh = 0;
- for (int j = 0; j < ti.size(); j++) {
- hsh = hext(hsh, ti[j] - 'a');
- }
- T[ti.size()][hsh].push_back(ti);
- }
- string R;
- vector<int> H(1, 0);
- vector<int> HAPW(1, 1);
- for (int i = 0; i < S.size(); i++) {
- int ch = S[i] - 'a';
- H.push_back(hext(H.back(), ch));
- HAPW.push_back((1ll * HAPW.back() * HA) % HM);
- R += S[i];
- for(auto& it : T){
- int len = it.first;
- if (len > R.size()) {
- continue;
- }
- int hsub = (1ll * H[R.size() - len] * HAPW[len]) % HM;
- int hsh = (HM + H.back() - hsub) % HM;
- bool found = false;
- auto jt = it.second.find(hsh);
- if(jt != it.second.end()){
- for(string &t : jt->second) {
- if (t == R.substr(R.size() - len)) {
- R.resize(R.size() - len);
- H.resize(H.size() - len);
- HAPW.resize(HAPW.size() - len);
- found = true;
- break;
- }
- }
- }
- if(found){
- break;
- }
- }
- }
- cout << R << endl;
- return 0;
- }
Add Comment
Please, Sign In to add comment