Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- #define int int64_t
- const int inf = 2e18;
- int32_t main() {
- ios_base::sync_with_stdio(0);
- cin.tie(0); cout.tie(0);
- int l, r, n;
- cin >> l >> r >> n;
- l--;
- vector<char> from(n);
- vector<string> to(n);
- for (int i = 0; i < n; i++) {
- cin >> from[i] >> to[i];
- from[i] -= 'a';
- for (int j = 0; j < to[i].size(); j++) {
- to[i][j] -= 'a';
- }
- }
- vector<vector<int>> sz(n + 1, vector<int>(26, 1));
- for (int i = n - 1; i >= 0; i--) {
- sz[i] = sz[i + 1];
- sz[i][from[i]] = 0;
- for (char c: to[i]) {
- sz[i][from[i]] += sz[i + 1][c];
- sz[i][from[i]] = min(sz[i][from[i]], inf);
- }
- }
- vector<int> fr(26);
- string s(1, 0);
- for (int i = 0; i < n; i++) {
- vector<int> new_fr(26);
- for (int j = 0; j < 26; j++) {
- if (j != from[i]) {
- new_fr[j] += fr[j];
- } else {
- for (char c: to[i]) {
- new_fr[c] += fr[j];
- }
- }
- }
- fr = new_fr;
- string new_s;
- for (char c: s) {
- if (c != from[i]) {
- new_s.push_back(c);
- } else {
- for (char c2: to[i]) {
- new_s.push_back(c2);
- }
- }
- }
- int fin_sz = 0;
- for (int j = 0; j < 26; j++) {
- fin_sz += fr[j] * sz[i + 1][j];
- }
- s = "";
- for (char c: new_s) {
- fin_sz += sz[i + 1][c];
- if (fin_sz <= l) {
- fr[c]++;
- continue;
- }
- s.push_back(c);
- if (fin_sz >= r) {
- break;
- }
- }
- }
- for (int i = 0; i < s.size(); i++) {
- s[i] += 'a';
- }
- cout << s << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement