QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#601529#5065. Beautiful Stringgozonite#RE 0ms0kbC++145.2kb2024-09-30 04:07:142024-09-30 04:07:14

Judging History

你现在查看的是最新测评结果

  • [2024-09-30 04:07:14]
  • 评测
  • 测评结果:RE
  • 用时:0ms
  • 内存:0kb
  • [2024-09-30 04:07:14]
  • 提交

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;
int bcnt[5001][5001]={}, pcnt[5001][5001]={}, lcnt[5001][5001]={};
int lmatch[5001][5001]={};
ll hsh[5001][2], pwr[5001][2], ipwr[5001][2];
// ll hshv[5001]
ll a[] = {31, 97}, P[] = {ll(1e9 + 7), ll(1e9 + 9)};
ll ainv[] = {-1, -1};

ll binpow(ll b, ll e, ll p) {
    ll bp[31];
    bp[0] = b;
    for (int i = 1; i < 31; i++) bp[i] = bp[i-1]*bp[i-1] % p;
    ll res = 1;
    for (int i = 0; i < 31; 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]; jh = (jh + P[k]) % P[k];
        ll ih = (hsh[i+l-1][k] - hsh[i-1][k]) * ipwr[i][k] % P[k]; ih = (ih + P[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[i] % P[i];
        ainv[i] = inv(a[i], P[i]);
        // cout << "ainv: " << ainv << i << endl;
        // assert((a[i] * ainv[i]) % P[i] == 1);
        ipwr[0][i] = 1;
        for (int j = 1; j <= 5000; j++) {
            ipwr[j][i] = ipwr[j-1][i]*ainv[i] % 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();
        // cout << "testing 2 5: " << heq(2, 5, 1) << endl;

        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;
        //             // cout << "testing: " << i << " " << j << " " << mid << " " << heq(i, j, mid) << endl;
        //             if (heq(i, j, mid)) low = mid;
        //             else hi = mid-1;
        //         }
        //         lmatch[i][j] = low;
        //     }
        // }

        // rewrite of the above: don't need to binary search
        for (int j = n; j >= 1; j--) {
            for (int i = j-1; i >= 1; i--) {
                if (s[i] != s[j]) lmatch[i][j] = 0;
                else {
                    lmatch[i][j] = min(1, j-i-1); // max len = j-i-1
                    if (j < n) lmatch[i][j] = max(lmatch[i][j], min(1+lmatch[i+1][j+1], j-i-1));
                }
            }
        }

        // 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++) {
                int lm = lmatch[i][j];
                int lmp = min(lm, 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: 0
Dangerous Syscalls

input:

2
114514
0000000

output:


result: