hdu 5642 King's Order(数位dp)

时间:2022-02-22 01:29:15
Problem Description
After the king's speech , everyone is encouraged. But the war is not over. The king needs to give orders from time to time. But sometimes he can not speak things well. So in his order there are some ones like this: "Let the group-p-p three come to me". As you can see letter 'p' repeats for 3 times. Poor king!
Now , it is war time , because of the spies from enemies , sometimes it is pretty hard for the general to tell which orders come from the king. But fortunately the general know how the king speaks: the king never repeats a letter for more than 3 times continually .And 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 that has the length of n
To make it simple , only lower case English Letters can appear in king's order , and please output the answer modulo 1000000007
We regard two strings are the same if and only if each charactor is the same place of these two strings are the same.
Input
The first line contains a number T(T≤10)——The number of the testcases.
For each testcase, the first line and the only line contains a positive number n(n≤2000).
Output
For each testcase, print a single number as the answer.
Sample Input
2
2
4
Sample Output
676
456950
hint:
All the order that has length 2 are legal. So the answer is 26*26.

For the order that has length 4. The illegal order are : "aaaa" , "bbbb"…….."zzzz" 26 orders in total. So the answer for n == 4 is 26^4-26 = 456950

题意:一个长度为n的序列,并且序列中不能出现长度大于3的连续的相同的字符,求一共有多少个合法序列。

思路:用dp[i][j]表示以j结尾,长度为i的合法序列个数。我们考虑一下这个怎么转移。
       以j结尾的话就三种情况,一个j结尾,两个j结尾,三个j结尾。

       如果是三个j结尾的话我们可以确定下来,长度为i的后三位是j,倒数第4位不可以是j,所以就是∑dp[i-3][k](k!=j),

       同理考虑两个j结尾和一个j结尾,那么转移方程就是:dp[i][j] = ∑dp[i-1][k] + ∑dp[i-2][k] + ∑dp[i-3][k] 

AC代码:

 #pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<math.h>
#include<algorithm>
#include<queue>
#include<set>
#include<bitset>
#include<map>
#include<vector>
#include<stdlib.h>
using namespace std;
#define ll long long
#define eps 1e-10
#define MOD 1000000007
#define N 2006
#define inf 1e12
ll n;
ll dp[N][];
void init(){
dp[][]=;
for(ll i=;i<;i++){
dp[i][]=dp[i-][]%MOD;
dp[i][]=dp[i-][]%MOD;
dp[i][]=(dp[i-][]+dp[i-][]+dp[i-][])%MOD*;
}
}
int main()
{
init();
int t;
scanf("%d",&t);
while(t--){
scanf("%I64d",&n);
printf("%I64d\n",(dp[n-][]+dp[n-][]+dp[n-][])%MOD);
}
return ;
}