King's Order
题目连接:
http://acm.hdu.edu.cn/showproblem.php?pid=5642
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
hint:
For the first testcase you can divide the into one cake of \(2\times2\) , 2 cakes of \(1\times 1\)
Hint
题意
问你长度为n的字符串,只含有小写字母。
没有超过3个连续相同。
问你这个字符串一共有多少种。
题解:
dp[i][j]表示第i个位置,当前连续长度为j的方案数。
然后转移就好了。
可以直接预处理出来。
代码
#include<stdio.h>
#include<algorithm>
#include<iostream>
#include<math.h>
#include<cstring>
using namespace std;
const int mod = 1e9+7;
const int maxn = 2000+7;
long long dp[maxn][4];
void pre()
{
dp[1][1]=26;
for(int i=1;i<=2000;i++)
{
for(int j=1;j<=3;j++)
{
if(dp[i][j])
{
if(j!=3)dp[i+1][j+1]=(dp[i][j]+dp[i+1][j+1])%mod;
dp[i+1][1]=(dp[i+1][1]+dp[i][j]*25)%mod;
}
}
}
}
void solve()
{
int n;scanf("%d",&n);
long long ans = 0;
for(int i=1;i<=3;i++)
ans=(ans+dp[n][i])%mod;
cout<<ans<<endl;
}
int main()
{
int t;scanf("%d",&t);
pre();
while(t--)solve();
return 0;
}
HDU 5642 King's Order 动态规划的更多相关文章
-
HDU 5642 King&#39;s Order dp
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5642 King's Order Accepts: 381 Submissions: 1361 ...
-
hdu 5642 King&#39;s Order(数位dp)
Problem Description After the king's speech , everyone is encouraged. But the war is not over. The k ...
-
HDU 5642 King&#39;s Order【数位dp】
题目链接: http://bestcoder.hdu.edu.cn/contests/contest_showproblem.php?cid=677&pid=1003 题意: 求长度为n的序列 ...
-
HDU 1074 Doing Homework (动态规划,位运算)
HDU 1074 Doing Homework (动态规划,位运算) Description Ignatius has just come back school from the 30th ACM/ ...
-
HDU 1176 免费馅饼 (动态规划)
HDU 1176 免费馅饼 (动态规划) Description 都说天上不会掉馅饼,但有一天gameboy正走在回家的小径上,忽然天上掉下大把大把的馅饼.说来gameboy的人品实在是太好了,这馅饼 ...
-
hdu-5642 King&#39;s Order(数位dp)
题目链接: King's Order Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Othe ...
-
King&#39;s Order(hdu5642)
King's Order Accepts: 381 Submissions: 1361 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: ...
-
HDU 5433 Xiao Ming climbing 动态规划
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5433 Xiao Ming climbing Time Limit: 2000/1000 MS (Ja ...
-
HDU 5643 King&#39;s Game 打表
King's Game 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5643 Description In order to remember hi ...
随机推荐
-
hibernate常用关联
<?xml version="1.0" encoding="utf-8" ?> <hibernate-mapping xmlns=" ...
-
UVALive 6264 Conservation --拓扑排序
题意:一个展览有n个步骤,告诉你每一步在那个场馆举行,总共2个场馆,跨越场馆需要1单位时间,先给你一些约束关系,比如步骤a要在b前执行,问最少的转移时间是多少. 解法:根据这些约束关系可以建立有向边, ...
-
OC基础(3)
对象的存储细节 函数与方法对比 常见错误 *:first-child { margin-top: 0 !important; } body > *:last-child { margin-bot ...
-
SQL Server 索引和视图
Ø 索引 1. 什么是索引 索引就是数据表中数据和相应的存储位置的列表,利用索引可以提高在表或视图中的查找数据的速度. 2. 索引分类 数据库中索引主要分为两类:聚集索引和非聚集索引.SQL Serv ...
-
CentOS 6.4 64位 安装 jdk 6u45
准备: 1.下载历史版本jdk 地址: http://java.sun.com/products/archive/ 下载的版本 jdk-6u45-linux-x64-rpm.bin Linux x6 ...
-
腾讯的一道js面试题(原型)
有一只小狗叫花花,它会“汪汪”叫,他的同伴也会汪汪叫,后来环境发生了变化,新出生的狗不会再“汪汪”叫,而变成“呜呜”叫. 试通过继承来达到目的 function Dog(){ 2 this.bark ...
-
去除inline-block元素间的间距
一.现象描述 真正意义上的inline-block水平呈现的元素间,换行显示或者空格隔开的情况下会有间距,这是因为浏览器在解析时,会将换行等读取成一个空格导致. 二.移出空格的方法 ① 我们可以去掉元 ...
-
EXCEL中把两列表格里的数字合成一列并且中间用逗号隔开
背景:使用loadrunner做参数化时,往往需要在excel表格中做数据,比如:第一列是用户名,第二列是密码,格式如下: 再将用户名和密码合并成一列,以逗号分隔,需要用到的公式为: =A1& ...
-
申请 Let’s Encrypt 泛域名证书 及 Nginx/Apache 证书配置
什么是 Let’s Encrypt? 部署 HTTPS 网站的时候需要证书,证书由 CA (Certificate Authority )机构签发,大部分传统 CA 机构签发证书是需要收费的,这不利于 ...
-
《剑指offer》 面试题53 :正则表达式匹配 Java
引言:这道题情况比较复杂,边界条件较多,为了便于以后复习,整理一下.另外,由于C语言和Java对于字符串的操作存在不一样的地方,代码也存在改动. 题目:请实现一个函数用来匹配包含'.'和'*'的正则表 ...