Advertisement
Guest User

Untitled

a guest
Jun 18th, 2019
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.07 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. typedef long long ll;
  4. typedef long double ld;
  5. #define pb push_back
  6.  
  7. const int N = 1e6;
  8. int n;
  9. map<ll, ll> dp;
  10. vector<string> v;
  11. ll pw[N], ans;
  12. const ll mod = 1e9 + 7;
  13. const ll p = 31;
  14.  
  15. main() {
  16. ios_base::sync_with_stdio(0), cin.tie(0);
  17. cin >> n;
  18. v.resize(n);
  19. for (int i = 0; i < n; ++i) cin >> v[i];
  20. /*sort(v.begin(), v.end(), [&](string a, string b) {
  21. return a.size() < b.size();
  22. });*/
  23. pw[0] = 1;
  24. for (int i = 1; i < N; ++i) {
  25. pw[i] = (pw[i - 1] * p) % mod;
  26. }
  27. for (auto d : v) {
  28. ll hsh = (d[0] - 'A' + 1);
  29. ll cnt = dp[hsh];
  30. for (int i = 1; i + 1 < (int)d.size(); ++i) hsh = (hsh + (d[i] - 'A' + 1) * pw[i] % mod) % mod, cnt = max(cnt, dp[hsh]);
  31. hsh = 0;
  32. for (int i = (int)d.size() - 1; i > 0; --i) hsh = (hsh * p % mod + (d[i] - 'A' + 1)) % mod, cnt = max(cnt, dp[hsh]);
  33. hsh = 0;
  34. for (int i = 0; i < (int)d.size(); ++i) hsh = (hsh + (d[i] - 'A' + 1) * pw[i] % mod) % mod;
  35. dp[hsh] = max(dp[hsh], cnt) + 1;
  36. ans = max(ans, dp[hsh]);
  37. }
  38. cout << ans << '\n';
  39. return 0;
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement