zoukankan      html  css  js  c++  java
  • 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<cstdio>
    using namespace std;
    char s[20];
    int dp[20][50],a[20];
    int dfs(int pos,int pre,bool lead,bool limit)
    {
        if(pos==0)
        if(lead)return 0;else return 1;
        if(!limit && dp[pos][pre]!=-1)return dp[pos][pre];
        int up=limit?a[pos]:26;
        int ans=0,i;
        if(lead)i=pre;else i=pre+1;
        for(;i<=up;i++)
        ans+=dfs(pos-1,i,lead && i==0,limit && i==a[pos]);
        if(!limit)dp[pos][pre]=ans;
        return ans;
    }
    int solve()
    {
        int pos=0;
        for(int i=0;i<strlen(s)-1;i++)
        if(s[i]>=s[i+1])return 0;
        for(int i=strlen(s)-1;i>=0;i--)
        a[++pos]=s[i]-'a'+1;
        return dfs(pos,0,1,1);
    }
    int main()
    {
        memset(dp,-1,sizeof(dp));
        scanf("%s",s);
        printf("%d
    ",solve());
    }
    View Code
  • 相关阅读:
    Study Plan The FortyEighth Day
    原码与补码
    【innoDB】加锁案例分析
    【InnoDB】事务基础知识
    了解 CAP
    妙用位运算
    Go学习笔记
    .NET Hot Reload热重载
    .NET 6 中的 dotnet monitor
    C# 实现多线程的同步方法详解
  • 原文地址:https://www.cnblogs.com/Sakits/p/6815885.html
Copyright © 2011-2022 走看看