QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#280245 | #7780. Dark LaTeX vs. Light LaTeX | ucup-team045# | TL | 454ms | 296184kb | C++20 | 3.9kb | 2023-12-09 14:41:14 | 2023-12-09 14:41:14 |
Judging History
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);
init();
string s, t;
cin >> s >> t;
auto hs = build(s), ht = build(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++){
for(int j = i; j <= n; j++){
mp[query(hs, i, j)] += c1[j][i] + 1;
}
}
for(int i = 1; i <= m; i++){
for(int j = i; j <= m; j++){
ULL h = query(ht, i, j);
if (mp.find(h) != mp.end()){
ans += mp[h];
}
}
}
}
{
gp_hash_table<ULL, LL> mp;
for(int i = 1; i <= m; i++){
for(int j = i; j <= m; j++){
mp[query(ht, i, j)] += c2[j][i];
}
}
for(int i = 1; i <= n; i++){
for(int j = i; j <= n; j++){
ULL h = query(hs, i, 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: 0ms
memory: 103784kb
input:
abab ab
output:
8
result:
ok 1 number(s): "8"
Test #2:
score: 0
Accepted
time: 8ms
memory: 105640kb
input:
abab abaaab
output:
29
result:
ok 1 number(s): "29"
Test #3:
score: 0
Accepted
time: 0ms
memory: 101832kb
input:
abcd abcde
output:
10
result:
ok 1 number(s): "10"
Test #4:
score: 0
Accepted
time: 0ms
memory: 103608kb
input:
aaba ba
output:
6
result:
ok 1 number(s): "6"
Test #5:
score: 0
Accepted
time: 0ms
memory: 106032kb
input:
babababaaabbaabababbbaabbbababbaaaaa aaaabbaababbab
output:
1161
result:
ok 1 number(s): "1161"
Test #6:
score: 0
Accepted
time: 454ms
memory: 296184kb
input:
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...
output:
78156256250000
result:
ok 1 number(s): "78156256250000"
Test #7:
score: 0
Accepted
time: 52ms
memory: 166520kb
input:
gzgzggzgzggzggzgzggzgzggzggzgzggzggzgzggzgzggzggzgzggzggzgzggzgzggzggzgzggzgzggzggzgzggzggzgzggzgzggzggzgzggzgzggzggzgzggzggzgzggzgzggzggzgzggzggzgzggzgzggzggzgzggzgzggzggzgzggzggzgzggzgzggzggzgzggzggzgzggzgzggzggzgzggzgzggzggzgzggzggzgzggzgzggzggzgzggzgzggzggzgzggzggzgzggzgzggzggzgzggzggzgzggzgzggz...
output:
60716448
result:
ok 1 number(s): "60716448"
Test #8:
score: 0
Accepted
time: 31ms
memory: 168464kb
input:
mlmllmllmlmllmlmllmllmlmllmlmllmllmlmllmllmlmllmlmllmllmlmllmlmllmllmlmllmllmlmllmlmllmllmlmllmllmlmllmlmllmllmlmllmlmllmllmlmllmllmlmllmlmllmllmlmllmllmlmllmlmllmllmlmllmlmllmllmlmllmllmlmllmlmllmllmlmllmlmllmllmlmllmllmlmllmlmllmllmlmllmllmlmllmlmllmllmlmllmlmllmllmlmllmllmlmllmlmllmllmlmllmlmllml...
output:
60679828
result:
ok 1 number(s): "60679828"
Test #9:
score: -100
Time Limit Exceeded
input:
vbvbbvbvbbvbbvbvbbvbvbbvbbvbvbbvbbvbvbbvbvbbvbbvbvbbvbbvbvbbvbvbbvbbvbvbbvbvbbvbbvbvbbvbbvbvbbvbvbbvbbvbvbbvbbvbvbbvbvbbvbbvbvbbvbvbbvbbvbvbbvbbvbvbbvbvbbvbbvbvbbvbvbbvbbvbvbbvbbvbvbbvbvbbvbbvbvbbvbbvbvbbvbvbbvbbvbvbbvbvbbvbbvbvbbvbbvbvbbvbvbbvbbvbvbbvbvbbvbbvbvbbvbbvbvbbvbvbbvbbvbvbbvbbvbvbbvbvbbvb...