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] + ...
随机推荐
- ubuntu安装和查看已安装软件
说明:由于图形化界面方法(如Add/Remove... 和Synaptic Package Manageer)比较简单,所以这里主要总结在终端通过命令行方式进行的软件包安装.卸载和删除的方法. 一.U ...
- 对其中的一个特点将NABC的分析结果
一.题目要求 每一个组员针对其中的一个特点将NABC的分析结果发表博客上(截止日期4月8日晚24:00前). 二.分析结果 特点之一:通讯方便 <渴了么>这个安卓APP特点之一就是通讯方便 ...
- A8
组员:陈锦谋 今日内容: PS学习.抠图.图标像素调整 明日计划: 继续小组内安排的任务 困难: 无
- Codeforces Beta Round #8 C. Looking for Order 状压dp
题目链接: http://codeforces.com/problemset/problem/8/C C. Looking for Order time limit per test:4 second ...
- cobbler-web 网络安装服务器套件 Cobbler(补鞋匠)
Cobbler作为一个预备工具,使部署RedHat/Centos/Fedora系统更容易,同时也支持Suse和Debian系统的部署. 它提供以下服务集成: * PXE服务支持 * DHCP服务管 ...
- HTML&CSS实体
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...
- jetty之maven配置
<!-- jetty 插件配置 --><plugin> <groupId>org.mortbay.jetty</groupId> <artifac ...
- ZOJ3084_S-Nim
题目的意思是这样的,给定你若干堆石子,每次你可以从任一堆取出某些固定数量的石子,每次取完后必须保证没堆石子的数量不为0,谁无法操作了就算fail. 刚刚开始看题目的时候有点也没有思路,甚至连Sg函数也 ...
- An In-Depth Look at the HBase Architecture--转载
原文地址:https://www.mapr.com/blog/in-depth-look-hbase-architecture In this blog post, I’ll give you an ...
- The Largest Clique UVA - 11324( 强连通分量 + dp最长路)
这题 我刚开始想的是 缩点后 求出入度和出度为0 的点 然后统计个数 用总个数 减去 然而 这样是不可以的 画个图就明白了... 如果 减去度为0的点 那么最后如果出现这样的情况是不可 ...