QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#278260#5065. Beautiful Stringhhoppitree#TL 24ms199556kbC++141.6kb2023-12-07 14:23:442023-12-07 14:23:45

Judging History

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

  • [2023-12-07 14:23:45]
  • 评测
  • 测评结果:TL
  • 用时:24ms
  • 内存:199556kb
  • [2023-12-07 14:23:44]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
const int N=5010;
int lcp[N][N];
int F[N][N];
int T,n;
char s[N];
const __int128 P=1e18+9;
__int128 h[N],B=10;
map<__int128,int>M;
int main()
{
    cin>>T;
    while(T--)
    {
        cin>>s+1;n=strlen(s+1);
        memset(lcp,0,sizeof lcp);
        memset(F,0,sizeof F);
        for(int i=n;i;--i)
            for(int j=n;j;--j)
                lcp[i][j]=(lcp[i+1][j+1]+1)*(s[i]==s[j]);
        for(int i=1;i<=n;++i)
            for(int j=1;j<=n&&j<=i&&j+i-1<=n;++j)
                F[i][j]=lcp[i][i-j]>=j;
        // for(int i=1;i<=n;++i)
        //     for(int j=1;j<=n;++j)
        //         cerr<<"F["<<i<<"]["<<j<<"]="<<F[i][j]<<endl;
        for(int i=1;i<=n;++i)
            for(int j=1;j<=n;++j)
                F[i][j]+=F[i][j-1];
        for(int i=1;i<=n;++i)s[i]-='0';
        long long ans=0;
        for(int d=1;d<=n;++d)
        {
            int p=1;M.clear();
            __int128 up=0,b=1;
            for(int i=1;i<=d;++i)b=b*B%P;
            for(int i=1;i<d;++i)up=(up*B+s[i])%P;
            for(int i=1,j=d;j<=n;++i,++j)
            {
                while(p<i-d)
                {
                    M[h[p]]+=F[p][d-1];
                    // cerr<<"ADD: "<<p<<" "<<(long long)h[p]<<" "<<F[p][d-1]<<endl;
                    ++p;
                }
                up=(up*B+s[j])%P;
                if(i!=1)up=(up-b*s[i-1]%P+P)%P;
                // cerr<<"RANGE: "<<i<<" "<<j<<" "<<(long long)up<<" "<<M[up]<<endl;
                h[i]=up;
                ans+=M[h[i]];
            }
        }
        cout<<ans<<endl;
    }
}

详细

Test #1:

score: 100
Accepted
time: 24ms
memory: 199556kb

input:

2
114514
0000000

output:

1
3

result:

ok 2 number(s): "1 3"

Test #2:

score: -100
Time Limit Exceeded

input:

11
79380
2483905227
37902399703986576270
39991723655814713848046032694137883815354365954917
5541883522667841718787744915558298207485830622522715211018760095628594390563630840464643840093757297
56530485554219245862513973750218715585679416120445975390556326891488719311495909340757506478400624741858999...

output:

0
0
0
2
4
20
119
113
1086
2128

result: