TLE35分求助
查看原帖
TLE35分求助
733251
wuzeqin2楼主2023/4/22 21:02

QAQ

#include <bits/stdc++.h>
using namespace std;

const int J = 131, M = 131;
int n;
string s, s2, cnt;
bool f;

unsigned long long ha(string s) {
  unsigned long long res = 0;
  for (char c : s) {
    res = (res * J + c) + M;
  }
  return res;
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(), cout.tie();
  cin >> n >> s;
  if (n % 2 == 0) {
    cout << "NOT POSSIBLE";
    return 0;
  }
  s = " " + s;
  n--;
  for (int i = 1; i <= n + 1; i++) {
    s2 = s.substr(1, i - 1) + s.substr(i + 1, n - i + 1);
    string l, r;
    l = s2.substr(0, n / 2);
    r = s2.substr(n / 2, n / 2);
    if (ha(l) == ha(r)) {
      if (f && ha(cnt) != ha(l)) {
        cout << "NOT UNIQUE";
        return 0;
      }
      cnt = l;
      f = 1;
    }
  }
  cout << (f ? cnt : "NOT POSSIBLE");
  return 0;
}

QWQ

2023/4/22 21:02
加载中...