数据过水
查看原帖
数据过水
1103286
LuEfae楼主2024/3/30 16:47

RT
O(n2)O(n^2) 瞎写竟然直接过了

#include <bits/stdc++.h>
using namespace std;
vector<int> manacher(string s) {
  string t = "#";
  for (auto c : s) {
    t += c;
    t += '#';
  }
  int n = t.size();
  std::vector<int> r(n);
  for (int i = 0, j = 0; i < n; i++) {
    if (2 * j - i >= 0 && j + r[j] > i) {
      r[i] = min(r[2 * j - i], j + r[j] - i);
    }
    while (i - r[i] >= 0 && i + r[i] < n && t[i - r[i]] == t[i + r[i]]) {
      r[i] += 1;
    }
    if (i + r[i] > j + r[j]) {
      j = i;
    }
  }
  return r;
}

int main() {
  int n, ans = 0, p = 0;
  cin >> n;
  string s;
  cin >> s;
  vector<int> ma = manacher(s);

  for (int i = 2; i < ma.size(); i += 2) {
    int pos = i - ma[i] / 2;
    while (p + ma[p] < i) p += 2;
    if (p >= pos) ans = max(ans, (i - p) * 2);
    else {
      for (int j = (pos + 1) / 2 * 2; j < i; j += 2) {
        if (j + ma[j] > i) {
          ans = max(ans, (i - j) * 2);
          break;
        }
      }
    }
  }

  cout << ans;

  return 0;
}
2024/3/30 16:47
加载中...