首页 技术 正文
技术 2022年11月15日
0 收藏 701 点赞 2,205 浏览 2219 个字

Problem DescriptionThe 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. InputEach 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’. OutputFor 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 InputII
ID
DI
DD
?D
?? Sample Output1
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.

 AuthorHONG, Qize 

2011 Asia Dalian Regional Contest

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

思路:刚开始完全没有思路。。。

其实做dp的话首先一定要确定好状态转移方程

状态转移方程: dp[i][j]表示在i个数时以j结尾的方案数,那么可以得到:

当s[i]=’I’或’?’时(表示增加),那么dp[i][j]+=dp[i-1][k](1=<k<j)

当s[i]=’D’或’?’时(表示减少),那么dp[i][j]+=dp[i-1][k](i>k>=j)

但是这样时间复杂度是O(n^3),会超时啊,所以引入sum[][]数组来记录前缀,使时间降为O(n^2)

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<stdlib.h>
using namespace std;
#define N 1006
#define MOD 1000000007
char s[N];
int dp[N][N];//dp[i][j]表示在这个排列中第i个数字以j结尾的,满足条件的子排列有多少个。
int sum[N][N];
int main()
{
while(scanf("%s",s+)!=EOF)
{
int n=strlen(s+);
memset(dp,,sizeof(dp));
memset(sum,,sizeof(sum));
dp[][]=sum[][]=;
for(int i=;i<=n+;i++)
{
for(int j=;j<=i;j++)
{
if(s[i]=='I' || s[i]=='?')
{ dp[i][j]=dp[i][j]+sum[i-][j-];
dp[i][j]%=MOD;
}
if(s[i]=='D' || s[i]=='?')
{ dp[i][j]=dp[i][j]+(sum[i-][i-]-sum[i-][j-])%MOD+MOD;
dp[i][j]%=MOD;
}
sum[i][j]=(sum[i][j-]+dp[i][j])%MOD;
} } printf("%d\n",sum[n+][n+]);
}
return ;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,965
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,486
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,331
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,114
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,747
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,781