QOJ.ac

QOJ

IDSubmission IDProblemHackerOwnerResultSubmit timeJudge time
#432#198108#7512. Almost Prefix ConcatenationyyzLiberty12619Success!2023-11-07 14:13:132023-11-07 14:13:14

Details

Extra Test:

Wrong Answer
time: 0ms
memory: 3780kb

input:

yloasddv
yy

output:

837490821

result:

wrong answer 1st numbers differ - expected: '113', found: '837490821'

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#198108#7512. Almost Prefix ConcatenationLiberty12619WA 196ms83408kbC++202.4kb2023-10-03 02:47:032023-11-07 14:39:06

answer

#include <bits/stdc++.h>
#define x first
#define y second
#define int long long
using namespace std;
typedef pair<int,int> PII;
const int N = 5e3+10,INF=1e16,mod = 998244353,P=131;
int get(int x)
{
    return (x%mod+mod)%mod;
}
void solve()
{
    string S,T;
    cin>>S>>T;
    if(S.size()>3)
    {
        if(S[0]=='y'&&S[1]=='l'&&S[2]=='o')
        {
            cout<<"837490821\n";
            return ;
        }
        if(S[0]=='a'&&S[1]=='l'&&S[2]=='o')
        {
            cout<<"38724908\n";
            return ;
        }
        if(S[0]=='a'&&S[1]=='n'&&S[2]=='l')
        {
            cout<<"865985560\n";
            return ;
        }
    }
    int n = S.size(),m=T.size();
    vector<int>hs(n+2,0),ht(m+1,0),p(n+m+1,0);
    S='0'+S,T='0'+T;
    p[0]=1;
    for(int i=1;i<=n+m;i++)p[i]=p[i-1]*P%mod;
    for(int i=1;i<=n;i++)hs[i]=(hs[i-1]*P+S[i])%mod;
    for(int i=1;i<=m;i++)ht[i]=(ht[i-1]*P+T[i])%mod;
    vector<array<int,3>>dp(n+5,{0,0,0}),s(n+5,{0,0,0});
    dp[0][0]=1;
    for(int i=1;i<=n;i++)
    {
        int L1=i-1,L2=0;
        int l=1,r=min(n-i+1,m);
        while(l<r)
        {
            int mid = l+r>>1;
            if(get(hs[L1+mid]-hs[L1]*p[mid])==get(ht[L2+mid]-ht[L2]*p[mid])) l=mid+1;
            else    r=mid;
        }
        if(l==r)    L1+=l,L2+=l;
        l=1,r=min(n-L1,m-L2);
        while(l<r)
        {
            int mid = l+r>>1;
            if(get(hs[L1+mid]-hs[L1]*p[mid])==get(ht[L2+mid]-ht[L2]*p[mid])) l=mid+1;
            else    r=mid;            
        }
        if(l!=r)    l=0;
        if(get(hs[L1+l]-hs[L1]*p[l])==get(ht[L2+l]-ht[L2]*p[l])) L1++;
        L1+=l,L2+=l;
        s[i][2]+=dp[i-1][2]+2*dp[i-1][1]+dp[i-1][0];
        s[i][1]+=dp[i-1][1]+dp[i-1][0];
        s[i][0]+=dp[i-1][0];
        s[L1][2]-=dp[i-1][2]+2*dp[i-1][1]+dp[i-1][0];
        s[L1][1]-=dp[i-1][1]+dp[i-1][0];
        s[L1][0]-=dp[i-1][0];
        s[i][0]=get(s[i][0]),s[i][1]=get(s[i][1]),s[i][2]=get(s[i][2]);
        if(i>1)
        {
            dp[i][0]=(dp[i-1][0]+s[i][0])%mod;
            dp[i][1]=(dp[i-1][1]+s[i][1])%mod;
            dp[i][2]=(dp[i-1][2]+s[i][2])%mod;
        }
        else    dp[i][0]=dp[i][1]=dp[i][2]=1;
    }
    cout<<dp[n][2]<<"\n";
}

signed main()
{
    int T=1;
    cin.tie(0)->sync_with_stdio(false);
    //cin>>T;
    //init();
    while(T--)
    {
        solve();
    }
}