codeforces 474D DP


 Flowers    CodeForces - 474D


题目原文:We saw the little game Marmot made for Mole's lunch. Now it's Marmot's dinner time and, as we all know, Marmot eats flowers. At every dinner he eats some red and white flowers. Therefore a dinner can be represented as a sequence of several flowers, some of them white and some of them red.

But, for a dinner to be tasty, there is a rule: Marmot wants to eat white flowers only in groups of size k.

Now Marmot wonders in how many ways he can eat between a and b flowers. As the number of ways could be very large, print it modulo 1000000007 (109 + 7).


题目大意:在长度l在a和b之间的字符串中找到连续k个白色小花。问这种的字符串有多少种。


解题思路:这道题按照样例先写出 0 到 5 的最大种数。


发现得到的字符串分别是:


1 R  没有也满足,因为满足0个k连续。

2 RR  WW

3 RRR WWR RWW

4 RRRR RWWR  RRWW WWRR WWWW

5 RRRRR RRRWW RRWWR RWWRR  WWRRR WWWWR WWRWW RWWWW


感觉使用dp做 然后估了估 分两种 最后第i个位子取R 和最后第i个位子取W

如果取R 那我前面i-1的字符串是要满足最大 与我第i个位子上的R无关

如果取W 那我前k-1个字符必须取W 否则不满足题意 那我前i-k满足最大值即可


所以选择策略为dp【i】=dp【i-k】+ dp【i-1】


#include
#include
using namespace std;
const int MOD=1e9+7;
const int MAXN=1e5+5;
int dp[100005];
int sum[MAXN];
int k;
void preoperation()
{
    dp[0]=0;
//    cout <<'*'<< k <> t >> k;
    preoperation();
    for(int i=1;i<=t;i++)
    {
        int a,b;
        cin >> a >>b ;
        cout << ((sum[b]-sum[a-1])%MOD+MOD)%MOD<


你可能感兴趣的:(动态规划)