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
#include
#include

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

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

你可能感兴趣的:(动态规划,NWPUACM)