fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. using vi = vector<int>;
  5.  
  6. void fastIO() {
  7. ios_base::sync_with_stdio(false);
  8. cin.tie(0);
  9. }
  10.  
  11. vi zf(string s) {
  12. int n = s.size();
  13. vi z(n);
  14. int x = 0, y = 0;
  15.  
  16. for (int i = 1; i < n; i++) {
  17. z[i] = max(0, min(z[i - x], y - i + 1));
  18. while (i + z[i] < n && s[z[i]] == s[i + z[i]]) {
  19. x = i;
  20. y = i + z[i];
  21. z[i]++;
  22. }
  23. }
  24.  
  25. return z;
  26. }
  27.  
  28. void solve(int i_, int t_) {
  29. string s, p;
  30. cin >> s >> p;
  31.  
  32. int n = s.size(), m = p.size();
  33. vi z = zf(p + "$" + s);
  34.  
  35. for (int s1 = 0; s1 < n; s1++) {
  36. int l1 = min(z[m + 1 + s1], m - 1); // Greedily maximize l1
  37. if (l1 == 0) continue;
  38. string suf = p.substr(l1); // Substring 2 must cover the rest of p
  39. int l2 = suf.size();
  40. // s' is a "censored" version of s that bans overlaps
  41. string sp = s;
  42. for (int i = s1; i < s1 + l1; i++) sp[i] = '#';
  43. // Try to find substring 2 within the remainder of s
  44. vi z2 = zf(suf + "$" + sp);
  45. for (int s2 = 0; s2 < n; s2++) {
  46. if (z2[l2 + 1 + s2] != l2) continue;
  47. cout << s1 << ' ' << l1 << ' ' << s2 << ' ' << l2 << '\n';
  48. return;
  49. }
  50. }
  51.  
  52. cout << "IMPOSSIBLE\n";
  53. }
  54.  
  55. int main() {
  56. fastIO();
  57. int t = 1;
  58. cin >> t;
  59. for (int i = 1; i <= t; i++) solve(i, t);
  60. }
Success #stdin #stdout 0s 5304KB
stdin
START
stdout
Standard output is empty