hdu4055 dp
2015-07-07 20:25
239 查看
http://acm.hdu.edu.cn/showproblem.php?pid=4055
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.
/**
hdu4055 dp
题目大意:给定一个字符串,I表示本字符要比前一个字符大,D表示本字符要不前一个字符小,?可大可小,问1~n的所有排列中,
有多少满足条件
解题思路:可以用dp[i][j]表示:处理完第i位,序列末尾位j的序列共有多少个。最后的结果为sigma{dp
[i]},1≤i≤N
处理dp[1~i][]的过程中i是依次1~n相加。处理完dp[i-1][]后,加入的数即为i,而dp[i][j]是要将i放进去j换
出来,而这里有一种将i放进去j换出来,同时不影响升降顺序的方法是:
将dp[i-1][j]的i-1个数的序列中 ≥j 的数都加1,这样i-1变成了i,j变成了j+1,而j自然就补在后面了。
所以对”ID“序列依次处理即可,初始条件:dp[1][1] = 1; 即只有{1}。
处理‘I’:dp[i][j] = sigma{dp[i-1][x]},其中1≤x≤j-1,可进一步简化,dp[i][j] = dp[i][j-1]+dp[i-1][j-1]
处理‘D’:dp[i][j] = sigma{dp[i-1][x]},其中j≤x≤i-1,可进一步简化,dp[i][j] = dp[i-1][j+1]+dp[i-1][j]
处理‘?’:dp[i][j] = sigma{dp[i-1][x]},其中1≤x≤i-1
*/
#include <stdio.h>
#include <algorithm>
#include <iostream>
#include <string.h>
using namespace std;
const int mod=1e9+7;
char a[1005];
int dp[1005][1005];
int main()
{
while(~scanf("%s",a))
{
int n=strlen(a)+1;
memset(dp,0,sizeof(dp));
dp[1][1]=1;
for(int i=2;i<=n;i++)
{
char ch=a[i-2];
if(ch=='?')
{
int sum=0;
for(int j=1;j<i;j++)
{
sum=(sum+dp[i-1][j])%mod;
}
for(int j=1;j<=i;j++)
{
dp[i][j]=sum;
}
}
else if(ch=='I')
{
for(int j=2;j<=i;j++)
{
dp[i][j]=(dp[i][j-1]+dp[i-1][j-1])%mod;
}
}
else
{
for(int j=i-1;j>=1;j--)
{
dp[i][j]=(dp[i][j+1]+dp[i-1][j])%mod;
}
}
}
int ans=0;
for(int i=1;i<=n;i++)
{
ans=(ans+dp
[i])%mod;
}
printf("%d\n",ans);
}
return 0;
}
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.
/**
hdu4055 dp
题目大意:给定一个字符串,I表示本字符要比前一个字符大,D表示本字符要不前一个字符小,?可大可小,问1~n的所有排列中,
有多少满足条件
解题思路:可以用dp[i][j]表示:处理完第i位,序列末尾位j的序列共有多少个。最后的结果为sigma{dp
[i]},1≤i≤N
处理dp[1~i][]的过程中i是依次1~n相加。处理完dp[i-1][]后,加入的数即为i,而dp[i][j]是要将i放进去j换
出来,而这里有一种将i放进去j换出来,同时不影响升降顺序的方法是:
将dp[i-1][j]的i-1个数的序列中 ≥j 的数都加1,这样i-1变成了i,j变成了j+1,而j自然就补在后面了。
所以对”ID“序列依次处理即可,初始条件:dp[1][1] = 1; 即只有{1}。
处理‘I’:dp[i][j] = sigma{dp[i-1][x]},其中1≤x≤j-1,可进一步简化,dp[i][j] = dp[i][j-1]+dp[i-1][j-1]
处理‘D’:dp[i][j] = sigma{dp[i-1][x]},其中j≤x≤i-1,可进一步简化,dp[i][j] = dp[i-1][j+1]+dp[i-1][j]
处理‘?’:dp[i][j] = sigma{dp[i-1][x]},其中1≤x≤i-1
*/
#include <stdio.h>
#include <algorithm>
#include <iostream>
#include <string.h>
using namespace std;
const int mod=1e9+7;
char a[1005];
int dp[1005][1005];
int main()
{
while(~scanf("%s",a))
{
int n=strlen(a)+1;
memset(dp,0,sizeof(dp));
dp[1][1]=1;
for(int i=2;i<=n;i++)
{
char ch=a[i-2];
if(ch=='?')
{
int sum=0;
for(int j=1;j<i;j++)
{
sum=(sum+dp[i-1][j])%mod;
}
for(int j=1;j<=i;j++)
{
dp[i][j]=sum;
}
}
else if(ch=='I')
{
for(int j=2;j<=i;j++)
{
dp[i][j]=(dp[i][j-1]+dp[i-1][j-1])%mod;
}
}
else
{
for(int j=i-1;j>=1;j--)
{
dp[i][j]=(dp[i][j+1]+dp[i-1][j])%mod;
}
}
}
int ans=0;
for(int i=1;i<=n;i++)
{
ans=(ans+dp
[i])%mod;
}
printf("%d\n",ans);
}
return 0;
}
相关文章推荐
- NYOJ 457 大小写互换
- Shell编程(脚本)的常用命令和语句
- 黑马程序员—Java多线程
- 36.如何使用定时任务
- HDU 5266 pog loves szh III (LAC)
- 36.如何使用定时任务
- redis-cli 命令总结
- Servlet中删除cookie
- 数据结构——九大排序算法二
- Android 画图详解
- 猜你喜欢-----推荐系统原理介绍
- 适配器模式 Adapter
- Vector 容器
- Hot Days Codeforces Round #132 (Div. 2) D(贪婪)
- java之Object转Map
- OC-可变数组-可变字典嵌套(练习-省市区)
- 以操作系统的角度述说线程与进程
- Mahout-HashMap的进化版FastByIdMap
- Android 中的BroadCastReceiver
- 数据结构——九大排序算法一