TC SRM 557 DIV2 1000

비교적 좋은 dp
k=m일 때, dp[i][j][m]는 i위 문자열의 현재와 j를 기록하고, 목표 문자열은 현재 문자열의 문자열의 개수는 k이다
k!m시, dp[i][j][k]는 i위 문자열에 현재 j이고 접두사와 목표 문자열이 일치하는 개수가 k인 개수를 기록합니다
e[i][0]는 목표 열의 앞 i위 +'U'접미사가 목표 열의 최대 길이와 일치한다는 것을 나타낸다
e[i][1]은 같은 이치로 유사하다.
// BEGIN CUT HERE

// END CUT HERE
#line 5 "FoxAndMountain.cpp"
#include <cstdlib>
#include <cctype>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
#include <iostream>
#include <sstream>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <fstream>
#include <numeric>
#include <iomanip>
#include <bitset>
#include <list>
#include <stdexcept>
#include <functional>
#include <utility>
#include <ctime>
using namespace std;

typedef long long ll;
int e[200][2];
ll dp[110][110][110];
int match(string a,string b){
    while(a.substr(0,b.length())!=b)
        b=b.substr(1);
    return b.length();
}
class FoxAndMountain
{
        public:
        int count(int n, string history)
        {
                int i,j,k;
                int m=history.size();
                for(i=0;i<m;i++){
                    e[i][0]=match(history,history.substr(0,i)+'U');
                    e[i][1]=match(history,history.substr(0,i)+'D');
                }
                e[m][0]=e[m][1]=m;
                memset(dp,0,sizeof(dp));
                dp[0][0][0]=1;
                for(i=0;i<n;i++)
                    for(j=0;j<=i;j++){
                        for(k=0;k<=m && k<=i;k++){
                            dp[i+1][j+1][e[k][0]]+=dp[i][j][k];
                            if(j)dp[i+1][j-1][e[k][1]]+=dp[i][j][k];
                        }
                    }
                return (int)(dp[n][0][m]%1000000009);
        }

// 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(); if ((Case == -1) || (Case == 4)) test_case_4(); if ((Case == -1) || (Case == 5)) test_case_5(); if ((Case == -1) || (Case == 6)) test_case_6(); }
	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 = 4; string Arg1 = "UUDD"; int Arg2 = 1; verify_case(0, Arg2, count(Arg0, Arg1)); }
	void test_case_1() { int Arg0 = 4; string Arg1 = "DUUD"; int Arg2 = 0; verify_case(1, Arg2, count(Arg0, Arg1)); }
	void test_case_2() { int Arg0 = 4; string Arg1 = "UU"; int Arg2 = 1; verify_case(2, Arg2, count(Arg0, Arg1)); }
	void test_case_3() { int Arg0 = 49; string Arg1 = "U"; int Arg2 = 0; verify_case(3, Arg2, count(Arg0, Arg1)); }
	void test_case_4() { int Arg0 = 20; string Arg1 = "UUUDUUU"; int Arg2 = 990; verify_case(4, Arg2, count(Arg0, Arg1)); }
	void test_case_5() { int Arg0 = 30; string Arg1 = "DUDUDUDUDUDUDUDU"; int Arg2 = 3718; verify_case(5, Arg2, count(Arg0, Arg1)); }
	void test_case_6() { int Arg0 = 50; string Arg1 = "U"; int Arg2 = 946357703; verify_case(6, Arg2, count(Arg0, Arg1)); }

// END CUT HERE

};

// BEGIN CUT HERE
int main()
{
        FoxAndMountain ___test;
        ___test.run_test(-1);
        return 0;
}
// END CUT HERE

좋은 웹페이지 즐겨찾기