给你三个字符串,这些字符串有些单词模糊不可认了,用"?"来代表。
现在你可以用任意英文小写字母来代表它们。要求是使得给定的三个字符串中
所有的"?"被你认定的字母代替后,各不相同且按字典序出现。问有多少种方式。
输出结果 Mod 10^9+9
#include<iostream> #include<algorithm> #include<string> #include<map>//int dx[4]={0,0,-1,1};int dy[4]={-1,1,0,0}; #include<set>//int gcd(int a,int b){return b?gcd(b,a%b):a;} #include<vector> #include<cmath> #include<queue> #include<string.h> #include<stdlib.h> #include<cstdio> #define mod 1e9+7 #define ll long long using namespace std; int com(string a,string b){ for(int i=0;i<min(a.length(),b.length());++i){ if(a[i]!=b[i]) return (a[i]>b[i]); } return (a.length()>b.length()); } map<string,int> r1; string h; int ss; set<string> w1,w2; void dfs(int k,string x){ while(k<x.length()&&x[k]!='a'-1) k++; if(k>=x.length()){ w1.insert(x); return; } for(int i=0;i<26;++i){ x[k]='a'+i; dfs(k+1,x); } } void dfs2(int k,string x){ while(k<x.length()&&x[k]!='a'-1) k++; if(k>=x.length()&&com(x,h)){ w2.insert(x); r1[x]++; return; } for(int i=0;i<26;++i){ x[k]='a'+i; if(com(x,h)) dfs2(k+1,x); } } void dfs3(int k,string x){ while(k<x.length()&&x[k]!='a'-1) k++; if(k>=x.length()&&com(x,h)){ ss+=r1[h]; return; } for(int i=0;i<26;++i){ x[k]='a'+i; if(com(x,h)) dfs3(k+1,x); } } int main(){ int t; scanf("%d",&t); while(t--){ ss=0; r1.clear();w1.clear();w2.clear(); string x,y,z; cin>>x>>y>>z; for(int i=0;i<x.length();++i) if(x[i]=='?') x[i]='a'-1; for(int i=0;i<y.length();++i) if(y[i]=='?') y[i]='a'-1; for(int i=0;i<z.length();++i) if(z[i]=='?') z[i]='a'-1; dfs(0,x); for(set<string>::iterator it=w1.begin();it!=w1.end();++it){ h=(*it); dfs2(0,y); } for(set<string>::iterator it=w2.begin();it!=w2.end();++it){ h=(*it); dfs3(0,z); } cout<<ss<<endl; } return 0; }下面是超时代码(复杂度10^6*27*27*27)。。大神的打表代码(复杂度4*4*27*27*27)真的很神奇。。
#include<iostream> #include<algorithm> #include<string> #include<map>//int dx[4]={0,0,-1,1};int dy[4]={-1,1,0,0}; #include<set>//int gcd(int a,int b){return b?gcd(b,a%b):a;} #include<vector> #include<cmath> #include<queue> #include<string.h> #include<stdlib.h> #include<cstdio> #define bb 1000000009 #define ll long long using namespace std; int dp[1000005][4]; int mod(int a,int b){ return (a+b)%bb; } int main(){ int t; cin>>t; while(t--){ memset(dp,0,sizeof(dp)); string x,y,z; cin>>x>>y>>z; int a=x.length(),b=y.length(),c=z.length(); int p=max(max(a,b),c); for(int i=0;i<p-a;++i) x+='a'-1; for(int i=0;i<p-b;++i) y+='a'-1; for(int i=0;i<p-c;++i) z+='a'-1; dp[0][0]=1; for(int i=0;i<p;++i){ int a1=x[i],b1=x[i]; int a2=y[i],b2=y[i]; int a3=z[i],b3=z[i]; if(x[i]=='?') a1='a',b1='z'; if(y[i]=='?') a2='a',b2='z'; if(z[i]=='?') a3='a',b3='z'; for(int j=a1;j<=b1;++j){ for(int k=a2;k<=b2;++k){ for(int l=a3;l<=b3;++l){ if(j==k&&k==l){ // 0 dp[i+1][0]=mod(dp[i+1][0],dp[i][0]); dp[i+1][1]=mod(dp[i+1][1],dp[i][1]); dp[i+1][2]=mod(dp[i+1][2],dp[i][2]); dp[i+1][3]=mod(dp[i+1][3],dp[i][3]); } else if(j==k&&k<l){ // 1 dp[i+1][1]=mod(mod(dp[i+1][1],dp[i][0]),dp[i][1]); dp[i+1][3]=mod(mod(dp[i+1][3],dp[i][2]),dp[i][3]); } else if(j<k&&k==l){ // 2 dp[i+1][2]=mod(mod(dp[i+1][2],dp[i][0]),dp[i][2]); dp[i+1][3]=mod(mod(dp[i+1][3],dp[i][1]),dp[i][3]); } else if(j<k&&k<l){ // 3 dp[i+1][3]=mod(dp[i+1][3],dp[i][0]); dp[i+1][3]=mod(dp[i+1][3],dp[i][1]); dp[i+1][3]=mod(dp[i+1][3],dp[i][2]); dp[i+1][3]=mod(dp[i+1][3],dp[i][3]); } else if(j<k){ // ab ac ba dp[i+1][3]=mod(dp[i+1][3],dp[i][1]); dp[i+1][3]=mod(dp[i+1][3],dp[i][3]); } else if(k<l){ // ab ba bc dp[i+1][3]=mod(dp[i+1][3],dp[i][2]); dp[i+1][3]=mod(dp[i+1][3],dp[i][3]); } else if(j==k){ // ac ac bb dp[i+1][1]=mod(dp[i+1][1],dp[i][1]); dp[i+1][3]=mod(dp[i+1][3],dp[i][3]); } else if(k==l){ // ac bb bb dp[i+1][2]=mod(dp[i+1][2],dp[i][2]); dp[i+1][3]=mod(dp[i+1][3],dp[i][3]); } else dp[i+1][3]=mod(dp[i+1][3],dp[i][3]); } } } } cout<<dp[p][3]<<endl; } return 0; }