(dp专题003)hdu 4055 Number String(dp)

Posted by 111qqz on Sunday, November 13, 2016

TOC

/***************************************************
Author :111qqz
************************************************ */
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <string>
#include <cmath>
#include <cstdlib>
#include <deque>
#include <ctime>
#define fst first
#define sec second
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define ms(a,x) memset(a,x,sizeof(a))
typedef long long LL;
#define pi pair < int ,int >
#define MP make_pair
using namespace std;
const double eps = 1E-8;
const int dx4[4]={1,0,0,-1};
const int dy4[4]={0,-1,1,0};
const int inf = 0x3f3f3f3f;
const int N=1E3+7;
const  int mod =  1000000007;
char st[N];
int dp[N][N],sum[N][N];//dp[i][j]表示长度为i，最后结尾的字符的相对大小为j的方案数。
int main()
{
#ifndef  ONLINE_JUDGE
//  freopen("D:\code\in.txt","r",stdin);
#endif

while (~scanf("%s",st+2))
{

int len = strlen(st+2);
//      cout<<"len:"<<len<<endl;
ms(sum,0);
ms(dp,0);
dp[1][1] = 1;
sum[1][1] = 1;
for ( int i = 2 ; i <= len+1 ; i++)
{
for ( int j = 1 ; j <= i ; j++)
{
if ( st[i]=='?'||st[i]=='I') dp[i][j] = (dp[i][j] + sum[i-1][j-1])%mod;
if (st[i]=='?'||st[i]=='D')
{
int tmp = ((sum[i-1][i-1]-sum[i-1][j-1])%mod+mod)%mod;
dp[i][j] = (dp[i][j] + tmp)%mod;
}
sum[i][j] = (sum[i][j-1] + dp[i][j])%mod;
}
}
printf("%d\n",sum[len+1][len+1]);
}

#ifndef ONLINE_JUDGE
fclose(stdin);
#endif
return 0;
}


「真诚赞赏，手留余香」