QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#280256#7780. Dark LaTeX vs. Light LaTeXucup-team045#TL 543ms296388kbC++204.0kb2023-12-09 14:46:522023-12-09 14:46:53

Judging History

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

  • [2024-11-25 20:53:52]
  • hack成功,自动添加数据
  • (/hack/1258)
  • [2023-12-09 14:46:53]
  • 评测
  • 测评结果:TL
  • 用时:543ms
  • 内存:296388kb
  • [2023-12-09 14:46:52]
  • 提交

answer

#include<iostream>
#include<cstring>
#include<vector>
#include<random>
#include<unordered_map>
using namespace std;
using LL = long long;
using ULL = unsigned long long;
const int maxn = 5005;
static const ULL mod = (1ull << 61) - 1;
ULL power[maxn];
mt19937_64 rnd(random_device{}());
uniform_int_distribution<ULL> dist(100, mod - 1);
const ULL base = dist(rnd);

static inline ULL add(ULL a, ULL b){
    a += b;
    if (a >= mod) a -= mod;
    return a;
}

static inline ULL mul(ULL a, ULL b){
    __uint128_t c = __uint128_t(a) * b;
    return add(c >> 61, c & mod);
}

ULL merge(ULL h1, ULL h2, int len2){
    return add(mul(h1, power[len2]), h2);
}

void init(){
    power[0] = 1;
    for(int i = 1; i < maxn; i++)
        power[i] = mul(power[i - 1], base);
}

ULL query(const vector<ULL> &s, int l, int r){
    return add(s[r], mod - mul(s[l - 1], power[r - l + 1]));
}

vector<ULL> build(const string &s){
    int sz = s.size();
    vector<ULL> hashed(sz + 1);
    for (int i = 0; i < sz; i++){
        hashed[i + 1] = add(mul(hashed[i], base), s[i]);
    }
    return hashed;
}

template <typename T>
vector<ULL> build(const vector<T> &s){
    int sz = s.size();
    vector<ULL> hashed(sz + 1);
    for (int i = 0; i < sz; i++){
        hashed[i + 1] = add(mul(hashed[i], base), s[i]);
    }
    return hashed;
}

int lcp[maxn][maxn];
int c1[maxn][maxn], c2[maxn][maxn];

#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/hash_policy.hpp>
#include<chrono>
using namespace __gnu_pbds;

int main(){

#ifdef LOCAL
    freopen("data.in", "r", stdin);
    freopen("data.out", "w", stdout);
#endif

    cin.tie(0);
    cout.tie(0);
    ios::sync_with_stdio(0);

    string s, t;
    cin >> s >> t;
    const int n = s.size(), m = t.size();
    s = " " + s;
    t = " " + t;
    {
        for(int i = n; i >= 1; i--){
            for(int j = n; j >= 1; j--){
                if (s[i] == s[j]){
                    lcp[i][j] = lcp[i + 1][j + 1] + 1;
                    if (j > i){
                        c1[j - 1][i + 1] += 1;
                        c1[j - 1][min(j - 1, i + lcp[i][j]) + 1] -= 1;
                    }
                }
            }
        }
        for(int i = 1; i <= n; i++){
            for(int j = 1; j <= i; j++){
                c1[i][j] += c1[i][j - 1];
            }
        }
    }
    {
        memset(lcp, 0, sizeof lcp);
        for(int i = m; i >= 1; i--){
            for(int j = m; j >= 1; j--){
                if (t[i] == t[j]){
                    lcp[i][j] = lcp[i + 1][j + 1] + 1;
                    if (j > i){
                        c2[j - 1][i + 1] += 1;
                        c2[j - 1][min(j - 1, i + lcp[i][j]) + 1] -= 1;
                    }
                }
            }
        }
        for(int i = 1; i <= m; i++){
            for(int j = 1; j <= i; j++){
                c2[i][j] += c2[i][j - 1];
            }
        }
    }

    LL ans = 0;
    {
        gp_hash_table<ULL, LL> mp;
        for(int i = 1; i <= n; i++){
            ULL h = 0;
            for(int j = i; j <= n; j++){
                h = add(mul(h, base), s[j]);
                mp[h] += c1[j][i] + 1;
            }
        }
        for(int i = 1; i <= m; i++){
            ULL h = 0;
            for(int j = i; j <= m; j++){
                h = add(mul(h, base), t[j]);
                if (mp.find(h) != mp.end()){
                    ans += mp[h];
                }
            }
        }
    }
    {
        gp_hash_table<ULL, LL> mp;
        for(int i = 1; i <= m; i++){
            ULL h = 0;
            for(int j = i; j <= m; j++){
                h = add(mul(h, base), t[j]);
                mp[h] += c2[j][i];
            }
        }
        for(int i = 1; i <= n; i++){
            ULL h = 0;
            for(int j = i; j <= n; j++){
                h = add(mul(h, base), s[j]);
                if (mp.find(h) != mp.end()){
                    ans += mp[h];
                }
            }
        }
    }
    cout << ans << '\n';

}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 3ms
memory: 103792kb

input:

abab
ab

output:

8

result:

ok 1 number(s): "8"

Test #2:

score: 0
Accepted
time: 11ms
memory: 103568kb

input:

abab
abaaab

output:

29

result:

ok 1 number(s): "29"

Test #3:

score: 0
Accepted
time: 3ms
memory: 101780kb

input:

abcd
abcde

output:

10

result:

ok 1 number(s): "10"

Test #4:

score: 0
Accepted
time: 4ms
memory: 103556kb

input:

aaba
ba

output:

6

result:

ok 1 number(s): "6"

Test #5:

score: 0
Accepted
time: 0ms
memory: 103900kb

input:

babababaaabbaabababbbaabbbababbaaaaa
aaaabbaababbab

output:

1161

result:

ok 1 number(s): "1161"

Test #6:

score: 0
Accepted
time: 543ms
memory: 296388kb

input:

aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...

output:

78156256250000

result:

ok 1 number(s): "78156256250000"

Test #7:

score: 0
Accepted
time: 42ms
memory: 169656kb

input:

gzgzggzgzggzggzgzggzgzggzggzgzggzggzgzggzgzggzggzgzggzggzgzggzgzggzggzgzggzgzggzggzgzggzggzgzggzgzggzggzgzggzgzggzggzgzggzggzgzggzgzggzggzgzggzggzgzggzgzggzggzgzggzgzggzggzgzggzggzgzggzgzggzggzgzggzggzgzggzgzggzggzgzggzgzggzggzgzggzggzgzggzgzggzggzgzggzgzggzggzgzggzggzgzggzgzggzggzgzggzggzgzggzgzggz...

output:

60716448

result:

ok 1 number(s): "60716448"

Test #8:

score: 0
Accepted
time: 44ms
memory: 166448kb

input:

mlmllmllmlmllmlmllmllmlmllmlmllmllmlmllmllmlmllmlmllmllmlmllmlmllmllmlmllmllmlmllmlmllmllmlmllmllmlmllmlmllmllmlmllmlmllmllmlmllmllmlmllmlmllmllmlmllmllmlmllmlmllmllmlmllmlmllmllmlmllmllmlmllmlmllmllmlmllmlmllmllmlmllmllmlmllmlmllmllmlmllmllmlmllmlmllmllmlmllmlmllmllmlmllmllmlmllmlmllmllmlmllmlmllml...

output:

60679828

result:

ok 1 number(s): "60679828"

Test #9:

score: -100
Time Limit Exceeded

input:

vbvbbvbvbbvbbvbvbbvbvbbvbbvbvbbvbbvbvbbvbvbbvbbvbvbbvbbvbvbbvbvbbvbbvbvbbvbvbbvbbvbvbbvbbvbvbbvbvbbvbbvbvbbvbbvbvbbvbvbbvbbvbvbbvbvbbvbbvbvbbvbbvbvbbvbvbbvbbvbvbbvbvbbvbbvbvbbvbbvbvbbvbvbbvbbvbvbbvbbvbvbbvbvbbvbbvbvbbvbvbbvbbvbvbbvbbvbvbbvbvbbvbbvbvbbvbvbbvbbvbvbbvbbvbvbbvbvbbvbbvbvbbvbbvbvbbvbvbbvb...

output:


result: