Southeastern European Regional Programming Contest Bucharest, Romania – Vinnytsya, Ukraine October 21, 2017
Codeforces: Problem A Concerts
题目链接:http://codeforces.com/gym/101669/attachments
解题心得:
- 就是一个很简单的dp,很简单的就能想到如果是要得到方案数,那么肯定使用dp叠加起来的,因为内存限制所以只能是滚动数组
- dp[now][j] += dp[now][j-1] + dp[pre][i-hb[i-1]]
- j代表给出的长度为n的字符串,i代表观看顺序的目标字符串。
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5+100;
const int mod = 1e9+7;
char apper[10010], s[maxn];
long long dp[2][maxn];
int hb[30], n, k;
void init() {
scanf("%d%d",&k, &n);
for(int i=0;i<26;i++) {
scanf("%d",&hb[i]);
}
for(int i=0;i<=n;i++) {
dp[0][i] = 1;
}
scanf("%s", apper+1);
scanf("%s", s+1);
}
int main() {
//freopen("1.in", "r", stdin);
//freopen("1.out", "w", stdout);
init();
int pos = 0;
for(int i=1;i<=k;i++) {
dp[pos^=1][0] = 0;
for(int j=1;j<=n;j++) {
dp[pos][j] = dp[pos][j-1];
if(apper[i] == s[j] && j-1-hb[apper[i-1] - 'A'] >= 0)
dp[pos][j] += dp[pos^1][j-1-hb[apper[i-1]-'A']];
dp[pos][j] %= mod;
}
}
printf("%lld", dp[pos][n]);
return 0;
}