记录f(i,j)是不是A/B的前缀,需要保留前m个dp值
其实就是轮廓线
复杂度
O(nm∗4m)
// BEGIN CUT HERE
#include<conio.h>
#include<sstream>
// END CUT HERE
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<vector>
#include<map>
#include<string>
#include<set>
#define cl(x) memset(x,0,sizeof(x))
using namespace std;
typedef long long ll;
const int P=1e9+9;
int n,m;
char A[20],B[20];
ll f[2][1<<16];
class StringPath{
public:
int countBoards(int n, int m, string A, string B){
if (A[0]!=B[0]) return 0;
int t=0;
cl(f); f[t][3<<(2*m-2)]=1;
for (int i=2;i<=n*m;i++,t=t^1){
int x=(i-1)/m+1,y=(i-1)%m+1; cl(f[t^1]);
for (int j=0;j<(1<<(2*m));j++)
if (f[t][j]){
if (A[x+y-2]==B[x+y-2]){
(f[t^1][j>>2]+=f[t][j]*25%P)%=P;
(f[t^1][(j>>2)|(((((j>>(2*m-2))*(y>1))|j)&3)<<(2*m-2))]+=f[t][j])%=P;
}else{
(f[t^1][j>>2]+=f[t][j]*24%P)%=P;
(f[t^1][(j>>2)|(((((j>>(2*m-2))*(y>1))|j)&1)<<(2*m-2))]+=f[t][j])%=P;
(f[t^1][(j>>2)|(((((j>>(2*m-2))*(y>1))|j)&2)<<(2*m-2))]+=f[t][j])%=P;
}
}
}
ll ans=0;
for (int j=0;j<(1<<(2*m));j++)
if (((j>>(2*m-2))&3)==3)
(ans+=f[t][j])%=P;
return ans;
}
// BEGIN CUT HERE
public:
void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0(); if ((Case == -1) || (Case == 1)) test_case_1(); if ((Case == -1) || (Case == 2)) test_case_2(); if ((Case == -1) || (Case == 3)) test_case_3(); }
private:
template <typename T> string print_array(const vector<T> &V) { ostringstream os; os << "{ "; for (typename vector<T>::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); }
void verify_case(int Case, const int &Expected, const int &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } }
void test_case_0() { int Arg0 = 2; int Arg1 = 2; string Arg2 = "ABC"; string Arg3 = "ADC"; int Arg4 = 2; verify_case(0, Arg4, countBoards(Arg0, Arg1, Arg2, Arg3)); }
void test_case_1() { int Arg0 = 2; int Arg1 = 2; string Arg2 = "ABC"; string Arg3 = "ABD"; int Arg4 = 0; verify_case(1, Arg4, countBoards(Arg0, Arg1, Arg2, Arg3)); }
void test_case_2() { int Arg0 = 3; int Arg1 = 4; string Arg2 = "ABCDDE"; string Arg3 = "ACCBDE"; int Arg4 = 1899302; verify_case(2, Arg4, countBoards(Arg0, Arg1, Arg2, Arg3)); }
void test_case_3() { int Arg0 = 8; int Arg1 = 8; string Arg2 = "ZZZZZZZZZZZZZZZ"; string Arg3 = "ZABCDEFGHIJKLMZ"; int Arg4 = 120390576; verify_case(3, Arg4, countBoards(Arg0, Arg1, Arg2, Arg3)); }
// END CUT HERE
};
// BEGIN CUT HERE
int main(){
StringPath ___test;
___test.run_test(-1);
getch() ;
return 0;
}
// END CUT HERE