TC SRM 557 DIV2 1000

來源:互聯網
上載者:User

比較好的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 HEREpublic: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 HEREint main(){        FoxAndMountain ___test;        ___test.run_test(-1);        return 0;}// END CUT HERE

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.