UPC 5130 Concerts

UPC 5130 Concerts

标签:动态规划


Description

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 (1 ≤ n ≤ \(10^5\)), 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 \(10^9 + 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

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

Sample Output

10

分析

  • 因为每次参加音乐后之后都要等待几天才能参加其他的,所以倒着DP会更方便一些
  • \(dp[i][j]\)来表示第\(i-n\)场音乐会中,参加了第\(j-k\)个乐队的方案数。
  • 可以得到转移方程为,\(dp[i][j]=dp[i+1][j]+dp[i+1+hb(i)][j+1]\),其中\(hb(i)\)为第i场音乐会对应的乐队的hb值。

代码

展开 ```cpp #include #include #include #include #include #include #include using namespace std; typedef long long ll; const ll MOD=1e9+7; const int maxn=100005; int dp[maxn][305]; int w[30]; char s1[maxn],s2[305]; int main(int argc, char const *argv[]) { int k,n; scanf("%d%d", &k,&n); for (int i = 0; i < 26; ++i) { scanf("%d", w+i); } scanf("%s", s2+1); scanf("%s", s1+1); ll ans=0; for (int i = n; i ; --i) { for (int j = k; j ; --j) { int &u=dp[i][j]; u=dp[i+1][j]; if(j==k&&s1[i]==s2[j]) u=(u+1)%MOD; else if(i+w[s1[i]-'A']+1<=n&&s1[i]==s2[j]) u=((ll)dp[i+w[s1[i]-'A']+1][j+1]+(ll)u)%MOD; } } printf("%d\n", dp[1][1]); return 0; } ```

转载于:https://www.cnblogs.com/sciorz/p/8955880.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值