QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#297036#7994. 勿蹖宠物tarjen#TL 1089ms219240kbC++206.1kb2024-01-03 21:35:322024-01-03 21:35:33

Judging History

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

  • [2024-01-03 21:35:33]
  • 评测
  • 测评结果:TL
  • 用时:1089ms
  • 内存:219240kb
  • [2024-01-03 21:35:32]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
const int mod=1e9+7;
void add(int &x,int y){
    if((x+=y)>=mod)x-=mod;
}
const int N=1e6+10;
typedef long long ll;
const ll p1=31,p2=131;
const ll mod1=1e9+7,mod2=1e9+9;
const ll p=31;
const ll Mod=1e9+7;
// typedef pair<ll,ll> hs;
typedef ll hs;
// const hs p = make_pair(p1,p2);
// hs &operator+=(hs &a, hs b) {
//     a.first=(a.first+b.first)%mod1;
//     a.second=(a.second+b.second)%mod2;
//     return a;
// }
// hs operator+(hs a, hs b) { return a += b; }
// hs &operator-=(hs &a, hs b) {
//     a.first=(a.first-b.first+mod1)%mod1;
//     a.second=(a.second-b.second+mod2)%mod2;
//     return a;
// }
// hs operator-(hs a, hs b) { return a -= b; }
// hs &operator*=(hs &a, hs b) {
//     a.first=(a.first*b.first)%mod1;
//     a.second=(a.second*b.second)%mod2;
//     return a;
// }
// hs operator*(hs a, hs b) { return a *= b; }
struct Hash{
    char st[N];
    int n;
    vector<hs>has1,has2,Pow;
    void Hash_init(string &s){
        n=(int)s.size();
        Pow.resize(n+2);
        has1.resize(n+2);
        has2.resize(n+2);
        // Pow[0]=make_pair(1ll,1ll);
        Pow[0]=1ll;
        for(int i=1;i<=n;i++)Pow[i]=Pow[i-1]*p%Mod;
        // for(int i=1;i<=n;i++)has1[i]=has1[i-1]*p+hs{s[i-1]-'a'+1,s[i-1]-'a'+1};
        // for(int i=n;i>=1;i--)has2[i]=has2[i+1]*p+hs{s[i-1]-'a'+1,s[i-1]-'a'+1};
        for(int i=1;i<=n;i++)has1[i]=(has1[i-1]*p+s[i-1]-'a'+1)%Mod;
        for(int i=n;i>=1;i--)has2[i]=(has2[i+1]*p+s[i-1]-'a'+1)%Mod;
    }
    hs get1(int l,int r){
        return (has1[r]-has1[l-1]*Pow[r-l+1]%Mod+Mod)%Mod;
    }
    hs get2(int l,int r){
        return (has2[l]-has2[r+1]*Pow[r-l+1]%Mod+Mod)%Mod;
    }
};
vector<vector<int>>dp1[1001],dp2[1001];
int main()
{
    // ios::sync_with_stdio(false);
    // cin.tie(0);
    int n,L;
    cin>>n>>L;
    vector<Hash> a(n);
    vector<string>s(n);
    for(int i=0;i<n;i++){
        cin>>s[i];
        a[i].Hash_init(s[i]);
    }
    vector<int> zero(L+1);
    //1 前缀 2后缀 值是 分界线 [1-i]  [i-len]
    for(int i=1;i<=L;i++){
        dp1[i].resize(n);
        dp2[i].resize(n);
        for(int j=0;j<n;j++){
            dp1[i][j].resize((int)s[j].size()+1);
            dp2[i][j].resize((int)s[j].size()+1);
        }
    }
    zero[0]=1;
    for(int i=0;i<n;i++){
        int len=s[i].size();
        for(int j=1;j<=len;j++){//奇中心
            if(j>1&&j<len){
                int p1=j-1,p2=len-j;
                if(p1<p2){
                    if(a[i].get1(1,p1)==a[i].get2(j+1,j+p1)){
                        dp2[len][i][j+p1+1]++;
                    }
                }
                if(p1==p2){
                    if(a[i].get1(1,p1)==a[i].get2(j+1,j+p1)){
                        zero[len]++;
                    }
                }
                if(p1>p2){
                    if(a[i].get1(p1-p2+1,p1)==a[i].get2(j+1,len)){
                        dp1[len][i][p1-p2]++;
                    }
                }
            }
            else{
                if(j==1&&j+1<=len)dp2[len][i][2]++;
                if(j==len&&len-1>=1)dp1[len][i][len-1]++;
            }
        }
        for(int j=1;j<len;j++){//偶中心
            int p1=j,p2=len-j;
            if(p1<p2){
                if(a[i].get1(1,p1)==a[i].get2(j+1,j+p1)){
                    dp2[len][i][j+p1+1]++;
                }
            }
            if(p1==p2){
                if(a[i].get1(1,p1)==a[i].get2(j+1,j+p1)){
                    zero[len]++;
                }
            }
            if(p1>p2){
                if(a[i].get1(p1-p2+1,p1)==a[i].get2(j+1,len)){
                    dp1[len][i][p1-p2]++;
                }
            }
        }
        dp1[len][i][len]++;
        if(len==1)zero[len]++;
    }
    auto check1 = [&](int i,int j,int k,int len2){// s[i][1-j]   with s[k] 
        if(j<=len2)return a[i].get1(1,j)==a[k].get2(1,j);
        else return a[i].get1(j-len2+1,j)==a[k].get2(1,len2);
    };
    auto check2 = [&](int i,int j,int k,int len2){// s[i][j-n]   with s[k] 
        int len=(int)s[i].size();
        if(len-j+1<=len2) return a[i].get1(j,len)==a[k].get2(len2-(len-j+1)+1,len2);
        else return a[i].get1(j,j+len2-1)==a[k].get2(1,len2);
    };
    for(int l=1;l<=L;l++){
        // cout<<"zero["<<l<<"]="<<zero[l]<<"\n";
        for(int i=0;i<n;i++){
            int len=(int)s[i].size();
            if(l+len<=L)add(dp1[l+len][i][len],zero[l]);

            for(int j=1;j<=len;j++){
                if(dp1[l][i][j]){
                    // cout<<"dp1["<<l<<"]["<<i<<"]["<<j<<"]="<<dp1[l][i][j]<<"\n";
                    for(int k=0;k<n;k++)if(l+(int)s[k].size()<=L&&check1(i,j,k,(int)s[k].size())){
                        int len2=(int)s[k].size();
                        if(j<len2){
                            add(dp2[l+len2][k][j+1],dp1[l][i][j]);
                        }
                        if(j==len2){
                            add(zero[l+len2],dp1[l][i][j]);
                        }
                        if(j>len2){
                            // cout<<"l="<<l<<" len2="<<len2<<" k="<<k<<" j="<<j<<" len2="<<len2<<"\n";
                            add(dp1[l+len2][i][j-len2],dp1[l][i][j]);
                        }
                    }
                }
            }
            for(int j=1;j<=len;j++)if(dp2[l][i][j]){
                // cout<<"dp2["<<l<<"]["<<i<<"]["<<j<<"]="<<dp2[l][i][j]<<"\n";
                assert(j!=1);
                for(int k=0;k<n;k++)if(l+(int)s[k].size()<=L&&check2(i,j,k,(int)s[k].size())){
                    int len2=(int)s[k].size();
                    if(len-j+1<len2){
                        add(dp1[l+len2][k][len2-(len-j+1)],dp2[l][i][j]);
                    }
                    if(len-j+1==len2){
                        add(zero[l+len2],dp2[l][i][j]);
                    }
                    if(len-j+1>len2){
                        add(dp2[l+len2][i][j+len2],dp2[l][i][j]);
                    }
                }
            }
        }
    }
    cout<<zero[L]<<"\n";
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 9956kb

input:

7 9
cats
eel
eve
evil
lee
olive
stack

output:

5

result:

ok single line: '5'

Test #2:

score: 0
Accepted
time: 0ms
memory: 5212kb

input:

2 2
a
aa

output:

2

result:

ok single line: '2'

Test #3:

score: 0
Accepted
time: 0ms
memory: 9148kb

input:

6 12
aa
aab
no
on
pets
step

output:

43

result:

ok single line: '43'

Test #4:

score: 0
Accepted
time: 4ms
memory: 31336kb

input:

26 1000
a
b
c
d
e
f
g
h
i
j
k
l
m
n
o
p
q
r
s
t
u
v
w
x
y
z

output:

710955506

result:

ok single line: '710955506'

Test #5:

score: 0
Accepted
time: 0ms
memory: 35560kb

input:

33 18
zrkodfkhhkfdokrzo
ytcbwrgyygrwbcty
makgiybggbyigkamc
aptmvovgccgvovmtpa
yydxdzhhhhzdxdyy
iadqfexoxacojythvk
iagcfiwlaalwifcgai
rtfzhddzzddhzftrm
vkreigbdyxfiuvyqid
mbcgnvrvvrvngcbmc
lywbtspuyyupstbwyl
bmjxalsyyslaxjmb
jrbminaooanimbrj
wwrajerkkrejarww
grocaiqccqiacorg
efvibgurrugbivfec
ilyzrft...

output:

9

result:

ok single line: '9'

Test #6:

score: 0
Accepted
time: 0ms
memory: 35576kb

input:

33 18
babbababbbbababbab
abbabaabaabaababba
aaabbbaabbaabbbaaa
aaabaababbabaabaaa
ababbaabbbbaabbaba
baababbbbbbbbabaab
aababbbaaaabbbabaa
bbbbbabaaaababbbbb
aabbbaaaaaaaabbbaa
aabbbabaaaababbbaa
bbabbaabaabaabbabb
aababbabbbbabbabaa
baaaaaabbbbaaaaaab
aaabababaabababaaa
bbaabbaaaaaabbaabb
bbbababab...

output:

33

result:

ok single line: '33'

Test #7:

score: 0
Accepted
time: 0ms
memory: 35620kb

input:

33 18
babababaaaabababa
abbbabbaaaabbabbba
bbaaaabbaabbaaaabb
baabbbbaaaabbbbaab
abbbaababbabaabbb
aaababbabbabbabaaa
abbbbbbabbabbbbbba
bbaabaaaaaaaabaabb
aaaaabaaaaaabaaaaa
abbbaabbbbbbaabbba
abbbbaaaaaaaabbbba
bbbbbbbbaabbbbbbbb
baabaabbbbaabaab
bbaababaaaababaabb
baaabaabbbbaabaaab
bbaaaabbbbbba...

output:

27

result:

ok single line: '27'

Test #8:

score: 0
Accepted
time: 0ms
memory: 35532kb

input:

33 18
bbbababaabababbb
abaabaaabbaaabaaba
aaabbbbbbaaaabbbab
aaabbababbababbaaa
abbaaabaabaaaaabbb
abababaaaaaabababa
bbabaaaaaaaababb
bababbaaaaaabbaba
aaaaababbbbabaaaa
aaabaababbabaabaa
abbbbbabbabbbbbaa
abbbbbaabbaabbbbba
abaaabbabaabbbabbb
aabaabbbaabbbaabaa
aaababbbbbabababaa
abaaabbaabbaaabab...

output:

8

result:

ok single line: '8'

Test #9:

score: 0
Accepted
time: 1032ms
memory: 219240kb

input:

199 999
lbl
buei
ieub
uw
wu
impv
vpmi
yrek
kery
cw
wc
hjs
sjh
bct
tcb
up
pu
wh
hw
ftn
ntf
iv
vi
ejpj
jpje
qjgh
hgjq
kvny
ynvk
xo
ox
pr
rp
sdh
hds
go
og
qw
wq
bgt
tgb
czwk
kwzc
coqr
rqoc
af
fa
ms
sm
gs
sg
hnz
znh
rugm
mgur
lak
kal
xlv
vlx
na
an
fdoe
eodf
oixg
gxio
mb
bm
bjt
tjb
bto
otb
oxq
qxo
tg
gt
...

output:

810133722

result:

ok single line: '810133722'

Test #10:

score: 0
Accepted
time: 1089ms
memory: 218304kb

input:

198 999
eme
hm
mh
msi
ism
ig
gi
kmm
mmk
nkxq
qxkn
uv
vu
mgj
jgm
zyap
payz
xz
zx
oi
io
spe
eps
kr
rk
pqp
pvyw
wyvp
pk
kp
nrin
nirn
ehj
jhe
vb
bv
fnzq
qznf
lem
mel
lxek
kexl
sy
ys
smcs
scms
xaq
qax
zdsn
nsdz
pudp
pdup
zt
tz
op
po
oe
eo
cea
aec
df
fd
om
mo
zr
rz
wabq
qbaw
ehde
edhe
cq
qc
utd
dtu
rj
jr
...

output:

165733504

result:

ok single line: '165733504'

Test #11:

score: -100
Time Limit Exceeded

input:

187 999
dcd
ede
bbb
ga
ag
aead
daea
ec
ce
bf
fb
ffg
gff
bde
edb
efc
cfe
bb
aged
dega
cea
aec
accg
gcca
cge
egc
gb
bg
cfcg
gcfc
fgg
ggf
bffc
cffb
fg
gf
bac
cab
adfb
bfda
ff
adfe
efda
ggaf
fagg
age
ega
cb
bc
acbd
dbca
bbdd
ddbb
ee
fegg
ggef
gca
acg
ffab
baff
gdb
bdg
gee
eeg
babb
bbab
bfe
efb
be
eb
ddb...

output:

359867349

result: