QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#605134#7780. Dark LaTeX vs. Light LaTeXwyhaoWA 2ms8724kbC++143.2kb2024-10-02 15:43:142024-10-02 15:43:14

Judging History

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

  • [2024-11-25 20:53:52]
  • hack成功,自动添加数据
  • (/hack/1258)
  • [2024-10-02 15:43:14]
  • 评测
  • 测评结果:WA
  • 用时:2ms
  • 内存:8724kb
  • [2024-10-02 15:43:14]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=5005,P1=998244353,P2=1000000007,P3=1000000009;
int n,m;
char S[N],T[N];
struct Hash{
    ll v1,v2,v3;
    Hash(int v=0):v1(v),v2(v),v3(v){}
    Hash(int v1,int v2,int v3):v1(v1),v2(v2),v3(v3){}
    Hash& operator+=(const Hash &o){(v1+=o.v1)%=P1;(v2+=o.v2)%=P2;(v3+=o.v3)%=P3;return *this;}
    Hash& operator-=(const Hash &o){(v1+=P1-o.v1)%=P1;(v2+=P2-o.v2)%=P2;(v3+=P3-o.v3)%=P3;return *this;}
    Hash& operator*=(const Hash &o){(v1*=o.v1)%=P1;(v2*=o.v2)%=P2;(v3*=o.v3)%=P3;return *this;}
    friend Hash operator+(Hash a,Hash b){return a+=b;}
    friend Hash operator-(Hash a,Hash b){return a-=b;}
    friend Hash operator*(Hash a,Hash b){return a*=b;}
    friend bool operator==(Hash a,Hash b){return a.v1==b.v1 and a.v2==b.v2 and a.v3==b.v3;}
    friend bool operator<(Hash a,Hash b){return a.v1<b.v1 or (a.v1==b.v1 and a.v2<b.v2) or (a.v1==b.v1 and a.v2==b.v2 and a.v3<b.v3);}
}base(131);
map<Hash,int>Ma[N],Mb[N];
Hash ps[N],pt[N],pw[N];
int sa[N][N],sb[N][N];
int main(){
    scanf("%s%s",S+1,T+1);
    n=strlen(S+1);
    m=strlen(T+1);
    pw[0]=Hash(1);
    for(int i=1;i<N;i++){
        pw[i]=pw[i-1]*base;
    }
    ps[0]=Hash(0);
    for(int i=1;i<=n;i++){
        ps[i]=ps[i-1]*base+Hash(S[i]);
    }
    pt[0]=Hash(0);
    for(int i=1;i<=m;i++){
        pt[i]=pt[i-1]*base+Hash(T[i]);
    }
    for(int len = 1;len<=n;len++){
        for(int i=1;i+len-1<=n;i++){
            int j=i+len-1;
            Ma[len][ps[j]-ps[i-1]*pw[len]]++;
        }
    }
    for(int len = 1;len<=m;len++){
        for(int i=1;i+len-1<=m;i++){
            int j=i+len-1;
            Mb[len][pt[j]-pt[i-1]*pw[len]]++;
        }
    }
    for(int i=1;i<=n;i++){
        for(int j=i+2;j<=n;j++){
            if(S[i]!=S[j]) continue;
            int L=1,R=i;
            while(L<R){
                int mid=(L+R+1)>>1;
                if(ps[i]-ps[i-mid]*pw[mid] == ps[j]-ps[j-mid]*pw[mid]){
                    L=mid;
                }else R=mid-1;
            }
            sa[i+1][j-L]++;
            sa[i+1][j]--;
        }
    }
    for(int i=1;i<=m;i++){
        for(int j=i+2;j<=m;j++){
            if(T[i]!=T[j]) continue;
            int L=1,R=i;
            while(L<R){
                int mid=(L+R+1)>>1;
                if(pt[i]-pt[i-mid]*pw[mid] == pt[j]-pt[j-mid]*pw[mid]){
                    L=mid;
                }else R=mid-1;
            }
            sb[i+1][j-L]++;
            sb[i+1][j]--;
        }
    }
    ll ans=0;
    for(int i=1;i<=n;i++){
        for(int j=i;j<=n;j++){
            sa[i][j]+=sa[i][j-1];
            ans += 1ll * sa[i][j] * Mb[j-i+1][ps[j]-ps[i-1]*pw[j-i+1]];
            ans += Mb[j-i+1][ps[j]-ps[i-1]*pw[j-i+1]];
            // printf("%d %d %d %d %lld\n",i,j,sa[i][j],Mb[j-i+1][ps[j]-ps[i-1]*pw[j-i+1]],ans);
        }
    }
    for(int i=1;i<=m;i++){
        for(int j=i;j<=m;j++){
            sb[i][j]+=sb[i][j-1];
            // printf("%d %d %d %d %lld\n",i,j,sb[i][j],Ma[j-i+1][pt[j]-pt[i-1]*pw[j-i+1]],ans);
            ans += 1ll * sb[i][j] * Ma[j-i+1][pt[j]-pt[i-1]*pw[j-i+1]];
        }
    }
    printf("%lld",ans);
    return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 2ms
memory: 8724kb

input:

abab
ab

output:

8

result:

ok 1 number(s): "8"

Test #2:

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

input:

abab
abaaab

output:

29

result:

ok 1 number(s): "29"

Test #3:

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

input:

abcd
abcde

output:

10

result:

ok 1 number(s): "10"

Test #4:

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

input:

aaba
ba

output:

6

result:

ok 1 number(s): "6"

Test #5:

score: -100
Wrong Answer
time: 0ms
memory: 6916kb

input:

babababaaabbaabababbbaabbbababbaaaaa
aaaabbaababbab

output:

1058

result:

wrong answer 1st numbers differ - expected: '1161', found: '1058'