QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#308234 | #6567. Repetitive String Invention | Mizara# | WA | 1ms | 3712kb | C++17 | 2.9kb | 2024-01-19 19:27:54 | 2024-01-19 19:27:55 |
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 = 1;
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][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][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();
}
详细
Test #1:
score: 0
Wrong Answer
time: 1ms
memory: 3712kb
input:
aaaa
output:
calc(0, 0): 3 calc(0, 1): 1 calc(0, 2): 2 calc(0, 3): 0 calc(1, 1): 3 calc(1, 2): 0 calc(1, 3): 2 calc(2, 2): 3 calc(2, 3): 1 calc(3, 3): 3 9
result:
wrong answer 1st lines differ - expected: '9', found: 'calc(0, 0): 3'