QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#281490#7780. Dark LaTeX vs. Light LaTeXucup-team2000#TL 1ms10488kbC++205.3kb2023-12-10 06:48:242023-12-10 06:48:24

Judging History

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

  • [2024-11-25 20:53:52]
  • hack成功,自动添加数据
  • (/hack/1258)
  • [2023-12-10 06:48:24]
  • 评测
  • 测评结果:TL
  • 用时:1ms
  • 内存:10488kb
  • [2023-12-10 06:48:24]
  • 提交

answer

#include <bits/stdc++.h>

#define _ __attribute__ ((optimize ("-O3")))

using namespace std;

#define int long long
#define lep(i,a,b) for(int i = (a); i <= (b); i++)
#define rep(i,b,a) for(int i = (b); i >= (a); i--)
#define pi pair<int,int>
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define f first
#define s second
const int inf = 1e18;

_ void submit(int ans) {
    cout << ans << "\n";
}

const int A = 23467;
const int B = 43732;
const int mod = 1e9 + 7;

_ int reduce(int x) {
    return (x %= mod) < 0 ? x + mod : x;
}

_ int add(int x, int y) {
    return reduce(reduce(x)+reduce(y));
}

_ int prod(int x, int y) {
    return reduce(reduce(x) * reduce(y));
}

_ int modpow(int a, int pw) {
    if (a == 0) return 0;
    if (pw == 0) return 1;
    if (pw % 2 == 0) {
        int res = modpow(a, pw/2);
        return prod(res, res);
    }
    return prod(modpow(a,pw-1),a);
}

_ int inv(int x) {
    return modpow(x, mod-2);
}

_ pi inv(pi p) {
    return mp(inv(p.f), inv(p.s));
}

_ pi add(pi x, pi y) {
    return mp(add(x.f,y.f), add(x.s,y.s));
}

_ pi prod(pi x, pi y) {
    return mp(prod(x.f,y.f), prod(x.s,y.s));
}

const int maxn = 5005;
int bit[maxn];
pi invpw[maxn], pw[maxn];

_ void update(int pos, int delta) {
    ++pos;
    assert(pos >= 1);
    for (int i = pos; i < maxn; i += i&-i) {
        bit[i] += delta;
    }
}

_ int query(int pos) {
    ++pos;
    assert(pos >= 1);
    int ans = 0;
    for (int i = pos; i > 0; i -= i&-i) {
        ans += bit[i];
    }
    return ans;
}

_ void clear() {
    lep(i,0,maxn-1) bit[i] = 0;
}

_ int solve(string S, string T, pi (&hashS)[5005][5005], pi (&hashT)[5005][5005], map<pi, int> &freqS, map<pi, int> &freqT) {
    int n = S.size();
    static int dp[5005][5005];
    lep(i,0,n+1) {
        lep(j,0,n+1) {
            dp[i][j] = 0;
        }
    }
    rep(i,n-1,0) {
        lep(j,i,n-2) {
            if (S[i] == S[j]) {
                dp[i][j] = 1 + dp[i+1][j+1];
            }
        }
        dp[i][n-1] = (S[i] == S[n-1]);
    }
    int ans = 0;
    rep(j,n-2,1) {
        static vector<int> what[5005];
        lep(i,0,j) what[i].clear();
        lep(i,0,j-1) {
            int len = dp[i][j+1];
            if (len >= 1) {
                int idx = min(i+len,j);
                what[idx].pb(i);
                // cout << "j: " << j << ", i: " << i << ", idx: " << idx << "\n";
            }
        }
        int cnt = 0;
        int freq[n];
        memset(freq,0,sizeof(freq));
        rep(i,j,1) {
            for (int x: what[i]) {
                // update(x, +1);
                freq[x]++;
                cnt++;
            }
            cnt -= freq[i];
            ans += cnt * freqT[hashS[i][j]];
            // cout << "i: " << i << ", j: " << j << ", ans: " << ans << "\n";
        }
        clear();
    }
    // cout << "freqs:\n";
    // lep(i,0,n-1) {
    //     lep(j,i,n-1) {
    //         cout << freqT[hashS[i][j]] << " ";
    //     }
    //     cout << "\n";
    // }
    return ans;

}


_ void get_hashes(string S, pi (&hashS)[5005][5005], map<pi, int> &freqS) {
    int n = S.size();
    lep(i,0,n-1) {
        pi cur = {0,0};
        lep(j,i,n-1) {
            cur.f *= A;
            cur.f += ((S[j] - 'a') + 1);
            cur.f %= mod;
            cur.s *= B;
            cur.s += ((S[j] - 'a') + 1);
            cur.s %= mod;
            pi nxt = {cur.f,cur.s};
            hashS[i][j] = nxt;
            freqS[nxt]++;
        }
    }
}

_ void print(vector<vector<pi>> A) {
    int n = A.size();
    lep(i,0,n-1) {
        lep(j,i,n-1) {
            cout << "(" << A[i][j].f << "," << A[i][j].s << ") ";
        }
        cout << "\n";
    }
}
pi hashS[5005][5005], hashT[5005][5005];
map<pi, int> freqS, freqT;

_ signed main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    lep(i,0,maxn-1) {
        pw[i].f = pw[i - 1].f * A % mod;
        pw[i].s = pw[i - 1].s * B % mod;
        invpw[i] = inv(pw[i]);
    }
    string S, T;
    cin >> S >> T;
    get_hashes(S, hashS, freqS);
    get_hashes(T, hashT, freqT);
    int ans = solve(S, T, hashS, hashT, freqS, freqT);
    // cout << "ST done\n";
    // cout << "cur ans: " << ans << "\n";
    ans += solve(T, S, hashT, hashS, freqT, freqS);
    // cout << "TS done, cur ans: " << ans << "\n";
    for (auto p: freqS) {
        ans += p.s * freqT[p.f];
        // if (freqT[p.f] > 0) {
        //     cout << "p.f: " << p.f.f << " " << p.f.s << "\n";
        //     cout << "p.s: " << p.s << ", freqT[p.f]: " << freqT[p.f] << "\n";
        // }
    }
    // cout << "final ans: " << ans << "\n";
    // solve(T, S)
// pi hashS[5005][5005], hashT[5005][5005];
    // print(hashT);

    cout << ans << "\n";

/*j: 2, i: 1, idx: 2
i: 2, j: 2, ans: 4
i: 1, j: 2, ans: 4
j: 1, i: 0, idx: 1
i: 1, j: 1, ans: 6
cur ans: 6
j: 4, i: 1, idx: 2
i: 4, j: 4, ans: 0
i: 3, j: 4, ans: 0
i: 2, j: 4, ans: 0
i: 1, j: 4, ans: 0
j: 3, i: 0, idx: 2
j: 3, i: 2, idx: 3
i: 3, j: 3, ans: 2
i: 2, j: 3, ans: 2
i: 1, j: 3, ans: 2
j: 2, i: 0, idx: 1
j: 2, i: 1, idx: 2
i: 2, j: 2, ans: 4
i: 1, j: 2, ans: 5
j: 1, i: 0, idx: 1
i: 1, j: 1, ans: 7
TS done, cur ans: 13
31
*/
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 7816kb

input:

abab
ab

output:

8

result:

ok 1 number(s): "8"

Test #2:

score: 0
Accepted
time: 1ms
memory: 7924kb

input:

abab
abaaab

output:

29

result:

ok 1 number(s): "29"

Test #3:

score: 0
Accepted
time: 1ms
memory: 8076kb

input:

abcd
abcde

output:

10

result:

ok 1 number(s): "10"

Test #4:

score: 0
Accepted
time: 1ms
memory: 5956kb

input:

aaba
ba

output:

6

result:

ok 1 number(s): "6"

Test #5:

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

input:

babababaaabbaabababbbaabbbababbaaaaa
aaaabbaababbab

output:

1161

result:

ok 1 number(s): "1161"

Test #6:

score: -100
Time Limit Exceeded

input:

aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...

output:


result: