willy108

TLE 95 B

Jan 2nd, 2022
1,123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.35 KB | None | 0 0
  1.  
  2.  
  3. //misaka and rin will carry me to cm
  4. #include <iostream>
  5. #include <cstdio>
  6. #include <cstring>
  7. #include <utility>
  8. #include <cassert>
  9. #include <algorithm>
  10. #include <vector>
  11. #include <array>
  12. #include <tuple>
  13. #include <map>
  14. #include <unordered_map>
  15.  
  16. #define ll long long
  17. #define lb long double
  18. #define sz(vec) ((int)(vec.size()))
  19. #define all(x) x.begin(), x.end()
  20.  
  21. const lb eps = 1e-9;
  22. const ll mod = 1e9 + 7, ll_max = 1e18;
  23. //const ll mod = (1 << (23)) * 119 +1;
  24. const int MX = 5e5 +10, int_max = 0x3f3f3f3f;
  25.  
  26. using namespace std;
  27.  
  28. typedef uint64_t ull;
  29. static int C = 1009; // initialized below
  30.  
  31. // Arithmetic mod two primes and 2^32 simultaneously.
  32. // "typedef uint64_t H;" instead if Thue-Morse does not apply.
  33. template<int M, class B>
  34. struct A {
  35.     int x; B b; A(int x=0) : x(x), b(x) {}
  36.     A(int x, B b) : x(x), b(b) {}
  37.     A operator+(A o){int y = x+o.x; return{y - (y>=M)*M, b+o.b};}
  38.     A operator-(A o){int y = x-o.x; return{y + (y< 0)*M, b-o.b};}
  39.     A operator*(A o) { return {(int)(1LL*x*o.x % M), b*o.b}; }
  40.     explicit operator ull() { return x ^ (ull) b << 21; }
  41.     bool operator==(A o){ return ull(*this) == ull(o); }
  42.     bool operator!=(A o){ return ull(*this) != ull(o); }
  43. };
  44. //typedef A<1000000007, A<1000000009, unsigned>> H;
  45. typedef uint64_t H;
  46. struct HashInterval {
  47.     int n;
  48.     vector<H> ha, pw;
  49.     HashInterval(string& str) : ha(sz(str)+1), pw(ha) {
  50.         n = sz(str);
  51.         pw[0] = 1;
  52.         ha[0] = 1;
  53.         for(int i = 0; i< sz(str); i++)
  54.             ha[i+1] = ha[i] * C + str[i],
  55.             pw[i+1] = pw[i] * C;
  56.     }
  57.     H get(int a, int b) { // hash [a, b)
  58.         return ha[b] - ha[a] * pw[b - a];
  59.     }
  60. };
  61.  
  62. vector<H> getHashes(string& str, int length) {
  63.     if (sz(str) < length) return {};
  64.     H h = 0, pw = 1;
  65.     for(int i = 0; i <length; i++)
  66.         h = h * C + str[i], pw = pw * C;
  67.     vector<H> ret = {h};
  68.     for(int i = length; i<sz(str); i++) {
  69.         ret.push_back(h = h * C + str[i] - pw * str[i-length]);
  70.     }
  71.     return ret;
  72. }
  73.  
  74. H hashString(string& s){H h{}; for(char c:s) h=h*C+c;return h;}
  75.  
  76. int lcp(HashInterval& a, HashInterval& b){ //length of lcp of two strings
  77.     if (a.n == 0 || b.n == 0 || a.get(0, 0) != b.get(0, 0)) return 0;
  78.     int lo=0, hi=min(a.n, b.n)+1, mid=(lo+hi)/2;
  79.     while (lo < mid && mid < hi){
  80.         if (a.get(0, mid-1) == b.get(0, mid-1)) lo=mid;
  81.         else hi=mid;
  82.         mid=(lo+hi)/2;
  83.     }
  84.     return lo;
  85. }
  86.  
  87. unordered_map<ull, ll> adj[MX];
  88. //set<HashInterval> adj[MX];
  89. string str;
  90. unordered_map<ull, ll> price;
  91. ll dp[MX];
  92. int m, cnt[MX];
  93. vector<pair<int, ull>> yes;
  94. vector<int> useful;
  95. void solve(){
  96.     cin >> m;
  97.     price.reserve(m);
  98.     for(int i = 0; i<m; i++){
  99.         string a; int b;
  100.         cin >> a >> b;
  101.         H temp = hashString(a);
  102.         ull ttt = (ull)(temp);
  103.         if(price.count(ttt)) price[ttt] = min((ll)b, price[ttt]);
  104.         else price[ttt] = (ll)b;
  105.         cnt[sz(a)]++;
  106.         yes.push_back(make_pair(sz(a), ttt));
  107.         //adj[sz(a)]
  108.     }
  109.     cin >> str;
  110.     for(int i = 1; i<=sz(str); i++){
  111.         if(cnt[i]) useful.push_back(i);
  112.     }
  113.     HashInterval ash = HashInterval(str);
  114.     memset(dp, 63, sizeof(dp));
  115.     dp[0] = 0;
  116.     for(int i = 1; i<=sz(str); i++){
  117.         for(int j : useful){
  118.             if(j > i) break;
  119.             ull t = (ull)(ash.get(i-j, i));
  120.             if(dp[i - j] <= (1e18) && price.count(t)) dp[i] = min(dp[i], dp[i-j] + price[t]);
  121.         }
  122.     }
  123.     cout << ((dp[sz(str)] > 1e18) ? (-1) : (dp[sz(str)])) << "\n";
  124. }
  125.  
  126. int main(){
  127.   cin.tie(0) -> sync_with_stdio(0);
  128.     int T = 1;
  129.     //cin >> T;
  130.   while(T--){
  131.         solve();
  132.     }
  133.     return 0;
  134. }
  135.  
  136.  
  137.  
Advertisement
Add Comment
Please, Sign In to add comment