From 71e84c055e039c9d9fdaf89477d74fe1e1cb22b5 Mon Sep 17 00:00:00 2001 From: Baoshuo Date: Thu, 18 Aug 2022 17:23:12 +0800 Subject: [PATCH] P4824 [USACO15FEB] Censoring S https://www.luogu.com.cn/record/84303219 --- Luogu/P4824/P4824.cpp | 51 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 Luogu/P4824/P4824.cpp diff --git a/Luogu/P4824/P4824.cpp b/Luogu/P4824/P4824.cpp new file mode 100644 index 00000000..689d776d --- /dev/null +++ b/Luogu/P4824/P4824.cpp @@ -0,0 +1,51 @@ +#include +#include +#include +#include +#include + +using std::cin; +using std::cout; +const char endl = '\n'; + +const int N = 1e6 + 5; +const int hash = 233333; + +int cnt; +char st[N]; +std::string s, t; +unsigned long long p = 1, h[N], t_hash; + +int main() { + std::ios::sync_with_stdio(false); + cin.tie(nullptr); + + cin >> s >> t; + + int n = s.size(); + int m = t.size(); + s = ' ' + s; + + for (char c : t) { + t_hash = t_hash * hash + c; + p *= hash; + } + + for (int i = 1; i <= n; i++) { + st[++cnt] = s[i]; + + h[cnt] = h[cnt - 1] * hash + s[i]; + + if (cnt >= m && h[cnt] - h[cnt - m] * p == t_hash) { + cnt -= m; + } + } + + for (int i = 1; i <= cnt; i++) { + cout << st[i]; + } + + cout << endl; + + return 0; +}