hdu-5642 King ' s Order (digital DP)

Source: Internet
Author: User

Topic Links:

King ' s Order

Time limit:2000/1000 MS (java/others)

Memory limit:65536/65536 K (java/others)
Total submission (s): 101 Accepted Submission (s): 59


Problem Descriptionafter The King ' s speech, everyone is encouraged. But the war isn't over. The king needs to give orders from time to time. But sometimes he can not speak things well. So in his order there is some ones like this: "Let the group-p-p three come to me". As can see letter ' P ' repeats for 3 times. Poor king!

Now, it's war time, because of the spies from enemies, sometimes it's pretty hard-to-do which order s come from the king. But fortunately the general know what the King Speaks:the King never repeats a letter for more than 3 times continually. A nd only this kind of order is legal. For example, the order: ' Let the group-p-p-p three come to me ' can never come from the king. While the order: ' Let the group-p three come to me ' is a legal statement.

The general wants to know how many legal orders which has the length of n

To make it simple, only lower case 中文版 Letters can appear in King's order, and please output the answer modulo1000000007

We regard strings is the same if and only if all charactor is the same place of these, the strings.

Inputthe First line contains a numberT(t≤ten) --the number of the testcases.

For each testcase, the first line and the contains a positive number n(n≤).

Outputfor each testcase, print a single number as the answer.

Sample Input224

Sample Output676456950 Test Instructions:
The king's morale increased after the speech, but at this time the war was not over, and the king occasionally issued orders. As the king's stuttering was not cured, the summons might appear: "Let the Third Army-army-Army, go to the front" such an order. Because of the presence of spies in the military and the urgency of the war, the commanders of the front line are often unable to tell which orders really came from the king. But the King's command had a characteristic that he repeated the most characters of each successive33 times. So he did not say in his order: "Let the Third Army-army-army-army, to the front", but can have: "Let the Third Army-army, to the front." When the general finds you, you need to tell him that the length of the given command isNn , how many different commands can be issued by the king. (That is, the length is nn the number of qualified strings) of course, The king may utter a word without making any stuttering, as he did in that speech. To simplify the answer, the King's command contains only lowercase English letters, and the answer output modulo 1000000007 < Span class= "Mord" >  
/span> Idea: Dp[i][j][k] I is the first character of the string, ' a ' +j for the end of the character, K for the end of the character appeared several times, the specific transfer equation to see the code;
AC Code:
#include <iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespacestd;Const Long Longmod=1e9+7;Long Longdp[2004][ -][5];intMain () {intT,n; scanf ("%d",&t);  while(t--) {scanf ("%d",&N); Memset (DP,0,sizeof(DP));  for(intI=0;i< -; i++) {dp[1][i][1]=1; }         for(intI=1; i<=n;i++)        {             for(intj=0; j<= -; j + +)//enumeration at the end of the character { for(intk=0;k< -; k++)//Enumerate the newly added characters {if(k==j) { for(intx=1; x<=3; x + +) Dp[i][j][x]+=dp[i-1][j][x-1],dp[i][j][x]%=mod;//equal words plus a piece}Else                    {                             for(intx=1; x<=3; x + +) dp[i][j][1]+=dp[i-1][j][x],dp[i][j][1]%=mod;//Not equal to X not add}} }        }        Long Longans=0;  for(intI=0;i< -; i++)        {             for(intj=1; j<=3; j + +) ans+=dp[n][i][j],ans%=MoD; } cout<<ans<<"\ n"; }    return 0;}

hdu-5642 King ' s Order (digital DP)

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.