Gym101669A Concerts(dp,字符串)

Input File: A.in
Output File: standard output
Time Limit: 0.3 seconds (C/C++)
Memory Limit: 128 megabytes

    John enjoys listening to several bands, which we shall denote using A through Z. Hewants 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
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 (single line)
AB
ABBBBABBBB

Sample output
10

 

2017 Europe - Southeastern。。。_(:з」∠)_

题目大意:音乐会共有n场,打算按顺序参加k场,每参加完一场都要在家休息h的时间(每种音乐会对应的h不相同),问一共有多少种参加方式。1<=n<=1e5,1<=k<=300

题解:刚开始用记忆化搜索写的……TLE……而且代码还长= =直接用dp写就能过,但是开的dp数组不能过大。我开的(1e5+2)*30+2用了118.3MB= =从后往前dp即可。

#include<iostream>
#include<cstdio>
#include<cstring>

using namespace std;

const int MOD=1e9+7;
char add[302];
char sch[100002];
int h[30];
int dp[302][100002];
int n, k;

int main()
{
    int i, j;
    int x;
	while(cin>>k>>n)
    {
        for(i=0;i<26;i++) cin>>h[i];
        cin>>add;
        cin>>sch;
        memset(dp, 0, sizeof(dp));
        for(i=n-1; i>=0; i--)
        {
            dp[k-1][i]=dp[k-1][i+1];
            if(sch[i]==add[k-1])
                dp[k-1][i]=(dp[k-1][i]+1)%MOD;
        }
        for(i=n-1; i>=0; i--)
        {
            x=h[sch[i]-'A']+1;
            for(j=k-2; j>=0; j--)
            {
                dp[j][i]=dp[j][i+1];
                if(sch[i]==add[j] && i+x<n)
                    dp[j][i]=(dp[j][i]+dp[j+1][i+x])%MOD;
            }
        }
        cout<<dp[0][0]<<endl;
    }
	return 0;
}

i j那个嵌套循环里面i和j不能调换,必须从n字符串的最后一位开始搜索k字符串。。应该是极限的问题【胡说】

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值