首页 > 其他 > 详细

【动态规划】Concerts

时间:2019-08-21 12:25:57      阅读:125      评论:0      收藏:0      [点我收藏+]

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 (1 ≤ n ≤ 1e5), 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 1e9 + 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 1e9 + 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


【题意】

定义S串,T串。分别是输入的第一个,第二个字符串。

给出26种音乐会,每次举行完必须要休息多少天。

然后S串是必须按照这个顺序去看的音乐会。

但是然后T串是。音乐会的时间安排。

问有多少种方案满足这个自己计划。


【题解】

然后定义dp[i][j],对应的是,已经完成第i~k及以后的计划,在第j天的这一天 方案数

可能比较绕。

从后往前考虑,

1、预处理出最后计划最后一场的位置的方案数为1。

2、然后从后往前进行递推。

 

技术分享图片
 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 const int N = 1e5+10;
 4 const int M = 3e2+5;
 5 const int mod = 1e9+7;
 6 typedef long long ll;
 7 int f[N][M];
 8 int S[N],T[N];
 9 int n,k;
10 int w[30];
11 char str[N];
12 int main()
13 {
14     scanf("%d%d",&k,&n);
15     for(int i=0;i<26;i++)   scanf("%d",&w[i]);
16  
17     scanf("%s",str+1);
18     for(int i=1;str[i];i++) S[i] = str[i] - A;
19  
20     scanf("%s",str+1);
21     for(int i=1;str[i];i++) T[i] = str[i] - A;
22  
23     //初始化
24     for(int i=n;i>=1;i--){
25         f[i][k] = f[i+1][k] ;
26         if( T[i] == S[k] )
27             f[i][k] = f[i][k] + 1 ;
28     }
29  
30     for(int i=n;i>=1;i--){
31         for(int j=k-1;j>=1;j--){
32             f[i][j] = f[i+1][j];
33             if( T[i] == S[j] && i+w[T[i]]+1 <= n ){
34                 f[i][j] = (int)((ll)f[i][j] + (ll)f[i+w[T[i]]+1][j+1] )% mod ;
35             }
36         }
37     }
38      
39     printf("%d\n",f[1][1]);
40     return 0;
41 }
View Code

 

【动态规划】Concerts

原文:https://www.cnblogs.com/Osea/p/11387702.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!