hdu 4055(经典问题)
总是不能正确的将一个大问题变成子问题,而且又找不到状态转移方程。 直接导致这题想了5个小时最后还是无果。。。
谨记!
Number String
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1136 Accepted Submission(s): 503
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 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'.
ID
DI
DD
?D
??
2
2
1
3
6
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.
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
#define N 1100
#define MOD 1000000007 char g[N];
int dp[][N]; int main()
{
while(scanf("%s",g)!=EOF)
{
memset(dp,,sizeof(dp));
int a=,b=;
int len=strlen(g);
dp[a][]=;
int ti=;
for(int i=;i<=len+;i++)
{
for(int j=;j<=i;j++)
{
if(g[ti]=='I')//以j结尾的。 所有情况
{
dp[b][j] = (dp[b][j]+dp[a][j-]);
if(dp[b][j]>=MOD) dp[b][j]-=MOD;
if(dp[b][j]<) dp[b][j]+=MOD;
}
else if(g[ti]=='D')
{
dp[b][j]= (dp[b][j]+dp[a][i-]-dp[a][j-]);
if(dp[b][j]>=MOD) dp[b][j]-=MOD;
if(dp[b][j]<) dp[b][j]+=MOD;
}else
{
dp[b][j]= (dp[b][j]+dp[a][i-]);
if(dp[b][j]>=MOD) dp[b][j]-=MOD;
if(dp[b][j]<) dp[b][j]+=MOD;
}
}
swap(a,b);
for(int j=;j<=i;j++)
{
dp[b][j]=;
dp[a][j]=(dp[a][j]+dp[a][j-]);
if(dp[a][j]>=MOD) dp[a][j]-=MOD;
if(dp[a][j]<) dp[a][j]+=MOD;
}
ti++;
}
//for(int i=1;i<=len+1;i++)
// ans= (ans+dp[a][i])%MOD;
printf("%d\n",(dp[a][len+]%MOD+MOD)%MOD);
}
return ;
}
hdu 4055(经典问题)的更多相关文章
- hdu 4055 && hdu 4489 动态规划
hdu 4055: 一开始我想的递推方向想得很复杂,看了别人的博客后才醍醐灌顶: 参照他的思路和代码: #include<cstdio> #include<cstring> # ...
- HDU 4055 The King’s Ups and Downs(DP计数)
题意: 国王的士兵有n个,每个人的身高都不同,国王要将他们排列,必须一高一矮间隔进行,即其中的一个人必须同时高于(或低于)左边和右边.问可能的排列数.例子有1千个,但是最多只算到20个士兵,并且20个 ...
- 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
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计数)
http://acm.hdu.edu.cn/showproblem.php?pid=4055 题意:给一个仅包含‘I','D','?'的字符串,’I'表示前面的数字比后面的数字要小(Increase升 ...
- hdu 3943 经典数位dp好题
/* 题意:求出p-q的第j个nya数 数位dp,求出p-q的所有nya数的个数很好求,但是询问求出最终那个第j个值时是我不会求了看了下别人的思路 具体就是把p-q的第j个转化成0-q的第low+j个 ...
- hdu 4055 递推
转自:http://blog.csdn.net/shiqi_614/article/details/7983298 题意:由数字1到n组成的所有排列中,问满足题目所给的n-1个字符的排列有多少个,如果 ...
- HDU 1176 经典dp
记录最晚时间 从time为2枚举到最晚时间 每个时间段的x轴节点都等于上一个时间段的可触及的最大馅饼数 #include<stdio.h> #include<string.h> ...
随机推荐
- hibernate3和4中 HibernateSessionFactory中不同之处 The method applySettings(Map) from the type ServiceRegistryBuilder is deprecated - The type ServiceRegistryBuilder is deprecated
hibernate3和4中 HibernateSessionFactory中不同之处 //serviceRegistry = new ServiceRegistryBuilder().applySet ...
- unity3d协同不同设备的代码
unity3d的脚本代码中,Update()函数每一帧都会运行一次. 假设有这两台设备:一台超级四路泰坦计算机,一台旧手机,它们一起运行一个赛跑游戏,Update()函数每一帧运行一次,游戏中的小人就 ...
- winform最小化及关闭提示
public PrintService() { InitializeComponent(); this.WindowState = FormWindowState.Minimized; } priva ...
- 关于Tomcat 开启不了的几点解释
这段时间基本熟悉java语言基本语法包,类(内部,外部),整体结构跟c#还是有点差异,在接口,多态,抽象等几乎一致,唯一差异仅存在于关键字上. 在用了几天记事本熟悉代码上,昨天晚上就准备转想myEcl ...
- Atitit .linux 取回root 密码q99
Atitit .linux 取回root 密码q99 1.1. 停止mysql1 1.2. mysqld_safe路径1 1.3. Mysql配置文件路径1 1.4. Mysql路径1 1.5. 安全 ...
- Atitit.数据操作dsl 的设计 ---linq 方案
Atitit.数据操作dsl 的设计 ---linq 方案 1.1. sql与api方式1 1.2. Linq方案与stream方案的选择,1 1.3. 前缀表达式 vs 中缀表达式1 1.4. 要不 ...
- Unix删除当前目录可执行文件
On GNU versions of find you can use -executable: find . -type f -executable -printFor BSD versions o ...
- (译)Getting Started——1.1.1 Start Developing IOS Today(开始IOS开发)
安装 本课程对于创建运行在iPad.iPhone和iPod触摸屏上的应用来说,是一个完美的起点.该向导的四个板块可以作为构建你第一个应用的简单向导——内容包括了你需要使用的工具,主要的理念 ...
- Struts2的简单使用
一.准备工作及实例 1.解压struts-2.1.6-all.zip apps目录:struts2自带的例子程序 docs目录:官方文档. lib 目录:存放所有jar文件. Src 目录:源文件存放 ...
- linux下安装python的第三方module
1.首先需要有python环境 2.安装pip软件:下载地址,https://pypi.python.org/pypi/pip/6.0.8 解压pip的压缩包:sudo tar -zxvf pip-6 ...