HDU 4055 Number String dp
题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=4055
Number String
Time Limit: 10000/5000 MS (Java/Others)Memory Limit: 32768/32768 K (Java/Others)
#### 问题描述
> The signature of a permutation is a string that is computed as follows: for each pair of consecutive elements of the permutation, write down the letter 'I' (increasing) if the second element is greater than the first one, otherwise write down the letter 'D' (decreasing). For example, the signature of the permutation {3,1,2,7,4,6,5} is "DIIDID".
>
> Your task is as follows: You are given a string describing the signature of many possible permutations, find out how many permutations satisfy this signature.
>
> Note: For any positive integer n, a permutation of n elements is a sequence of length n that contains each of the integers 1 through n exactly once.
输入
Each test case consists of a string of 1 to 1000 characters long, containing only the letters 'I', 'D' or '?', representing a permutation signature.
Each test case occupies exactly one single line, without leading or trailing spaces.
Proceed to the end of file. The '?' in these strings can be either 'I' or 'D'.
输出
For each test case, print the number of permutations satisfying the signature on a single line. In case the result is too large, print the remainder modulo 1000000007.
样例输入
II
ID
DI
DD
?D
??
样例输出
1
2
2
1
3
6
题意
给你一个排列的相邻位置的大小关系:'I'表示前一个小于后一个,'D'表示前一个大于后一个,'?'表示不确定。问满足这些大小关系的全排列的所有可能数。
题解
比较容易想到的是dp[i][j]表示前i个数,最后一个是j的满足条件的总数,然后用前缀和优化下跑n^2,但是,这样跑出来的结果是可重集的答案!不是1到n的全排列!!!
那么如何做到每个数子都只出现一次?一种巧妙的做法是dp[i][j]表示1到i的全排列的符合条件的总数,那么怎么转移?其实转移没差,dp[i][j]=sigma(dp[i][k]),1<=k<=i,你可以这样理解:吧前面所有>=j的数都全部+1。比如{1,3,2,4},现在插入一个3变成{1,4,2,5,3}!
代码
#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<ctime>
#include<vector>
#include<cstdio>
#include<string>
#include<bitset>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
using namespace std;
#define X first
#define Y second
#define mkp make_pair
#define lson (o<<1)
#define rson ((o<<1)|1)
#define mid (l+(r-l)/2)
#define sz() size()
#define pb(v) push_back(v)
#define all(o) (o).begin(),(o).end()
#define clr(a,v) memset(a,v,sizeof(a))
#define bug(a) cout<<#a<<" = "<<a<<endl
#define rep(i,a,b) for(int i=a;i<(b);i++)
#define scf scanf
#define prf printf
typedef long long LL;
typedef vector<int> VI;
typedef pair<int,int> PII;
typedef vector<pair<int,int> > VPII;
const int INF=0x3f3f3f3f;
const LL INFL=0x3f3f3f3f3f3f3f3fLL;
const double eps=1e-8;
const double PI = acos(-1.0);
//start----------------------------------------------------------------------
const int maxn=1111;
const int mod=1e9+7;
char str[maxn];
LL dp[maxn][maxn],sum[maxn][maxn];
int main() {
while(scf("%s",str+2)==1) {
clr(sum,0);
sum[1][1]=dp[1][1]=1;
int len=strlen(str+2);
// bug(len);
for(int i=2; i<len+2; i++) {
for(int j=1; j<=i; j++) {
if(str[i]=='I') {
dp[i][j]=sum[i-1][j-1];
} else if(str[i]=='D') {
dp[i][j]=(sum[i-1][i-1]-sum[i-1][j-1]+mod)%mod;
} else {
dp[i][j]=sum[i-1][i-1];
}
sum[i][j]=sum[i][j-1]+dp[i][j];
sum[i][j]%=mod;
}
}
prf("%lld\n",sum[len+1][len+1]);
}
return 0;
}
//end-----------------------------------------------------------------------
HDU 4055 Number String dp的更多相关文章
- HDU 4055 Number String(DP计数)
题意: 给你一个含n个字符的字符串,字符为'D'时表示小于号,字符为“I”时表示大于号,字符为“?”时表示大小于都可以.比如排列 {3, 1, 2, 7, 4, 6, 5} 表示为字符串 DIIDID ...
- hdu 4055 Number String(有点思维的DP)
Number String Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...
- hdu 4055 Number String (基础dp)
Number String Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- hdu 4055 Number String
Number String http://acm.hdu.edu.cn/showproblem.php?pid=4055 Time Limit: 10000/5000 MS (Java/Others) ...
- HDU 4055 Number String:前缀和优化dp【增长趋势——处理重复选数】
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4055 题意: 给你一个由'I', 'D', '?'组成的字符串,长度为n,代表了一个1~n+1的排列中 ...
- hdu 4055 Number String(dp)
Problem Description The signature of a permutation is a string that is computed as follows: for each ...
- HDU 4055 Number String (计数DP)
题意:由数字1到n组成的所有排列中,问满足题目所给的n-1个字符的排列有多少个,如果第i字符是‘I’表示排列中的第i-1个数是小于第i个数的. 如果是‘D’,则反之. 析:dp[i][j] 表示前 i ...
- hdu 4055 Number String(递推DP)
给一个只含‘I','D','?'三种字符的字符串,I表示当前数字大于前面的数字,D表示当前的数字小于前面一位的数字,?表示当前位既可以小于又可以大于. 问1~n的排列中有多少个满足该字符串. http ...
- HDU 4054 Number String
HDU 4054 Number String 思路: 状态:dp[i][j]表示以j结尾i的排列 状态转移: 如果s[i - 1]是' I ',那么dp[i][j] = dp[i-1][j-1] + ...
随机推荐
- CentOS6安装各种大数据软件 第三章:Linux基础软件的安装
相关文章链接 CentOS6安装各种大数据软件 第一章:各个软件版本介绍 CentOS6安装各种大数据软件 第二章:Linux各个软件启动命令 CentOS6安装各种大数据软件 第三章:Linux基础 ...
- python中用Pillow库进行图片处理
一.Python中 PIL 图像处理库简介 PIL可以做很多和图像处理相关的事情: 图像归档(Image Archives).PIL非常适合于图像归档以及图像的批处理任务.你可以使用PIL创建缩略图, ...
- XAMPP之Mysql启动失败
启动XAMPP中的Mysql出现如下: 可能的原因是本地有多个MySQL,所以要在注册表编辑器中将imagePath改成XAMPP中的mysql的地址.(打开注册表编辑器:win+R,输入regedi ...
- (数据科学学习手札50)基于Python的网络数据采集-selenium篇(上)
一.简介 接着几个月之前的(数据科学学习手札31)基于Python的网络数据采集(初级篇),在那篇文章中,我们介绍了关于网络爬虫的基础知识(基本的请求库,基本的解析库,CSS,正则表达式等),在那篇文 ...
- 20155212 2016-2017-2 《Java程序设计》第5周学习总结
20155212 2016-2017-2 <Java程序设计>第5周学习总结 教材学习内容总结 Chapter8 就Java在异常处理的设计上,不鼓励捕捉InputMismatchExce ...
- 20155313 2016-2017-2 《Java程序设计》第四周学习总结
20155313 2016-2017-2 <Java程序设计>第四周学习总结 教材学习内容总结 6 继承与多态 面对对象中,子类继承父类,避免重复的行为定义,不过并非为了避免重复定义行为就 ...
- 20155316 实验二《Java面向对象程序设计》实验报告
实验1 实验内容 参考 博客 完成单元测试的学习 提交最后三个JUnit测试用例(正常情况,错误情况,边界情况)都通过的截图,截图上要有画图加水印,输入自己的学号 本提交点考查JUnit会不会使用,测 ...
- 20155320《Java程序设计》实验一(Java开发环境的熟悉)实验报告
20155320<Java程序设计>实验一(Java开发环境的熟悉)实验报告 实验内容及步骤 (一)命令行下Java程序开发 步骤一:首先在cmd中输入d:和cd ljq20155320进 ...
- Firefox+Burpsuite抓包配置(可抓取https)
0x00 以前一直用的是火狐的autoproxy代理插件配合burpsuite抓包 但是最近经常碰到开了代理却抓不到包的情况 就换了Chrome的SwitchyOmega插件抓包 但是火狐不能抓包的问 ...
- 2017-2018-1 20155329《信息安全技术》实验二——Windows口令破解
2017-2018-1 20155329<信息安全技术>实验二--Windows口令破解 实验原理 口令破解方法 字典破解: 指通过破解者对管理员的了解,猜测其可能使用某些信息作为密码,利 ...