"Algorithmic Learning note" 42. Positive and negative DP filling continuous SJTU OJ 1285 when the rain is fine

Source: Internet
Author: User

1285. When it is sunny and rainy description

Taring like sunny days, but also like rain.

Taring said: I want to experience the continuous K-day clear, go hiking, to sing aloud, I also want to this k a sunny day, then to experience the continuous K days of the rain, to feel rain when the light language. This is the most happy thing to make taring. Other time, Taring will write the code silently in the machine room.

Of course, taring don't want to be disturbed by the affairs of the computer room or by the change of natural weather in this continuous K-sunny day and continuous K-rainy days. In other words, the K-Sunny and K-rainy days must be continuous, but they do not need time in succession. Obviously, taring if in the sense of a continuous k days after the clear, go to the room to write code, waiting for the rain to start his sentiment again, this will not affect the mood of taring.

Taring weather conditions for the most recent consecutive N days (weather conditions are only "fine" and "Rain"), but the weather conditions for other days are unknown.

He would like to know how many different weather conditions are possible (for both weather conditions, if there is a different weather situation on any one day, even if the weather conditions vary), so that he can complete both experiences.

Input Format

Enter a total of two rows.

The 1th line has 2 integers, each representing the N and K described above.

Line 2nd is a string of length n, containing only B (clear), W (Rain), X (unknown) three characters.

Output Format

Outputs an integer ans that represents the number of legitimate weather schemes.

Since ans may be large, please add the answer mod 1,000,000,007 (9+7)

Sample Input 1
4 2XXXX
Sample Output 1
1
Sample Input 2
10 2XXBXXWXXXX
Sample Output 2
166

 

Original title: Http://codeforces.com/contest/204/problem/D

Official: http://codeforces.com/blog/entry/4849

According to the official analysis of the general idea is as follows:

B[I] Sequential DP Indicates the number of fill methods for K consecutive B that did not appear until I position

C[i] Sequential DP represents the number of fill methods for the first occurrence of a continuous K-B at the I position position

W_1[i] Reverse DP means the number of fill methods with k consecutive w not appearing from N to i position

W_2[I] Reverse DP Indicates the number of k consecutive w fills that have occurred from N to i position

TOTAL[I] Reverse DP Indicates how many fill methods are used when starting from n position to end of I position

1th Step seeking b[]

First Initialize b[0] = 1; and s[0]= ' 0 ';//This step is for the future of the judgment is more convenient

1.1 Determining the type of current processing location

1.1.1 If you encounter an indeterminate bit X, let b[i]= 2 * b[i-1] because there can be two fill methods here, of course, there may be some wrong padding under processing

1.1.2 If you encounter a certain bit then let b[i] = 1 * b[i-1] because there is only one method of filling, of course, it will also produce some wrong padding

1.2 Record the number of consecutive B in the current position continous_b[i]

1.3 If Continous_b[i]>=k and s[i-k] are not ' B '

To calculate the wrong number of padding bad

Because it is a DP process, we assume that B[0]~b[i-1] 's calculations are correct, then the wrong padding refers to the case where the i-k start to the I position happens to be a continuous K-B.

According to the example is the case of XXXXXWBB, the previous xxxxx fill number is b[i-k-1] behind the WBB is a fixed part of * 1

So bad = 1 * b[i-k-1]

(Note the special case if the i=k is exactly the first K characters can achieve K B, then let bad be 1)

Finally let b[i] = b[i]-bad;

2nd Step seeking c[]

2.1 c[0]=0

2.2 C[i] is actually the 1th step in the bad difference is that if the situation is not satisfied with another c[i]=0

If you combine c[] with b[, you don't need a continous_b array, you need only one number.

3rd Step seeking w_1[]

The same idea as the 1th step, different directions.

4th Step seeking total[]

2 When you encounter X in reverse order

5th Step seeking w_2[]

The idea of subtracting total from w_1 and/or replenishing the set

6th. Finding the final result ans

Note here: We have got c[] and w[], c[] Indicates the number of fill methods for the first occurrence of K-B at position I, w[] indicates the number of successive K-W methods in the Post J, which is seen to traverse I from 1 to n

Ans + = c[i] * w[i+1], the reason is c[] has finished the heavy work!

Ps:mod, there is a detail to note is the subtraction of the modulo to add a mod to take the modulo, the reason: the subtraction between the remainder of the operation may lead to the existence of negative numbers!

The code is as follows:

#include <iostream>using namespacestd;Const intMAXN = (int) 1e6+5;Const intMod = (int) 1e9+7; typedef unsignedLong LongULL;CharS[MAXN]; ULL B[MAXN],C[MAXN],W[MAXN],CONTINOUS_B[MAXN],TOTAL[MAXN];//Continous_b records the number of occurrences of a continuous K-B at the I position .intMainintargcChar Const*argv[]) {    intN,k; Cin>>n>>K; s[0]='0';//In order to determine the critical condition at the time of calculation BCin>> (s+1);//For subscript alignment, input starting from 1//b[] B[i] represents the number of cases where a continuous K-B is not present from 1 to Ib[0] =1;  for(inti =1; I <= N; ++i) {//if it is x because you can fill 2 kinds of, so multiply 2 Note: This time b[i] is bad filling//This bad filling means that the first I position has a continuous K B//Assuming that the previous B was all right, then this bad filling has only one situation where it just fills in B and satisfies the first k is B.B[i] = (s[i]=='X') ?2* b[i-1]% Mod:1* b[i-1]; continous_b[0]=0; //in order to find out if there is a bad filling, we want to record the number of consecutive B that appears before I        Switch(S[i]) { Case 'X':             Case 'B'://can be furtherContinous_b[i] = continous_b[i-1]+1;  Break;  Case 'W'://it's broken.Continous_b[i] =0; Continue;//It's going to be the next cycle, because it's not bad filling.        }        //determine if a bad filling is present        if(Continous_b[i] >= K and s[i-k]!='B'){//there's a continuous K-B.//1 of the 1 * b[i-k-1] on the right side of this is the case of WBB: Only one, b[i-k-1] represents the absence of a continuous K-B in the former i-k-1//##### Focus: Multiplying the two is called bad fillings            intBad ; if(I >k) { Bad=1* b[i-(k +1)]; }Else if(i==k) { Bad=1*1;//Special Critical conditions} C[i]= i-k-1>=0? b[i-k-1] :1 ; B[i]= (B[i]-bad + Mod)%Mod; }ElseC[i]=0; }//b[] and continous_b[] are calculated at this time.//c[] CI represents the number of cases where a continuous K-B is present to the position I (for the purpose of de-emphasis) or it can be placed in the previous loop .//c[0] = 0; //for (int i = 1; I <= n; ++i)// {    //if (continous_b[i]>=k and s[i-k]!= ' B ')//C[i] = i-k-1>=0? B[i-k-1]: 1; //Else//C[i] = 0; // }    //now W[i], W[i] represents the number of cases from the last position to the I cutoff with K consecutive W and the first step similar to the last plus a complement.//now the w[i] is not the real resultw[n+2] =1;//for conveniencew[n+1] =1; S[n+1] ='0'; intContinous_w =0;  for(inti = n; I >=0; --i) {W[i]= (s[i]=='X') ?2* w[i+1]% mod:w[i+1]; Switch(S[i]) { Case 'X':             Case 'W': Continous_w++;  Break;  Case 'B': Continous_w=0; Continue;//don't judge if there's bad fullings.        }        if(Continous_w >= K and s[i+k]! ='W') W[i]= (W[i]-w[i+k+1] + Mod)%Mod; }    //record the total number of casestotal[n+1]=1;  for(inti = n; I >=0; --i) {Total[i]= (s[i]=='X') ?2* total[i+1]% mod:total[i+1]; }        //transform to complement set     for(inti =1; I <= N; ++i) w[i]= (Total[i]-w[i] + Mod)%Mod; ULL ans=0;  for(inti =1; I <= N-1; ++i) {ans= (ans + c[i] * w[i+1]) %Mod; } cout<<ans<<Endl; //for (int i = 1; I <= n; ++i)// {    //cout<<b[i]<< ""; //}cout<<endl; //for (int i = 1; I <= n; ++i)// {    //cout<<c[i]<< ""; //}cout<<endl; //for (int i = 1; I <= n; ++i)// {    //cout<<w[i]<< ""; //}cout<<endl;    return 0;}/*2XXBXXWXXXX 3 2*/

"Algorithmic Learning note" 42. Positive and negative DP filling continuous SJTU OJ 1285 when the rain is fine

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.