Concerts 题解

Concerts

时间限制: 1 Sec 内存限制: 128 MB
题目描述
John enjoys listening to several bands, which we shall denote using A through Z. He wants to attend several con certs, so he sets out to learn their schedule for the upcoming season.
He finds that in each of the following n days (1 ≤ n ≤ 105), there is exactly one concert. He decides to show up at exactly k concerts (1 ≤ k ≤ 300), in a given order, and he may decide to attend more than one concert of the same band.
However, some bands give more expensive concerts than others, so, after attending a concert given by band b, where b spans the letters A to Z, John decides to stay at home for at least hb days before attending any other concert.
Help John figure out how many ways are there in which he can schedule his attendance, in the desired order. Since this number can be very large, the result will be given modulo 109 + 7.

输入
The first line contains k and n. The second line contains the 26 hb values, separated by spaces.
The third line contains the sequence of k bands whose concerts John wants to attend e.g.,AFJAZ, meaning A, then F etc. The fourth line contains the schedule for the following n days,specified in an identical manner.

输出
The number of ways in which he can schedule his attendance (mod 109 + 7).

样例输入
2 10
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
AB
ABBBBABBBB
样例输出
10

题意就不说了很显然
考虑用O(n*k)的做法
自然想到dp
开始队友提出了要记录前缀和
然后发现数组开不下
听说有大佬用了滚动数组过的
然后我就考虑从后往前推
可以很自然想到递推公式
dp[i][j] = dp[i+1][j] + dp[i + hb[i] + 1][j+1]
hb[i]是第i天的音乐会听完之后要休息的天数
然后就很简单了

#include <bits/stdc++.h>
#define LL long long
using namespace std;
 
const LL p = 1000000007;
const int N = 100000 + 5;
const int K = 300 + 5;
 
int n, k, f[N][K], h[30], hb[N];
char sk[K], sn[N];
 
int main()
{
    scanf("%d%d", &k, &n);
    for (int i = 0; i < 26; i++) scanf("%d", &h[i]);
    scanf("%s%s", sk + 1, sn + 1);
    for (int i = 1; i <= n; i++) hb[i] = h[sn[i] - 'A'];
    for (int i = 1; i <= n; i++) if (sn[i] == sk[k]) f[i][k] = 1;
    for (int i = n; i >= 1; i--)
        for (int j = k; j >= 1; j--) {
            f[i][j] += f[i+1][j];
            if (sk[j] == sn[i] && i + hb[i] + 1 <= n)
                f[i][j] = (f[i][j] + f[i + hb[i] + 1][j+1]) % p;
            //printf("i = %d j = %d f[i][j] = %d\n",i,j,f[i][j]);
        }
    printf("%d\n", f[1][1]);
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值