Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma GCC optimize ("Ofast")
- #pragma GCC target ("avx2")
- #define _CRT_SECURE_NO_WARNINGS
- #include <iostream>
- #include <string>
- #include <vector>
- #include <stack>
- #include <algorithm>
- #define endl '\n';
- using namespace std;
- int n;
- string s,s1;
- vector<string> dp[100];
- vector <pair <string, string> > alph;
- int main()
- {
- #ifdef _DEBUG
- //freopen("input.txt", "r", stdin);
- #else
- ios_base::sync_with_stdio(false);
- cin.tie(0);
- cout.tie(0);
- #endif
- cin >> n;
- alph.resize(n);
- for (long long i = 0; i < n; i++) {
- cin >> alph[i].first >> alph[i].second;
- }
- cin >> s;
- dp[0].push_back("");
- for (int i = 0; i < s.size(); i++) {
- if (!dp[i].empty()) {
- for (int j = 0; j < n; j++) {
- s1 = alph[j].first;
- if (s1.size() + i <= s.size()) {
- bool z = true;
- for (int j1 = i; j1 < i + s1.size(); j1++) {
- if (s[j1] != s1[j1 - i]) {
- z = false;
- break;
- }
- }
- if (z) {
- dp[i + s1.size()].push_back(dp[i][0] + alph[j].second);
- }
- }
- }
- }
- }
- if (dp[s.size()].empty()) {
- cout << "NO ANSWER";
- }
- else {
- cout << dp[s.size()][0];
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement