Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #define pb push_back
- #define mp make_pair
- #define sz size
- #define ll long long
- #define ld long double
- #define fs first
- #define sc second
- #define forn(i, f, t) for(int i = f; i < t; i++)
- #define all(x) (x).begin(), (x).end()
- #define ins insert
- const int INF = 2147483647;
- const int MOD = 1000000007;
- const ll INF64 = 9223372036854775807;
- const ld EPS = 1e-7;
- using namespace std;
- int main(){
- int n, m, k, t, ans = 0;
- string ph, s;
- vector<pair<int, string> > phs;
- cin >> m >> n;
- cin >> s;
- forn(i, 0, n){
- cin >> k >> t;
- cin >> ph;
- phs.pb(mp(t, ph));
- }
- sort(all(phs));
- string tmp = "";
- forn(i, 0, n){
- tmp = phs[i].sc + '#' + s;
- int pos = phs[i].sc.sz() + 1;
- for (; tmp[0] != '#'; tmp = tmp.substr(1, tmp.sz() - 1)){
- int l = tmp.sz();
- int p[l];
- p[0] = 0;
- forn(j, 1, l){
- int d = p[j - 1];
- while (tmp[d] != tmp[j] && d)
- d = p[d - 1];
- p[j] = d + (tmp[d] == tmp[j]);
- }
- /*forn(j, 0, pos) cout << " ";
- forn(j, pos, l)
- cout << p[j];
- cout << "\n";*/
- forn(j, pos, l - 1)
- if (p[j + 1] == 0)
- forn(z, j - p[j] + 1, j + 1)
- tmp[z] = '0';
- if (p[l - 1] != 0)
- forn(z, l - p[l - 1], l)
- tmp[z] = '0';
- // cout << tmp << " ";
- string tmp2 = tmp;
- tmp = "";
- int cur = 0;
- forn(j, 0, tmp2.sz())
- if (tmp2[j] != '0')
- tmp += tmp2[j];
- else
- cur++;
- ans += cur * phs[i].fs;
- pos--;
- // cout << tmp << "\n";
- }
- s = tmp.substr(tmp.find('#') + 1, tmp.sz() - tmp.find('#'));
- }
- cout << (tmp.find('#') != tmp.sz() - 1 ? -1 : ans) << "\n";
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement