noomaK

Untitled

Nov 5th, 2023
424
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.78 KB | None | 0 0
  1. #include "bits/stdc++.h"
  2. using namespace std;
  3.  
  4. typedef long long ll;
  5.  
  6. #define deb(...) logger(#__VA_ARGS__, __VA_ARGS__)
  7. template<typename ...Args>
  8. void logger(string vars, Args&&... values) {
  9.   cout << vars << " = ";
  10.   string delim = "";
  11.   (..., (cout << delim << values, delim = ", "));
  12.   cout << '\n';
  13. }
  14.  
  15. void solve() {
  16.   int n, m;
  17.   cin >> n >> m;
  18.   vector<string> a(n);
  19.   for (auto &x : a) cin >> x;
  20.   map<string, int> pr, sf;
  21.   map<string, int> fp, fs;
  22.   for (int i = 0; i < n; ++i) {
  23.     string cur;
  24.     auto &s = a[i];
  25.     for (int j = 0; j < s.size(); ++j) {
  26.       cur.push_back(s[j]);
  27.       if (j >= 2) {
  28.         fp[cur] = i;
  29.         pr[cur]++;
  30.       }
  31.     }
  32.     cur = "";
  33.     for (int j = 0; j + 2 < s.size(); ++j) {
  34.       cur = s.substr(j);
  35.       fs[cur] = i;
  36.       sf[cur]++;
  37.     }
  38.   }
  39.   while (m--) {
  40.     string s;
  41.     cin >> s;
  42.     string t;
  43.     int n = s.size();
  44.     int f = 0;
  45.     int ok = 1;
  46.     int cnt = 0;
  47.     pair<int, int> ans {-1, -1};
  48.     for (int j = 2; ok && n - (j + 1) >= 2 && j < n; ++j) {
  49.       string a = s.substr(0, j + 1);
  50.       string x = s.substr(j + 1);
  51.       for (auto b : {a.back() + x, x}) if (b.length() >= 3) {
  52.         if (fp.count(a) && fs.count(b)) {
  53.           if (pr[a] > 1 || sf[b] > 1) {
  54.             ok = 0;
  55.             break;
  56.           }
  57.           cnt += 1;
  58.           int i = fp[a], j = fs[b];
  59.           if (cnt > 1 && i != ans.first && ans.second != j) {
  60.             ok = 0;
  61.             break;
  62.           }
  63.           ans = {fp[a], fs[b]};
  64.         }
  65.       }
  66.     }
  67.     if (!ok) {
  68.       cout << "ambiguous";
  69.     } else if (ans.first == -1) {
  70.       cout << "none";
  71.     } else {
  72.       cout << a[ans.first] << ' ' << a[ans.second];
  73.     }
  74.     cout << '\n';
  75.   }
  76. }
  77.  
  78. int main() {
  79.   ios::sync_with_stdio(false);
  80.   cin.tie(nullptr);
  81.   int t = 1;
  82.   // cin >> t;
  83.   while (t--) solve();
  84. }
  85.  
Advertisement
Add Comment
Please, Sign In to add comment