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;
}