Advertisement
PikMike

Untitled

Jun 10th, 2016
338
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.04 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. #define pb push_back
  4. #define mp make_pair
  5. #define sz size
  6. #define ll long long
  7. #define ld long double
  8. #define fs first
  9. #define sc second
  10. #define forn(i, f, t) for(int i = f; i < t; i++)
  11. #define all(x) (x).begin(), (x).end()
  12. #define ins insert
  13.  
  14. const int INF = 2147483647;
  15. const int MOD = 1000000007;
  16. const ll INF64 = 9223372036854775807;
  17. const ld EPS = 1e-7;
  18.  
  19. using namespace std;
  20.  
  21.  
  22. int main(){
  23.     int n, m, k, t, ans = 0;
  24.     string ph, s;
  25.     vector<pair<int, string> > phs;
  26.     cin >> m >> n;
  27.     cin >> s;
  28.     forn(i, 0, n){
  29.         cin >> k >> t;
  30.         cin >> ph;
  31.         phs.pb(mp(t, ph));
  32.     }
  33.     sort(all(phs));
  34.     string tmp = "";
  35.     forn(i, 0, n){
  36.         tmp = phs[i].sc + '#' + s;
  37.         int pos = phs[i].sc.sz() + 1;
  38.         for (; tmp[0] != '#'; tmp = tmp.substr(1, tmp.sz() - 1)){
  39.             int l = tmp.sz();
  40.             int p[l];
  41.             p[0] = 0;
  42.             forn(j, 1, l){
  43.                 int d = p[j - 1];
  44.                 while (tmp[d] != tmp[j] && d)
  45.                     d = p[d - 1];
  46.                 p[j] = d + (tmp[d] == tmp[j]);
  47.             }
  48.             /*forn(j, 0, pos) cout << " ";
  49.             forn(j, pos, l)
  50.                 cout << p[j];
  51.             cout << "\n";*/
  52.             forn(j, pos, l - 1)
  53.                 if (p[j + 1] == 0)
  54.                     forn(z, j - p[j] + 1, j + 1)
  55.                         tmp[z] = '0';
  56.             if (p[l - 1] != 0)
  57.                 forn(z, l - p[l - 1], l)
  58.                     tmp[z] = '0';
  59.             // cout << tmp << " ";
  60.             string tmp2 = tmp;
  61.             tmp = "";
  62.             int cur = 0;
  63.             forn(j, 0, tmp2.sz())
  64.                 if (tmp2[j] != '0')
  65.                     tmp += tmp2[j];
  66.                 else
  67.                     cur++;
  68.             ans += cur * phs[i].fs;
  69.             pos--;
  70.             // cout << tmp << "\n";
  71.         }
  72.         s = tmp.substr(tmp.find('#') + 1, tmp.sz() - tmp.find('#'));
  73.     }
  74.     cout << (tmp.find('#') != tmp.sz() - 1 ? -1 : ans) << "\n";
  75.     return 0;
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement