当前位置:   article > 正文

CF633C(trie树dfs / 字符串hash + 线性dp)

cf633c

 题意:
描述了一种加密技术,现在将加密后的字符串和字典给出,要你求还原后的字符串。
加密方式:
①将所有字母改为小写字母
②将所单词翻转
③将所有空格去掉

思路:

解法一:(字符串hash+dp)原串长度只有1e4,然后我们可以考虑dp,令f_i为以第i个位置开头的待匹配子串的hash值,然后线性dp即可.不过cf卡unordered_map,会T,

解法二:(trie树)考虑对每个待匹配的子串倒着插入字典树,然后dfs即可.

代码:

思路一:

  1. #include <bits/stdc++.h>
  2. // #define int long long
  3. #define IOS ios::sync_with_stdio(false), cin.tie(0)
  4. #define ll long long
  5. #define double long double
  6. #define ull unsigned long long
  7. #define PII pair<int, int>
  8. #define PDI pair<double, int>
  9. #define PDD pair<double, double>
  10. #define debug(a) cout << #a << " = " << a << endl
  11. #define point(n) cout << fixed << setprecision(n)
  12. #define all(x) (x).begin(), (x).end()
  13. #define mem(x, y) memset((x), (y), sizeof(x))
  14. #define lbt(x) (x & (-x))
  15. #define SZ(x) ((x).size())
  16. #define inf 0x3f3f3f3f
  17. #define INF 0x3f3f3f3f3f3f3f3f
  18. namespace nqio{const unsigned R = 4e5, W = 4e5; char *a, *b, i[R], o[W], *c = o, *d = o + W, h[40], *p = h, y; bool s; struct q{void r(char &x){x = a == b && (b = (a = i) + fread(i, 1, R, stdin), a == b) ? -1 : *a++;} void f(){fwrite(o, 1, c - o, stdout); c = o;} ~q(){f();}void w(char x){*c = x;if (++c == d) f();} q &operator >>(char &x){do r(x);while (x <= 32); return *this;} q &operator >>(char *x){do r(*x); while (*x <= 32); while (*x > 32) r(*++x); *x = 0; return *this;} template<typename t> q&operator>>(t &x){for (r(y),s = 0; !isdigit(y); r(y)) s |= y == 45;if (s) for (x = 0; isdigit(y); r(y)) x = x * 10 - (y ^ 48); else for (x = 0; isdigit(y); r(y)) x = x * 10 + (y ^ 48); return *this;} q &operator <<(char x){w(x);return *this;}q &operator<< (char *x){while (*x) w(*x++); return *this;}q &operator <<(const char *x){while (*x) w(*x++); return *this;}template<typename t> q &operator<< (t x) {if (!x) w(48); else if (x < 0) for (w(45); x; x /= 10) *p++ = 48 | -(x % 10); else for (; x; x /= 10) *p++ = 48 | x % 10; while (p != h) w(*--p);return *this;}}qio; }using nqio::qio;
  19. using namespace std;
  20. const int base = 131, N = 2e6 + 10;
  21. int n, m;
  22. ull f[N];
  23. unordered_map<ull, string> mp;
  24. string ori, s;
  25. void solve() {
  26. cin >> n >> ori >> m;
  27. for (int i = 1; i <= m; ++i) {
  28. cin >> s;
  29. ull hsh = 0;
  30. for (int j = SZ(s) - 1; j >= 0; --j) hsh = hsh * base + tolower(s[j]) - 'a' + 1;
  31. mp[hsh] = s;
  32. }
  33. f[n] = 1;
  34. for (int i = n - 1; i >= 0; --i) {
  35. ull hsh = 0;
  36. for (int j = 1; i + j - 1 <= n; ++j) {
  37. hsh = hsh * base + ori[i + j - 1] - 'a' + 1;
  38. if (mp.count(hsh) && f[i + j]) {
  39. f[i] = hsh; break;
  40. }
  41. }
  42. }
  43. for (int i = 0; i <= n - 1; i += SZ(mp[f[i]])) cout << mp[f[i]] << " ";
  44. }
  45. signed main() {
  46. IOS;
  47. int T = 1;
  48. // qio >> T;
  49. while (T--) solve();
  50. }

思路二:

  1. #include <bits/stdc++.h>
  2. // #define int long long
  3. #define IOS ios::sync_with_stdio(false), cin.tie(0)
  4. #define ll long long
  5. #define double long double
  6. #define ull unsigned long long
  7. #define PII pair<int, int>
  8. #define PDI pair<double, int>
  9. #define PDD pair<double, double>
  10. #define debug(a) cout << #a << " = " << a << endl
  11. #define point(n) cout << fixed << setprecision(n)
  12. #define all(x) (x).begin(), (x).end()
  13. #define mem(x, y) memset((x), (y), sizeof(x))
  14. #define lbt(x) (x & (-x))
  15. #define SZ(x) ((x).size())
  16. #define inf 0x3f3f3f3f
  17. #define INF 0x3f3f3f3f3f3f3f3f
  18. namespace nqio{const unsigned R = 4e5, W = 4e5; char *a, *b, i[R], o[W], *c = o, *d = o + W, h[40], *p = h, y; bool s; struct q{void r(char &x){x = a == b && (b = (a = i) + fread(i, 1, R, stdin), a == b) ? -1 : *a++;} void f(){fwrite(o, 1, c - o, stdout); c = o;} ~q(){f();}void w(char x){*c = x;if (++c == d) f();} q &operator >>(char &x){do r(x);while (x <= 32); return *this;} q &operator >>(char *x){do r(*x); while (*x <= 32); while (*x > 32) r(*++x); *x = 0; return *this;} template<typename t> q&operator>>(t &x){for (r(y),s = 0; !isdigit(y); r(y)) s |= y == 45;if (s) for (x = 0; isdigit(y); r(y)) x = x * 10 - (y ^ 48); else for (x = 0; isdigit(y); r(y)) x = x * 10 + (y ^ 48); return *this;} q &operator <<(char x){w(x);return *this;}q &operator<< (char *x){while (*x) w(*x++); return *this;}q &operator <<(const char *x){while (*x) w(*x++); return *this;}template<typename t> q &operator<< (t x) {if (!x) w(48); else if (x < 0) for (w(45); x; x /= 10) *p++ = 48 | -(x % 10); else for (; x; x /= 10) *p++ = 48 | x % 10; while (p != h) w(*--p);return *this;}}qio; }using nqio::qio;
  19. using namespace std;
  20. const int N = 1e4 + 10, M = 1e6 + 10;
  21. int n, m, tot;
  22. string ori, w[M];
  23. vector<int> ans;
  24. struct rec {
  25. int son[26], flag;
  26. } tr[M];
  27. void build(string s, int id) {
  28. int p = 0;
  29. for (int i = 0; i < SZ(s); ++i) {
  30. int c = s[i] - 'a';
  31. if (tr[p].son[c] == 0) tr[p].son[c] = ++tot;
  32. p = tr[p].son[c];
  33. }
  34. tr[p].flag = id;
  35. }
  36. bool dfs(int pos) {
  37. if (pos >= SZ(ori)) return true;
  38. int p = 0;
  39. for (int i = pos; i < SZ(ori); ++i) {
  40. int c = ori[i] - 'a';
  41. if (tr[p].son[c] == 0) break;
  42. p = tr[p].son[c];
  43. if (tr[p].flag != 0 && dfs(i + 1)) return ans.emplace_back(tr[p].flag), true;
  44. }
  45. return false;
  46. }
  47. void solve() {
  48. cin >> n >> ori >> m;
  49. for (int i = 1; i <= m; ++i) {
  50. cin >> w[i];
  51. auto t = w[i];
  52. reverse(all(t));
  53. for (auto &c : t) if (c < 'a') c += 32;
  54. build(t, i);
  55. }
  56. dfs(0);
  57. for (int i = SZ(ans) - 1; i >= 0; --i) cout << w[ans[i]] << " ";
  58. }
  59. signed main() {
  60. IOS;
  61. int T = 1;
  62. // qio >> T;
  63. while (T--) solve();
  64. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/在线问答5/article/detail/900823
推荐阅读
相关标签
  

闽ICP备14008679号