POJ 1850 Code 数位DP

据说又是一道组合数学题,数学不好的我只想出的DP写法

注意如果输入不合法要输出0

#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <climits>
#include <string>
#include <iostream>
#include <map>
#include <cstdlib>
#include <list>
#include <set>
#include <queue>
#include <stack>

using namespace std;

typedef long long LL;
const int maxn = 15;
int lim[maxn],len;
char buf[maxn],str[maxn];
LL f[90][90];

void getlim(char *str) {
    memset(lim,0,sizeof(lim));
    len = strlen(str);
    for(int i = 0;i < len;i++) lim[i] = buf[i] - ‘a‘ + 1;
}

LL dfs(int now,int prev,int bound) {
    LL &note = f[now][prev == -1 ? 0 : prev];
    if(now == len - 1) {
        if(prev > 0) return 1;
        return 0;
    }
    if(!bound && note != -1) return note;
    int m = bound ? lim[now + 1] : 26;
    LL ret = 0;
    for(int i = prev == -1 ? 0 : prev + 1;i <= m;i++) {
        ret += dfs(now + 1,i == 0 ? -1 : i,bound && i == lim[now + 1]);
    }
    if(!bound) note = ret;
    return ret;
}

int main() {
    while(scanf("%s",buf) != EOF) {
        bool bad = false;
        len = strlen(buf);
        for(int i = 1;i < len;i++) if(buf[i] <= buf[i - 1]) bad = true;
        if(bad) {
            puts("0"); continue;
        }
        memset(f,-1,sizeof(f));
        getlim(buf);
        cout << dfs(-1,-1,1) << endl;
    }
    return 0;
}

  

POJ 1850 Code 数位DP

时间: 2024-12-29 12:36:51

POJ 1850 Code 数位DP的相关文章

poj:1850 Code(组合数学?数位dp!)

题目大意:字符的字典序依次递增才是合法的字符串,将字符串依次标号如:a-1 b-2 ... z-26 ab-27 bc-52. 为什么题解都是组合数学的...我觉得数位dp很好写啊(逃 f[pos][pre]前pos位,前一位是pre有几个满足条件的字符串,其实等同于这个字符串的序号是多少 好像数位dp的博客真没什么东西好写的... #include<iostream> #include<cstring> #include<cstdlib> #include<cs

[POJ 1850] Code

Code Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 8539   Accepted: 4048 Description Transmitting and memorizing information is a task that requires different coding systems for the best use of the available space. A well known system

FZU 2113 BCD Code 数位dp

数位dp,但是很奇怪的是我在虚拟oj上用GUC C++提交会wa,用Visual c++提交正确,但是加上注释后提交又莫名CE……好任性啊 0 ,0 题目思路:看代码吧 注释很详细 #include<iostream>#include<algorithm>#include<cstring>#include<vector>#include<stdio.h>#include<stdlib.h>#include<queue>#i

POJ 1850 Code(组合数学)

Code Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 8662   Accepted: 4113 Description Transmitting and memorizing information is a task that requires different coding systems for the best use of the available space. A well known system

POJ 1850 Code 组合数学

Description Transmitting and memorizing information is a task that requires different coding systems for the best use of the available space. A well known system is that one where a number is associated to a character sequence. It is considered that

POJ Round Numbers(数位DP)

题目大意: Round Number:  将一个整数转化为二进制数字后,(不含前导0) 要是0的个数 大于等于1的个数 则是 Round Number 问从L-R之中有多少个Round Number 题目分析: 要转化为2进制数字,我们用10进制保存明显不好判断0和1的个数,所以选择用8进制来存储,这样的话每一次进位会多出 ”000“, 然后再加上8进制的尾数, 最后我们可以得出增加了几个 1 和 增加了 几个 0 需要注意的一点就是, 当前面的数字小于 8 的时候 我们要对 前导 0 进行特殊

POJ 1850 Code(找规律)

Code Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 7913   Accepted: 3709 Description Transmitting and memorizing information is a task that requires different coding systems for the best use of the available space. A well known system

POJ 1850/ 1496 组合数学

POJ 1850/ 1496 组合数学 题目地址: POJ 1496 Word Index POJ 1850 Code 题意: 1. 每个词是自增的 2. 同样长度的词是按字典序排练的 3. 我们把这些词标序 求某个词的序号 分析: 组合数学... 推出公式然后用杨辉三角打表出组合数,具体看http://blog.csdn.net/lyy289065406/article/details/6648492. 我还是太弱Orz... 代码: 1496: /* * Author: illuz <iil

【POJ 1850】 Code

[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