Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma once
- #define _CRT_SECURE_NO_WARNINGS
- #define _USE_MATH_DEFINES
- #include <iostream>
- #include <string>
- #include <map>
- #include <set>
- #include <algorithm>
- #include <vector>
- #include <stdio.h>
- #include <cmath>
- #include <math.h>
- #include <queue>
- #include <stack>
- #include <climits>
- #include <deque>
- #include <ctime>
- #include <iomanip>
- #include <bitset>
- #include <unordered_map>
- #include <unordered_set>
- using namespace std;
- typedef long long ll;
- typedef unsigned long long ull;
- typedef unsigned int ui;
- #define mh() make_heap()
- #define poph() pop_heap()
- #define pushh() push_heap()
- #define sor(n) n.begin(), n.end()
- #define rsor(n) n.rbegin(), n.rend()
- #define mp make_pair
- #define files freopen("input.txt", "rt", stdin); freopen("output.txt", "wt", stdout)
- #define p(T) pair<T,T>
- #define toch(x) cout.precision(x), cout.setf(ios::fixed)
- #define znac(l) abs(l)/l
- #define IOS ios::sync_with_stdio(false)
- #define IOSB cin.tie(0), cout.tie(0);
- const ll ok = ll(1e9 + 7);
- void bfs(vector<vector<ll>>&graf, vector<bool> &used, vector<ll> &var, int v)
- {
- queue<ll>next;
- next.push(v);
- while (!next.empty())
- {
- ll now = next.front();
- next.pop();
- for (int i = 0; i < graf[now].size(); i++)
- {
- if (!used[graf[now][i]])
- {
- used[graf[now][i]] = 1;
- var[graf[now][i]] = now;
- next.push(graf[now][i]);
- }
- }
- }
- }
- int main()
- {
- IOSB;
- IOS;
- #ifdef TheCompiler
- files;
- #endif
- short numer[26] = { 2,2,2,3,3,3,4,4,1,1,5,5,6,6,0,7,0,7,7,8,8,8,9,9,9,0 };
- string num = "";
- cin >> num;
- while (num != "-1")
- {
- vector<vector<ll>>graf(102);
- vector<bool> used(102, 0);
- ll n;
- cin >> n;
- vector<string>word;
- map<ll, set<string>> dic;
- map<string, string>old;
- for (int i = 1; i <= n; i++)
- {
- string s;
- string h = "";
- cin >> s;
- for (int i = 0; i < s.size(); i++)
- {
- h += numer[s[i] - 'a'] + '0';
- }
- ll p = num.find(h);
- if (p >= 0 && p <= num.size())
- {
- for (int i = 0; i < num.size(); i++)
- {
- if (num.find(h, i) == i)
- {
- graf[i].push_back(i + h.size());
- }
- }
- old[h] = s;
- }
- }
- vector<ll> var(102, -1);
- bfs(graf, used, var, 0);
- ll mn = LLONG_MAX, pos = -1;
- stack<string>ans;
- if (var[num.size()] != -1)
- {
- ll i = num.size();
- string h = "";
- while (var[i] != -1)
- {
- for (int j = var[i]; j <i; j++)
- {
- h += num[j];
- }
- ans.push(h);
- h = "";
- i = var[i];
- }
- while (!ans.empty())
- {
- h = ans.top();
- ans.pop();
- cout << old[h] << " ";
- }
- cout << endl;
- }
- else
- cout << "No solution." << endl;
- cin >> num;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement