poj1850的数位dp解法。

博客给出了poj1850的数位dp解法代码。包含头文件、常量定义、结构体定义等,有dfs函数进行递归计算,init函数进行初始化和输入处理,最终在main函数中调用相关函数完成求解。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <iomanip>
#include <cstring>
#include <map>
#include <queue>
#include <set>
#include <cassert>
#include <stack>
#include <bitset>
//#include <unordered_set>
#define mkp make_pair
#define err cout<<"here"<<endl
using namespace std;
const double EPS=1e-12;
typedef long long lon;
typedef unsigned long long ull;
typedef map<ull,int>::iterator IT;
const lon SZ=12,SSZ=10,APB=128,m2=200003,mod=9901,one=97;
const lon INF=0x7FFFFFFF;
int n,dp['z'+1][SZ];
struct nd{
    int to,wt;
    nd(int a=0,int b=0):to(a),wt(b){}
};
char ch[SZ];

int dfs(int pos,char pre,int lim)
{
    if(!lim&&dp[pre][pos]!=-1)return dp[pre][pos];
    if(pos==n+1)
    {
        return pre!='a'-1;
    }
    int up=lim?ch[pos]:'z',res=0;
    for(int i=pre=='a'-1?'a'-1:pre+1;i<=up;++i)
    {
        res+=dfs(pos+1,i,lim&&i==ch[pos]);
    }
    if(!lim)dp[pre][pos]=res;
    return res;
}

void init()
{
    cin>>ch+1;
    memset(dp,-1,sizeof(dp));
    n=strlen(ch+1);
    for(int i=1;ch[i];++i)
    {
        if(ch[i]<=ch[i-1])
        {
            cout<<0<<endl;
            return;
        }
    }
    int res=dfs(1,'a'-1,1);
    cout<<res<<endl;
}

void work()
{
    
}

void release()
{
    
}

int main()
{
    std::ios::sync_with_stdio(0);
    //freopen("d:\\1.txt","r",stdin);
    lon casenum;
    //cin>>casenum;
    //cout<<casenum<<endl;
    //for(lon time=1;time<=casenum;++time)
    //for(lon time=1;cin>>n;++time)
    {
        init();
        work();
        release();
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/gaudar/p/10830612.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值