zoukankan      html  css  js  c++  java
  • hdu 5642 King's Order(数位dp)

    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(T10)——The number of the testcases.
    For each testcase, the first line and the only line contains a positive number n(n2000).
     
    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代码:

     1 #pragma comment(linker, "/STACK:1024000000,1024000000")
     2 #include<iostream>
     3 #include<cstdio>
     4 #include<cstring>
     5 #include<cmath>
     6 #include<math.h>
     7 #include<algorithm>
     8 #include<queue>
     9 #include<set>
    10 #include<bitset>
    11 #include<map>
    12 #include<vector>
    13 #include<stdlib.h>
    14 using namespace std;
    15 #define ll long long
    16 #define eps 1e-10
    17 #define MOD 1000000007
    18 #define N 2006
    19 #define inf 1e12
    20 ll n;
    21 ll dp[N][6];
    22 void init(){
    23     dp[0][1]=26;
    24     for(ll i=1;i<2005;i++){
    25         dp[i][2]=dp[i-1][1]%MOD;
    26         dp[i][3]=dp[i-1][2]%MOD;
    27         dp[i][1]=(dp[i-1][1]+dp[i-1][2]+dp[i-1][3])%MOD*25;
    28     }
    29 }
    30 int main()
    31 {
    32     init();
    33     int t;
    34     scanf("%d",&t);
    35     while(t--){
    36         scanf("%I64d",&n);
    37         printf("%I64d
    ",(dp[n-1][1]+dp[n-1][2]+dp[n-1][3])%MOD);
    38     }
    39     return 0;
    40 }
  • 相关阅读:
    TensorFlow笔记-初识
    SMP、NUMA、MPP体系结构介绍
    Page Cache, the Affair Between Memory and Files.页面缓存-内存与文件的那些事
    How The Kernel Manages Your Memory.内核是如何管理内存的
    Anatomy of a Program in Memory.剖析程序的内存布局
    Cache: a place for concealment and safekeeping.Cache: 一个隐藏并保存数据的场所
    Memory Translation and Segmentation.内存地址转换与分段
    CPU Rings, Privilege, and Protection.CPU的运行环, 特权级与保护
    The Kernel Boot Process.内核引导过程
    How Computers Boot Up.计算机的引导过程
  • 原文地址:https://www.cnblogs.com/UniqueColor/p/5286963.html
Copyright © 2011-2022 走看看