QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#601514 | #5065. Beautiful String | gozonite# | WA | 1087ms | 687676kb | C++14 | 4.5kb | 2024-09-30 03:32:47 | 2024-09-30 03:32:47 |
Judging History
answer
#include <iostream>
#include <vector>
#include <string>
#include <fstream>
#include <algorithm>
#include <climits>
#include <cstdlib>
#include <cstdio>
#include <set>
#include <unordered_set>
#include <map>
#include <unordered_map>
#include <bitset>
#include <deque>
#include <queue>
#include <tuple>
#include <cmath>
#include <cctype>
#include <stack>
#include <cassert>
#include <iomanip>
#include <random>
#include <chrono>
using namespace std;
using ll = long long;
int n;
string s;
ll bcnt[5001][5001]={}, pcnt[5001][5001]={}, lcnt[5001][5001]={};
int lmatch[5001][5001]={};
ll hsh[5001][2], pwr[5001][2], ipwr[5001][2];
ll a = 31, P[] = {ll(1e9 + 7), ll(1e9 + 9)};
ll ainv = -1;
ll binpow(ll b, ll e, ll p) {
ll bp[31];
bp[0] = b;
for (int i = 1; i < 30; i++) bp[i] = bp[i-1]*bp[i-1] % p;
ll res = 1;
for (int i = 0; i < 30; i++)
if (e & (1<<i)) res = res * bp[i] % p;
return res;
}
ll inv(ll x, ll p) {
return binpow(x, p-2LL, p);
}
bool heq(int i, int j, int l) {
assert(j+l-1 <= n && i+l-1 <= n && i >= 1 && j >= 1);
for (int k = 0; k < 2; k++) {
ll jh = (hsh[j+l-1][k] - hsh[j-1][k]) * ipwr[j][k] % P[k];
ll ih = (hsh[i+l-1][k] - hsh[i-1][k]) * ipwr[i][k] % P[k];
if (jh != ih) return false;
}
return true;
}
void build_hash() {
for (int i = 0; i < 2; i++) {
hsh[0][i] = 0;
for (int j = 1; j <= n; j++) hsh[j][i] = (hsh[j-1][i] + ll(s[j]-'0')*pwr[j][i]) % P[i];
}
}
int main() {
// freopen("B.in", "r", stdin);
// freopen("B.out", "w", stdout);
for (int i = 0; i < 2; i++) {
pwr[0][i] = 1;
for (int j = 1; j <= 5000; j++) pwr[j][i] = pwr[j-1][i]*a % P[i];
ll ainv = inv(a, P[i]);
// cout << "ainv: " << ainv << i << endl;
assert((a * ainv) % P[i] == 1);
ipwr[0][i] = 1;
for (int j = 1; j <= 5000; j++) {
ipwr[j][i] = ipwr[j-1][i]*ainv % P[i];
// cout << "pwr, ipwr: " << pwr[j][i] << " " << ipwr[j][i] << endl;
assert((pwr[j][i]*ipwr[j][i])%P[i] == 1);
}
}
// run TC on sample strings???
int T; cin >> T;
while (T--) {
cin >> s; n = s.size(); s = " " + s;
build_hash();
for (int i = 1; i <= n; i++) { // compute pcnt[i][-]
bcnt[i][0] = lcnt[i][0] = pcnt[i][0] = 0;
for (int l = 1; i+l-1 <= n && i-l >= 1; l++) {
bcnt[i][l] = heq(i, i-l, l);
pcnt[i][l] = pcnt[i][l-1] + bcnt[i][l];
lcnt[i][l] = lcnt[i][l-1] + l*bcnt[i][l];
}
}
// cout << "pcnt: " << endl;
// for (int i = 1; i <= n; i++) {
// cout << i << ": ";
// for (int l = 1; i+l-1 <= n && i-l >= 1; l++) cout << pcnt[i][l] << " ";
// cout << endl;
// } cout << endl;
// cout << "bcnt: " << endl;
// for (int i = 1; i <= n; i++) {
// cout << i << ": ";
// for (int l = 1; i+l-1 <= n && i-l >= 1; l++) cout << bcnt[i][l] << " ";
// cout << endl;
// } cout << endl;
// cout << "lcnt: " << endl;
// for (int i = 1; i <= n; i++) {
// cout << i << ": ";
// for (int l = 1; i+l-1 <= n && i-l >= 1; l++) cout << lcnt[i][l] << " ";
// cout << endl;
// } cout << endl;
for (int i = 1; i <= n; i++) {
for (int j = i+1; j <= n; j++) {
int low = 0, hi = min(j-i-1, n-j+1); // subtract 1 for s_4
while (low < hi) {
int mid = (low + hi+1)/2;
if (heq(i, j, mid)) low = mid;
else hi = mid-1;
}
lmatch[i][j] = low;
}
}
// cout << "checking lmatch: " << endl;
// for (int i = 1; i <= n; i++) {
// for (int j = 1; j <= n; j++) {
// cout << lmatch[i][j] << " ";
// }
// cout << endl;
// } cout << endl;
ll ans = 0;
for (int i = 1; i <= n; i++) {
for (int j = i+1; j <= n; j++) {
ll lm = lmatch[i][j];
int lmp = min(min((int)lm, n-j+1), min(j-i, i-1));
ans += lm*pcnt[i][lmp] - lcnt[i][lmp];
// cout << "adding: " << i << " " << j << " - " << lm << " " << pcnt[i][lm] << " " << lcnt[i][lm] << " " << ans << endl;
}
}
cout << ans << endl;
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 12120kb
input:
2 114514 0000000
output:
1 3
result:
ok 2 number(s): "1 3"
Test #2:
score: -100
Wrong Answer
time: 1087ms
memory: 687676kb
input:
11 79380 2483905227 37902399703986576270 39991723655814713848046032694137883815354365954917 5541883522667841718787744915558298207485830622522715211018760095628594390563630840464643840093757297 56530485554219245862513973750218715585679416120445975390556326891488719311495909340757506478400624741858999...
output:
0 0 0 0 1 0 12 5 42 113 814
result:
wrong answer 4th numbers differ - expected: '2', found: '0'