[DP套DP] SRM 591 div1 StringPath

记录f(i,j)是不是A/B的前缀,需要保留前m个dp值
其实就是轮廓线
复杂度 O(nm4m)

// 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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值