danielvitor23

3347 - Ancestralidade

Jul 12th, 2023
1,118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.79 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main() {
  5.   cin.tie(0)->sync_with_stdio(0);
  6.  
  7.   int n, p; cin >> n >> p;
  8.  
  9.   vector<string> s(n);
  10.   string t;
  11.  
  12.   for (int i = 0; i < p; i++) {
  13.     cin >> s[i];
  14.   }
  15.  
  16.   cin >> t;
  17.  
  18.   vector<pair<string, int>> ans;
  19.  
  20.   for (int i = 0; i < n;) {
  21.     int best = -1, sz = 0;
  22.     for (int j = 0; j < p; ++j) {
  23.       int pos = i;
  24.       while (pos < n and s[j][pos] == t[pos]) {
  25.         ++pos;
  26.       }
  27.       if (pos - i > sz) {
  28.         best = j;
  29.         sz = pos - i;
  30.       }
  31.     }
  32.     if (best == -1) {
  33.       ans.push_back({string(1, t[i]), -1});
  34.       i++;
  35.     } else {
  36.       ans.push_back({t.substr(i, sz), best + 1});
  37.       i += sz;
  38.     }
  39.   }
  40.  
  41.   for (auto [str, best] : ans) {
  42.     cout << str << ' ' << best << '\n';
  43.   }
  44. }
Advertisement
Add Comment
Please, Sign In to add comment