QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#297003 | #7994. 勿蹖宠物 | tarjen# | WA | 1053ms | 219516kb | C++20 | 5.7kb | 2024-01-03 21:15:36 | 2024-01-03 21:15:36 |
Judging History
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;
typedef pair<ll,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);
for(int i=1;i<=n;i++)Pow[i]=Pow[i-1]*p;
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};
}
hs get1(int l,int r){
return has1[r]-has1[l-1]*Pow[r-l+1];
}
hs get2(int l,int r){
return has2[l]-has2[r+1]*Pow[r-l+1];
}
};
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]++;
}
auto check1 = [&](int i,int j,int k,int len2){// s[i][1-j] with s[k]
//len2 = (int)s[k].size();
// cout<<"i="<<i<<" j="<<j<<" k="<<k<<" len2="<<len2<<"\n";
// if(j<=len2){
// cout<<a[i].get1(1,j).first<<" "<<a[k].get2(1,j).first<<"\n";
// }
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]
//len2 = (int)s[k].size();
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++){
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){
add(dp1[l+len2][k][j-len2],dp1[l][i][j]);
}
}
}
}
for(int j=1;j<=len;j++)if(dp2[l][i][j]){
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][k][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: 2ms
memory: 10092kb
input:
7 9 cats eel eve evil lee olive stack
output:
5
result:
ok single line: '5'
Test #2:
score: 0
Accepted
time: 1ms
memory: 5148kb
input:
2 2 a aa
output:
2
result:
ok single line: '2'
Test #3:
score: 0
Accepted
time: 1ms
memory: 9096kb
input:
6 12 aa aab no on pets step
output:
43
result:
ok single line: '43'
Test #4:
score: 0
Accepted
time: 5ms
memory: 31300kb
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: 35636kb
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: 2ms
memory: 35696kb
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: 35556kb
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: 35600kb
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: -100
Wrong Answer
time: 1053ms
memory: 219516kb
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:
316034389
result:
wrong answer 1st lines differ - expected: '810133722', found: '316034389'