Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "bits/stdc++.h"
- using namespace std;
- typedef long long ll;
- #define deb(...) logger(#__VA_ARGS__, __VA_ARGS__)
- template<typename ...Args>
- void logger(string vars, Args&&... values) {
- cout << vars << " = ";
- string delim = "";
- (..., (cout << delim << values, delim = ", "));
- cout << '\n';
- }
- void solve() {
- int n, m;
- cin >> n >> m;
- vector<string> a(n);
- for (auto &x : a) cin >> x;
- map<string, int> pr, sf;
- map<string, int> fp, fs;
- for (int i = 0; i < n; ++i) {
- string cur;
- auto &s = a[i];
- for (int j = 0; j < s.size(); ++j) {
- cur.push_back(s[j]);
- if (j >= 2) {
- fp[cur] = i;
- pr[cur]++;
- }
- }
- cur = "";
- for (int j = 0; j + 2 < s.size(); ++j) {
- cur = s.substr(j);
- fs[cur] = i;
- sf[cur]++;
- }
- }
- while (m--) {
- string s;
- cin >> s;
- string t;
- int n = s.size();
- int f = 0;
- int ok = 1;
- int cnt = 0;
- pair<int, int> ans {-1, -1};
- for (int j = 2; ok && n - (j + 1) >= 2 && j < n; ++j) {
- string a = s.substr(0, j + 1);
- string x = s.substr(j + 1);
- for (auto b : {a.back() + x, x}) if (b.length() >= 3) {
- if (fp.count(a) && fs.count(b)) {
- if (pr[a] > 1 || sf[b] > 1) {
- ok = 0;
- break;
- }
- cnt += 1;
- int i = fp[a], j = fs[b];
- if (cnt > 1 && i != ans.first && ans.second != j) {
- ok = 0;
- break;
- }
- ans = {fp[a], fs[b]};
- }
- }
- }
- if (!ok) {
- cout << "ambiguous";
- } else if (ans.first == -1) {
- cout << "none";
- } else {
- cout << a[ans.first] << ' ' << a[ans.second];
- }
- cout << '\n';
- }
- }
- int main() {
- ios::sync_with_stdio(false);
- cin.tie(nullptr);
- int t = 1;
- // cin >> t;
- while (t--) solve();
- }
Advertisement
Add Comment
Please, Sign In to add comment