Advertisement
yicongli

LG3290

Mar 25th, 2019
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.06 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. #define gc c=getchar()
  6. #define r(x) read(x)
  7. #define ll long long
  8.  
  9. template<typename T>
  10. inline void read(T&x){
  11.     x=0;T k=1;char gc;
  12.     while(!isdigit(c)){if(c=='-')k=-1;gc;}
  13.     while(isdigit(c)){x=x*10+c-'0';gc;}x*=k;
  14. }
  15.  
  16. const int p=1e9+7;
  17. const int N=205;
  18. const int M=15;
  19.  
  20. inline void add(int &a,int b){
  21.     if((a+=b)>=p)a-=p;
  22. }
  23.  
  24. inline int index(char c){
  25.     switch(c){
  26.         case 'W':return 0;
  27.         case 'B':return 1;
  28.         default :return 2;
  29.     }
  30. }
  31.  
  32. int n,m,c,T,len;
  33.  
  34. inline void GetNext(int *Next,int *S){
  35.     for(int i=2,j=0;i<=c;++i){
  36.         while(j&&S[i]!=S[j+1])j=Next[j];
  37.         if(S[i]==S[j+1])j++;
  38.         Next[i]=j;
  39.     }
  40. }
  41.  
  42. inline int kmp(int *S,int *Next,int x,int ch){
  43.     while(x&&S[x+1]!=ch)x=Next[x];
  44.     if(S[x+1]==ch)x++;
  45.     return x;
  46. }
  47.  
  48. char s1[M];
  49. char s2[M];
  50. int A[M];
  51. int B[M];
  52. int nx1[M];
  53. int nx2[M];
  54. int f[2][(1<<12)|7][7][7];
  55.  
  56. int main(){
  57.     // freopen(".in","r",stdin);
  58.     // freopen(".out","w",stdout);
  59.     r(n),r(m),r(c),r(T);
  60.     len=m-c+1;
  61.     while(T--){
  62.         memset(f,0,sizeof(f));
  63.         scanf("%s%s",s1+1,s2+1);
  64.         for(int i=1;i<=c;++i)A[i]=index(s1[i]);A[0]=A[c+1]=-1;
  65.         for(int i=1;i<=c;++i)B[i]=index(s2[i]);B[0]=B[c+1]=-1;
  66.         GetNext(nx1,A);
  67.         GetNext(nx2,B);
  68.         bool cur;
  69.         f[cur][0][0][0]=1;
  70.         ll power=1;
  71.         for(int i=1;i<=n;++i){
  72.             for(int j=1;j<=m;++j){
  73.                 (power*=3)%=p;
  74.                 for(int s=0;s<(1<<len);++s){
  75.                     for(int a=0;a<=c;++a){
  76.                         for(int b=0;b<=c;++b){
  77.                             if(!f[cur][s][a][b])continue;
  78.                             for(int ch=0;ch<3;++ch){
  79.                                 int x=kmp(A,nx1,a,ch);
  80.                                 int y=kmp(B,nx2,b,ch);
  81.                                 int t=s;
  82.                                 if(j>=c){
  83.                                     t&=(~(1<<(j-c)));
  84.                                     t|=(x==c)<<(j-c);
  85.                                 }
  86.                                 if(j>=c&&(s>>(j-c)&1)&&y==c)continue;
  87.                                 add(f[cur^1][t][x][y],f[cur][s][a][b]);
  88.                             }
  89.                             f[cur][s][a][b]=0;
  90.                         }
  91.                     }
  92.                 }
  93.                 cur^=1;
  94.             }
  95.             for(int s=0;s<(1<<len);++s){
  96.                 int sum=0;
  97.                 for(int a=0;a<=c;++a){
  98.                     for(int b=0;b<=c;++b){
  99.                         add(sum,f[cur][s][a][b]);
  100.                         f[cur][s][a][b]=0;
  101.                     }
  102.                 }
  103.                 f[cur][s][0][0]=sum;
  104.             }
  105.         }
  106.         int ans=0;
  107.         for(int s=0;s<(1<<len);++s){
  108.             add(ans,f[cur][s][0][0]);
  109.         }
  110.         printf("%lld\n",(power-ans+p)%p);
  111.     }
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement