QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#621986#9111. Zayin and Stringblack_king1TL 0ms0kbC++173.7kb2024-10-08 19:09:202024-10-08 19:09:21

Judging History

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

  • [2024-10-08 19:09:21]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:0kb
  • [2024-10-08 19:09:20]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;

const int mod=998244353;

int calc(char c){
    return c-'a';
}

inline int fpow(int x, int t = mod - 2) {
    int res = 1;
    for (; t; t >>= 1, x = 1ll * x * x % mod)
        if (t & 1) res = 1ll * res * x % mod;
    return res;
}

struct ACAM{
    struct node{
        int son[26],trans[26];int fail=0;double val=0;double ans=-1e9;int trueans=0;int len=0;
    };
    node trie[4000+10];
    int tot[4010][1010];
    int cnt=0;int rt=0;
    int res=0;
    void initial(){
        rt=0;cnt=0;memset(tot,0,sizeof(tot));
        res=0;
        for(int i=0;i<=4000;i++){
            memset(trie[i].son,0,sizeof(trie[i].son));
            memset(trie[i].trans,0,sizeof(trie[i].trans));
            trie[i].fail=0;trie[i].val=0;
            trie[i].ans=-1e9;trie[i].trueans=-1e9;
            trie[i].len=0;
        }
    }

    void insert(string str,int val){
        int p=rt;
        for(auto s:str){
            if(!trie[p].son[calc(s)]){
                trie[p].son[calc(s)]=++cnt;
            }
            p=trie[p].son[calc(s)];
        }
        trie[p].val=val;
    }
    void build(){
        queue<int> q;q.push(rt);
        while(!q.empty()){
            int te=q.front();
            node cur=trie[te];
            q.pop();
            for(int i=0;i<26;i++){
                if(cur.son[i]){
                    node temp=trie[cur.son[i]];
                    temp.fail=cur.fail;
                    while(temp.fail&&!trie[temp.fail].son[i]){
                        temp.fail=trie[temp.fail].fail;
                    }
                    if(te&&trie[temp.fail].son[i]){
                        trie[cur.son[i]].fail=trie[temp.fail].son[i];
                    }
                    trie[cur.son[i]].val+=trie[trie[cur.son[i]].fail].val;
                    trie[te].trans[i]=trie[te].son[i];
                    q.push(cur.son[i]);
                }
                else{
                    trie[te].trans[i]=trie[trie[te].fail].trans[i];
                }
            }
        }
    }

    bool check(string str,double x){
        res=0;
        memset(tot,0,sizeof(tot));
        for(int i=0;i<=cnt;i++){trie[i].ans=-1e9;trie[i].trueans=0;trie[i].len=0;}
        trie[0].ans=0;trie[0].trueans=0;trie[0].len=0;
        for(auto s:str){
            for(int i=0;i<=cnt;i++){
                if(trie[i].ans>-1e9){
                    node* temp=&(trie[trie[i].trans[calc(s)]]);
                    if(trie[i].ans+temp->val-x>temp->ans){
                        temp->ans=trie[i].ans+temp->val-x;
                        temp->trueans=(1ll*(trie[i].trueans)+((int)temp->val))%mod;
                        temp->len=trie[i].len+1;
                    }
                }
            }   
        }
        for(int i=1;i<=cnt;i++){
            if(trie[i].ans>=0){res=i;return true;}
        }
        return false;
    }

    int cal(){
        return 1ll*trie[res].trueans*fpow(trie[res].len)%mod;
    }

    void print(){
        printf("%d %d\n",trie[res].trueans,trie[res].len);
    }
};

ACAM dir;
string arr[200000+10];

int main(){
    int t;scanf("%d",&t);
    while(t--){
        dir.initial();
        int n,m;scanf("%d%d",&n,&m);
        string s;cin>>s;
        for(int i=1;i<=m;i++){
            cin>>arr[i];
            int val;scanf("%d",&val);
            dir.insert(arr[i],val);
        }
        dir.build();
        double l=0.0,r=1e5+0.0, eps=1e-8;
        while(r-l>eps){
            double mid=(l+r)/2;
            if(dir.check(s,mid)){l=mid;}
            else{r=mid;}
        }
        dir.check(s,l);
        //dir.print();
        printf("%d\n",dir.cal());
        string temp;
    }
    
    
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Time Limit Exceeded

input:

80
4 7
ggeg
g 92946
d 65678
gg 50828
wrj 93954
gge 53780
a 94179
geg 40837
5 6
hiiii
ii 67984
foh 69185
hi 88153
pj 39431
i 32219
wfmve 96834
8 12
wvxxvwww
xw 1522
rzl 16262
wx 77596
v 69622
vw 82225
nykkmkv 19236
xv 83470
o 16781
w 2405
m 98319
ww 13889
qggbvty 16331
8 14
jjjiiijh
i 96670
z 74397
i...

output:

199782479
332836990
249640519
199809603
52085
81272
61572
67112
76834
748779217
767969477
554665139
74929
665552405
499139737
665543594
332830174
60785
166465174
63646
103574
101389
894645396
44033
133212058
89874
798718984
499215461
665540622
41750
246992939
96189
249662442
94537
83443
120688
66556...

result: