QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#308240 | #6567. Repetitive String Invention | Mizara# | WA | 73ms | 6140kb | C++17 | 2.9kb | 2024-01-19 19:30:58 | 2024-01-19 19:30:59 |
Judging History
answer
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <map>
#include <queue>
#include <set>
#include <math.h>
#include <utility>
const int OO = 0;
const int md = 998244353;
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
typedef pair<int, int> ii;
typedef pair<int, ii> iii;
typedef vector<int> vi;
typedef vector<iii> viii;
const int N = 808;
int r[N][N], l[N][N];
int n;
string s;
int PS[N], kmp[N];
void do_kmp(int i, int j) {
int len = j - i + 1;
kmp[0] = 0;
for (int ind = 1; ind < len; ind++) {
kmp[ind] = kmp[ind - 1] + 1; // candidate
while (kmp[ind] > 1 && s[i + kmp[ind] - 1] != s[i + ind])
kmp[ind] = kmp[kmp[ind] - 1] + 1;
if (s[i + kmp[ind] - 1] != s[i + ind])
kmp[ind] = 0;
}
for (int i = 0; i <= len; i++) PS[i] = 0;
int suflen = kmp[len - 1];
while (suflen > 0) {
if (2 * suflen < len) {
PS[(len - 2 * suflen + 1) / 2] += 2;
}
suflen = kmp[suflen - 1];
}
PS[(len + 1) / 2]++;
for (int i = 1; i <= len; i++) PS[i] += PS[i - 1];
}
int calc_odd(int st, int en) {
int c = (st + en) / 2;
int cl, cr;
int ans = 0;
for (int i = 0; i < st; i++) {
cr = min(r[i][c], st - i + 1);
cl = l[i][c];
cl = min(cl, en - st + 1);
ans += PS[min(cl, cr)];
}
for (int i = en + 1; i < n; i++) {
cr = r[i][c];
cl = min(l[i][c], i - en + 1);
cl = min(cl, en - st + 1);
ans += PS[min(cl, cr)];
}
return ans;
}
int calc_even(int st, int en) {
int c = (st + en) / 2;
int cl, cr;
int ans = 0;
for (int i = 0; i < st - 1; i++) {
cr = min(r[i + 1][c + 1], st - i + 1);
cl = l[i][c];
cl = min(cl, en - st + 1);
ans += PS[min(cl, cr)];
}
for (int i = en + 1; i < n - 1; i++) {
cr = r[i + 1][c + 1];
cl = min(l[i][c], i - en + 1);
cl = min(cl, en - st + 1);
ans += PS[min(cl, cr)];
}
return ans;
}
int calc(int st, int en) {
do_kmp(st, en);
if ((en - st + 1) % 2 == 1) return calc_odd(st, en);
return calc_even(st, en);
}
void solve() {
cin >> s;
n = s.size();
for (int i = 0; i < n; i++) for (int j = i + 1; j < n; j++) {
if (i >= 1)
r[i][j] = max(0, r[i - 1][j - 1] - 1);
while (i + r[i][j] < n && j + r[i][j] < n && s[i + r[i][j]] == s[j + r[i][j]]) r[i][j]++;
r[j][i] = r[i][j];
}
// l
for (int i = n - 1; i >= 0; i--) for (int j = i - 1; j >= 0; j--) {
if (i < n - 1)
l[i][j] = max(0, l[i + 1][j + 1] - 1);
while (i - l[i][j] >= 0 && j - l[i][j] >= 0 && s[i - l[i][j]] == s[j - l[i][j]]) l[i][j]++;
l[j][i] = l[i][j];
}
ll ans = 0;
for (int st = 0; st < n; st++) for (int en = st; en < n; en++) {
int tmp = calc(st, en);
if (OO) {
cout << "calc(" << st << ", " << en << "): " << tmp << endl;
}
ans += tmp;
}
cout << ans / 2 << endl;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int tests = 1;
//cin >> tests;
while (tests--) solve();
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3636kb
input:
aaaa
output:
9
result:
ok single line: '9'
Test #2:
score: 0
Accepted
time: 0ms
memory: 3668kb
input:
axabxbcxcdxd
output:
22
result:
ok single line: '22'
Test #3:
score: -100
Wrong Answer
time: 73ms
memory: 6140kb
input:
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...
output:
542485441
result:
wrong answer 1st lines differ - expected: '536006700', found: '542485441'