hiho1482 Attendance Record II

Source: Internet
Author: User

Topic 2: Attendance Record II time limit: 10000ms single point time limit: 1000ms memory limit: 256MB description

Small Hi algorithm teacher every class will count small hi's attendance record. Late will be recorded an L, the absence will be recorded a, on time class will be recorded an O.

At the end of the semester, a little hi's attendance record can be seen as a string containing only Lao, such as "Oooolooolallo ...".

If little hi absent in the whole semester not more than 1 times, and not 3 consecutive times, small hi's attendance record even qualified.

Now given the length of the string n, little hi would like to know how many of the qualifying records are in the attendance record of length n.

For example, there are 19 eligible attendance records of length 3: OOO OOL OOA OLO OAO LOO AOO OLL OLA OAL LOL LOA AOL LLO LAO ALO LLA LAL all. input

An integer n (1 <= n <= 100000). Output

The total number of qualified records with a length of N. As the result can be large, you only need to output the remainder of the result modulo 109+7. Test instructions: To satisfy the number of qualified series.

Idea: a recursive question. The meaning of the state Dp[i][j][k] is the sequence of length I, the number of absenteeism is J, the last time to record a continuous K-time late legal sequence numbers.

State transitions See comments for code

#include <bits/stdc++.h> using namespace std;
typedef long Long LL;
const int N = 1e5 + 10;
int mod = 1e9 + 7;

0:0 Absences 1:1 Absences//0: consecutive 0 times 1:1 consecutive times 2: consecutive late 2 times int dp[n][2][3];
    int main () {//Freopen ("In.txt", "R", stdin);
    
    Dp[1][1][0] = dp[1][0][0] = dp[1][0][1] = 1; for (int i = 2; I <= 100000; i++) {dp[i][1][0] = dp[i-1][1][0] + dp[i-1][1][1];//i-1 sequence absenteeism 1 times, eventually 0 consecutive times and 1 times, add

        A normal sign-in if (dp[i][1][0] >= MoD) dp[i][1][0]-= mod;

        Dp[i][1][0] + = dp[i-1][1][2];//i-1 sequence last 2 consecutive times to add a normal check-in if (dp[i][1][0] >= MoD) dp[i][1][0]-= mod;  Dp[i][1][0] + = dp[i-1][0][0];//i-1 sequence last 0 consecutive times add a normal check-in if (dp[i][1][0] >= MoD) dp[i][1][0]-=

        MoD

        Dp[i][1][0] + = dp[i-1][0][1];//i-1 Sequence truancy 0 consecutive 1 times late, add a truancy if (dp[i][1][0] >= MoD) dp[i][1][0]-= mod; Dp[i][1][0] + = dp[i-1][0][2];//i-1 Sequence truancy 0 consecutive 2 times late, add a truancy if (dp[i][1][0] >= MoD) dp[i][1][0]-= mod; 
            ########### dp[i][1][1] = dp[i-1][1][0];//i-1 sequence truancy One consecutive late 0 times, add a late if (dp[i][1][1] >= MoD)
DP[I][1][1]-= mod; 
            ############ dp[i][1][2] = dp[i-1][1][1];//i-1 sequence truancy One consecutive late 1 times, add a late if (dp[i][1][2] >= MoD) 
DP[I][1][2]-= mod; ########### dp[i][0][0] = dp[i-1][0][0] + dp[i-1][0][1];//i-1 sequence absenteeism 0 times, the last consecutive 1 times late 0 times, add a normal check in if (dp[i][0][0 

        ] >= MoD) dp[i][0][0]-= mod;  Dp[i][0][0] + = dp[i-1][0][2];//i-1 sequence absenteeism 0 times, the last consecutive late 2 times, add a normal check in if (dp[i][0][0] >= MoD) dp[i][0][0]-=
MoD 
            ########### dp[i][0][1] = dp[i-1][0][0];//i-1 sequence absenteeism 0 times, last consecutive 0 times late, add a late if (dp[i][0][1] >= MoD)
DP[I][0][1]-= mod; ########### dp[i][0][2] = dp[i-1][0][1];//i-1 sequence absenteeism 0 times, last consecutive 2 times late, add a late if (dp[i][0][2] >= mo 
        d) dp[i][0][2]-= mod; 
        } int n; 
         while (~SCANF ("%d", &n)) {   int ans = 0;  for (int i = 0, i < 2; i++) for (int j = 0; J < 3; J + +) {ans 
                + = dp[n][i][j];//Each result is to add up the various situations if (ans >= mod) ans-= mod; 
        } printf ("%d\n", ans);
 } return 0;
 }




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.