hdu 4055 Number String
Number String
http://acm.hdu.edu.cn/showproblem.php?pid=4055
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
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.
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'.
satisfying the signature on a single line. In case the result is too large,
print the remainder modulo 1000000007.
Permutation {1,2,3} has signature "II".
- #include<cstdio>
- #include<cstring>
- #define mod 1000000007
- using namespace std;
- int len,f[][],sum[][];
- char s[];
- int main()
- {
- while(scanf("%s",s+)!=EOF)
- {
- memset(f,,sizeof(f));
- len=strlen(s+);
- f[][]=; sum[][]=;
- for(int i=;i<=len+;i++)
- for(int j=;j<=i;j++)
- {
- if(s[i-]=='I') f[i][j]=sum[i-][j-];
- else if(s[i-]=='D') f[i][j]=((sum[i-][i-]-sum[i-][j-])%mod+mod)%mod;
- else f[i][j]=sum[i-][i-];
- sum[i][j]=(sum[i][j-]+f[i][j])%mod;
- }
- printf("%d\n",sum[len+][len+]);
- }
- }
未优化代码
- #include<cstdio>
- #include<cstring>
- #define mod 1000000007
- using namespace std;
- int len,f[][];
- char s[];
- int main()
- {
- while(scanf("%s",s+)!=EOF)
- {
- memset(f,,sizeof(f));
- len=strlen(s+);
- f[][]=;
- for(int i=;i<=len+;i++)
- for(int j=;j<=i;j++)
- {
- if(s[i-]=='I')
- for(int k=;k<j;k++) f[i][j]+=f[i-][k];
- else if(s[i-]=='D')
- for(int k=j;k<i;k++) f[i][j]+=f[i-][k];
- else
- for(int k=;k<i;k++) f[i][j]+=f[i-][k];
- }
- int ans=;
- for(int i=;i<=len+;i++) ans+=f[len+][i];
- printf("%d\n",ans);
- }
- }
hdu 4055 Number String的更多相关文章
- HDU 4055 Number String dp
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4055 Number String Time Limit: 10000/5000 MS (Java/O ...
- 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:前缀和优化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计数)
题意: 给你一个含n个字符的字符串,字符为'D'时表示小于号,字符为“I”时表示大于号,字符为“?”时表示大小于都可以.比如排列 {3, 1, 2, 7, 4, 6, 5} 表示为字符串 DIIDID ...
- 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] + ...
随机推荐
- C语言之goto浅析
1. 读代码时遇了的疑惑点: static int do_bind(const char *host, int port, int protocol, int *family) { int fd; ...
- 【第五周】alpha发布之小组评论
对于昨天的阿尔法发布,有那么几点需要说一下: 1,对这次阿尔法发布的过程,我们组还是基本顺利的,由于之前吃过亏,这次我提前试用了一下投影仪,做了些调试,之后的发布过程起码设备上是正常的. 2,我们的项 ...
- 简单的 php 防注入、防跨站 函数
/** * 简单的 php 防注入.防跨站 函数 * @return String */ function fn_safe($str_string) { //直接剔除 $_arr_dangerChar ...
- BZOJ 1853 幸运数字(容斥原理+dfs)
题意:求闭区间内能被6和8组成的数字整除的数目.n<=1e11. 我们可以预处理出这些6和8组成的数字,大概2500个,然后排除一些如88,66的情况.这样大概还剩下1000个. 转化为[0,r ...
- MySQL5.7 初使用
MySQL 5.7.6 发布了快一年了, 官方的说法是比起前几代有很大的安全性提升,性能报告也是比之前版本提升了 2.3倍. 性能上这么大的提升,安全上也这么大的改进,怎么能不心动呢,这几天出于好奇, ...
- 线段树之Sum
题面: 给定一数列,规定有两种操作,一是修改某个元素,二是求区间的连续和. Input: 输入数据第一行包含两个正整数n,m(n<=100000,m<=500000),以下是m行, 每行有 ...
- 【POJ2891】Strange Way to Express Integers(拓展CRT)
[POJ2891]Strange Way to Express Integers(拓展CRT) 题面 Vjudge 板子题. 题解 拓展\(CRT\)模板题. #include<iostream ...
- CF449,急需提高姿势水平
这可能是我第1场只出1题的div1? A 鬼畜题? 我的dev没有字符统计功能,于是只好扔到notepad++上.数错好几次.题目本身没什么特别的地方. B 当时一直没想出来QAQ 题目中说m> ...
- python之快速排序
快速排序(Quicksort)是对冒泡排序的一种改进. 快速排序由C. A. R. Hoare在1962年提出.它的基本思想是:通过一趟排序将要排序的数据分割成独立的两部分,其中一部分的所有数据都比另 ...
- 驱动之DMA的介绍与应用20170210
本文主要介绍的是DMA相关的知识,首先: 1)在实现DMA传输时,是由DMA控制器直接掌管总线,因此,存在着一个总线控制权转移问题.即DMA传输前,CPU要把 总线控制权交给DMA控制器,而在结束DM ...