Concerts
John enjoys listening to several bands, which we shall denote using A through Z. He
wants to attend several concerts, so he sets out to learn their schedule for the upcoming season.
He finds that in each of the following n days (n ≤ 104
), there is exactly one concert. He decides to
show up at exactly k concerts (k ≤ 103
), 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.
Input
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.
Output
The number of ways in which he can schedule his attendance (mod 109 + 7).
Sample input Sample output
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
题目给的数组大小有误,注意数组大小
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int M = 1e9 + 7;
int k, n, t;
int c[26];
int s[100005][305]; //按照题目给的大小开数组,很可能会Runtime Error
char a[305];
char b[100005];
int main() {
ios::sync_with_stdio(false);
std::cin.tie(0);
cin >> k >> n;
for (int i = 0; i < 26; i++) cin >> c[i];
cin >> (a + 1) >> (b + 1);
for (int j = 1; j <= k; j++) {
for (int i = 1; i <= n; i++) {
if (b[i] == a[j]) {
if (j == 1)
s[i][j] = (LL)(s[i - 1][j] + 1) % M;
else {
t = c[a[j - 1] - 'A'] + 1;
if (i - t <= 0)
s[i][j] = s[i - 1][j];
else
s[i][j] = (LL)(s[i - 1][j] + s[i - t][j - 1]) % M;
}
} else
s[i][j] = s[i - 1][j];
}
}
cout << s[n][k];
}