TZOJ 5094 Stringsobits(DP)
描述
Consider an ordered set S of strings of N (1 <= N <= 31) bits. Bits, of course, are either 0 or 1.
This set of strings is interesting because it is ordered and contains all possible strings of length N that have L (1 <= L <= N) or fewer bits that are `1'.
Your task is to read a number I (1 <= I <= sizeof(S)) from the input and print the Ith element of the ordered set for N bits with no more than L bits that are `1'.
输入
A single line with three space separated integers: N, L, and I.
输出
A single line containing the integer that represents the Ith element from the order set, as described.
样例输入
5 3 19
样例输出
10011
题意
N位的二进制串,从小到大排序,问'1'的个数<=L的第I个串。
题解
采用逐位确定法,如果要第N位填上'1',那么前N-1位<=L的串的个数<=I。
那么如何确定前N-1位<=L的串的个数呢?
dp[i][j]代表第i位填了j个'1'的串的个数,
当第i位填'1',那么就有dp[i-1][j-1]。
当第i位填‘0’,那么就有dp[i-1][j]。
代码
#include<bits/stdc++.h>
using namespace std;
#define ll long long
ll dp[][],I;
int main()
{
int N,L;
cin>>N>>L>>I;
dp[][]=;
for(int i=;i<=N;i++)
for(int j=;j<=L;j++)
if(j==)dp[i][j]=dp[i-][j];
else if(j>i)dp[i][j]=dp[i][i];
else if(j==i)dp[i][j]=dp[i][j-]+;
else dp[i][j]=dp[i-][j]+dp[i-][j-];
for(int i=N-;i>=;i--)
if(dp[i][L]<I){cout<<"";I-=dp[i][L--];}
else cout<<"";
if(I!=)cout<<"";
else cout<<"";
return ;
}
最新文章
- TouchPoint.js – 可视化展示 HTML 原型点击效果
- Linux内核设计第二周——操作系统工作原理
- Browser设置搜索引擎
- HTML5之结构元素
- visual asssit 过期提示
- fragment嵌套,viewpager嵌套 不能正确显示
- Oracle语句优化1
- C++各种指针辨析
- JS获取前天、昨天、今天、明天、后天的时间
- cocos2dx-lua中handler解析
- Qt学习2---信号与槽
- DP基础练习(4.21)
- 【2018.05.05 C与C++基础】C++中的自动废料收集:概念与问题引入
- JCR分区(WOS或Thomson Reuters或汤姆森 路透)和中科院分区(附网址及查询方法)
- javascript编写带阴历的黄历
- android 当ListView滚动时自动调用 onCheckedChanged 导致CheckBox 状态不停变化 的解决办法
- Maven命令行使用 mvn clean package
- Visual Studio最常用的快捷键
- C# 合并多个结构相同的DataTable
- jquery中的正则表达式