1. 程式人生 > >【POJ 1850】 Code

【POJ 1850】 Code

重要 ack ace size dig har i+1 cstring pre

【POJ 1850】 Code


還是非常想說

數位dp真的非常方便!

!。

數位dp真的非常方便!。!


數位dp真的非常方便!

!!



重要的事說三遍


該題轉換規則跟進制差點兒相同 到z時進一位 如az下位為bc 上位必須比下位小

依據這個規則搜出全部情況就可以


#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

int dp[11][27];
int digit[11];

/*
1~26表示加的字母 0表示不加
有前導時 枚舉pre+1 ~ 26-pos
沒有的話枚舉 0 ~ 26-pos
*/

int dfs(int pos,int pre,bool high)
{
    if(pos == -1) return pre > 0;
   if(!high && ~dp[pos][pre]) return dp[pos][pre];

    int i,en,ans = 0,st;
    en = high? digit[pos]: 26-pos;
    st = pre? pre+1: 0;

    for(i = st; i <= en; ++i) ans += dfs(pos-1,i,high && i == en);

    if(!high) dp[pos][pre] = ans;
    return ans;
}

int Solve(char *str)
{
    int i,len = strlen(str);
    for(i = 0; i < len; ++i)
    {
        digit[i] = str[len-i-1]-‘a‘+1;
    }
    return dfs(len-1,-1,1);
}

int main()
{
    memset(dp,-1,sizeof(dp));
    char str[11],i;
    scanf("%s",str);
    for(i = 0; str[i+1]; ++i)
    {
        if(str[i] >= str[i+1])
        {
            puts("0");
            return 0;
        }
    }
    printf("%d\n",Solve(str));
    return 0;
}




【POJ 1850】 Code