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)

Problem Description
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.
 
 
Input
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'.
 
 
Output
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.
 
Sample Input
II
ID
DI
DD
?D
??
 
Sample Output
1
2
2
1
3
6
 
Hint

Permutation {1,2,3} has signature "II".

Permutations {1,3,2} and {2,3,1} have signature "ID".
Permutations {3,1,2} and {2,1,3} have signature "DI".
Permutation {3,2,1} has signature "DD".
"?D" can be either "ID" or "DD".
"??" gives all possible permutations of length 3.
 
 
题意:
给出长为n的一个字符串,根据这个字符串构造长为n+1的数字串
字符为‘I’,下一个数字要比这一个数字大
字符为‘D’ ,下一个数字要比这一个数字小
字符为‘?’,下一个数字没有限制
问数字串有多少种方案
 
一般套路:
f[i][j]表示长为i的串中,最后一个数字为j的方案数
若字符为‘I’,f[i][j]= Σ f[i-1][x]   1 <= x < j     
若字符为‘D’,f[i][j]= Σ f[i-1][x]  j <= x <= i-1  
若字符为‘?’,f[i][j]=Σ f[i-1][x]  1 <= x <= i-1
我们虽然根据 字符保证了相邻两个数字的大小关系,但并没有保证数字串里前i-1个数字没有数字j
这就有了后效性。怎么办?
给状态增加含义:必须选前i-1个数字
我们将过程想象为一个一个填数字的过程
那么由f[i-1][]向f[i][]的转移,就是在末尾(i位置)填上数字i
(因为状态的定义是必须选前i-1个数字)
那么岂不是只能填数字i?字符为‘D’时不就错了吗?第二维不就没有用吗?
我们考虑第二维j(要填的最后一个数字),
我们是否可以将填数字i转换到填数字j,
那么状态转移的时候,就要想如何填j使相邻数字的大小关系不变
将前i-1个数字>=j的都+1,这样就拿出了j,放在最后面
所以,上方状态转移成立
时间复杂度?O(n³) TLE
 
前缀和优化,时间复杂度O(n²)
其实很简单,照着原方程x的取值范围写就行
 1 <= x < j     前缀和就是sum[i-1][j-1]
j <= x <= i-1  前缀和就是 sum[i-1][i-1]-sum[i-1][j-1]
 1 <= x <= i-1  前缀和就是sum[i-1][i-1]
前缀和的更新: sum[i][j]=(sum[i][j-1]+f[i][j])%mod;
 
前缀和优化代码
#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的更多相关文章

  1. HDU 4055 Number String dp

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4055 Number String Time Limit: 10000/5000 MS (Java/O ...

  2. hdu 4055 Number String(有点思维的DP)

    Number String Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) T ...

  3. hdu 4055 Number String (基础dp)

    Number String Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  4. HDU 4055 Number String:前缀和优化dp【增长趋势——处理重复选数】

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4055 题意: 给你一个由'I', 'D', '?'组成的字符串,长度为n,代表了一个1~n+1的排列中 ...

  5. hdu 4055 Number String(dp)

    Problem Description The signature of a permutation is a string that is computed as follows: for each ...

  6. HDU 4055 Number String (计数DP)

    题意:由数字1到n组成的所有排列中,问满足题目所给的n-1个字符的排列有多少个,如果第i字符是‘I’表示排列中的第i-1个数是小于第i个数的. 如果是‘D’,则反之. 析:dp[i][j] 表示前 i ...

  7. HDU 4055 Number String(DP计数)

    题意: 给你一个含n个字符的字符串,字符为'D'时表示小于号,字符为“I”时表示大于号,字符为“?”时表示大小于都可以.比如排列 {3, 1, 2, 7, 4, 6, 5} 表示为字符串 DIIDID ...

  8. hdu 4055 Number String(递推DP)

    给一个只含‘I','D','?'三种字符的字符串,I表示当前数字大于前面的数字,D表示当前的数字小于前面一位的数字,?表示当前位既可以小于又可以大于. 问1~n的排列中有多少个满足该字符串. http ...

  9. 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] + ...

随机推荐

  1. Beta周王者荣耀交流协会第三次Scrum会议

    1.立会照片 成员王超,高远博,冉华,王磊,王玉玲,任思佳,袁玥全部到齐. master:王玉玲 2.时间跨度: 2017年11月12日 18:00 — 18:20 ,总计20分钟. 3.地点: 一食 ...

  2. tensorflow训练线性回归模型

    tensorflow安装 tensorflow安装过程不是很顺利,在这里记录一下 环境:Ubuntu 安装 sudo pip install tensorflow 如果出现错误 Could not f ...

  3. HDU 5875 Function 优先队列+离线

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5875 Function Time Limit: 7000/3500 MS (Java/Others) ...

  4. web压力测试_(收集)

    作者:ZeldaZzz链接:http://www.zhihu.com/question/19867883/answer/89775858来源:知乎著作权归作者所有,转载请联系作者获得授权. 一个完整的 ...

  5. lintcode-206-区间求和 I

    206-区间求和 I 给定一个整数数组(下标由 0 到 n-1,其中 n 表示数组的规模),以及一个查询列表.每一个查询列表有两个整数 [start, end] . 对于每个查询,计算出数组中从下标 ...

  6. TFTP服务 简单文件传输协议)是TCP/IP协议族中的一个用来在客户机与服务器之间进行简单文件传输的协议,默认端口号为69

    (1)yum安装:tftp.tftp-server   (2)启动tftp CentOS 6 service xinetd restart chkconfig tftp on CentOS 7 sys ...

  7. MMU 和 MPU的区别

    S3C2440里面带的是MMU,而现在流行的Cortex-M3/4 里面带的是MPU. MMU vs MPU 内存是现代计算机最重要的组件之一.因此,它的内容不能被任何错误的应用所篡改.这个功能可以通 ...

  8. Qt使用QNetworkAccessManager实现Ftp操作

    版权声明:若无来源注明,Techie亮博客文章均为原创. 转载请以链接形式标明本文标题和地址: 本文标题:Qt使用QNetworkAccessManager实现Ftp操作     本文地址:http: ...

  9. 【Linux 命令】- tail命令

    linux tail命令用途是依照要求将指定的文件的最后部分输出到标准设备,通常是终端,通俗讲来,就是把某个档案文件的最后几行显示到终端上,假设该档案有更新,tail会自己主动刷新,确保你看到最新的档 ...

  10. 框架整合小小总结【SSH】注解式

    Spring 注解式注册 bean: 大致分为以下几步: 开启 context 空间支持 开启自动扫描功能,指定扫描包路径 使用注解配置 bean (使用@Component 注解) 给 bean 注 ...