hdu 5642 King's Order

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5642

题意:给出一个n,问一个长度为n的只含有小写字母的序列中,满足其中不含有超过3个连续相同字母的序列的个数有多少。

思路:

设dp[i][j],表示序列第i位为字母j的合法序列的个数。sum[i]表示长度为i的序列有多少个。

假设第i位为字母‘a‘,dp[i][j]就是sum[i-1]*1-不合法的情况。

不合法的情况就是sum[i-4] - dp[i-4][j]。

sum[i-4]表示已经计算出的合法长度为i-4的序列个数,所以sum[i-4]*1*1*1表示第j-1,j-2,j-3的字母与第j位字母相同。

且因为sum[i-4]记录的是合法情况,在第j位之前不能出现连续4个相同字母,所以要再减去dp[i-4][j]。

所以可以得出dp[i][j] = sum[i-1] - (sum[i-4] - dp[i-4][j])

 1 #include <iostream>
 2 #include <cstring>
 3 #include <cstdio>
 4 using namespace std;
 5 const long long mod = 1000000007;
 6 int n;
 7 int T;
 8 long long dp[2010][30];
 9 long long sum[2010];
10 int main()
11 {
12 //  freopen("in.txt", "r", stdin);
13     scanf("%d", &T);
14
15     while(T--)
16     {
17         scanf("%d", &n);
18         memset(dp, 0, sizeof(dp));
19         memset(sum, 0, sizeof(sum));
20         for(int i = 0; i < 26; i++)
21         {
22             dp[1][i] = 1;
23         }
24         for(int i = 2; i <= n; i++)
25         {
26             for(int j = 0; j < 26; j++)
27             {
28                 sum[i-1] += dp[i-1][j];
29                 sum[i-1] %= mod;
30             }
31             for(int j = 0; j < 26; j++)
32             {
33                 if(i < 4) dp[i][j] = sum[i-1];
34                 else if(i == 4)
35                 {
36                     dp[i][j] = (sum[i-1]-1+mod)%mod;
37                 }
38                 else if(i == 5)
39                 {
40                     dp[i][j] = (sum[i-1]-25+mod)%mod;
41                 }
42                 else
43                 {
44                     dp[i][j] = (sum[i-1]-(sum[i-4]-dp[i-4][j]+mod)%mod + mod)%mod;
45                 }
46             }
47         }
48         for(int i = 0; i < 26; i++)
49         {
50             sum[n] += dp[n][i];
51             sum[n] %= mod;
52         }
53        cout<<sum[n]<<endl;
54     }
55     return 0;
56 }

hdu 5642 King's Order

时间: 2024-10-07 02:15:58

hdu 5642 King's Order的相关文章

HDU 5642 King&#39;s Order(数位DP)

题目链接:点击打开链接 题意:要求你生成一个合法的字符串, 由小写字母a~z组成, 相同字母相邻出现不能超过3个, 求有多少种组合. 思路:数位DP来做, 用d[i][j][k]表示处理完前i个字母, 第i-1个字母为j,已经连续出现了k次的方法数. 然后每次转移就很简单了, 继续选择字母j(if(k < 3)), 或者换其他的. 细节参见代码: #include<cstdio> #include<cstring> #include<algorithm> #inc

hdu 5640 King&#39;s Cake(模拟)

Problem Description It is the king's birthday before the military parade . The ministers prepared a rectangle cake of size n×m(1≤n,m≤10000) . The king plans to cut the cake himself. But he has a strange habit of cutting cakes. Each time, he will cut

hdu 5641 King&#39;s Phone(暴力模拟题)

Problem Description In a military parade, the King sees lots of new things, including an Andriod Phone. He becomes interested in the pattern lock screen. The pattern interface is a 3×3 square lattice, the three points in the first line are labeled as

HDU 4337 King Arthur&#39;s Knights 找出一条哈密顿回路

n个点m条无向边 输出一条哈密顿回路 #include <cstdio> #include <cstring> #include <iostream> using namespace std; const int N = 155; int n, m; bool mp[N][N]; int S, T, top, Stack[N]; bool vis[N]; void _reverse(int l,int r) { while (l<r) swap(Stack[l++

HDU 5640 King&#39;s Cake

King's Cake Problem Description It is the king's birthday before the military parade . The ministers prepared a rectangle cake of size n×m(1≤n,m≤10000) . The king plans to cut the cake himself. But he has a strange habit of cutting cakes. Each time,

hdu-5642 King&#39;s Order(数位dp)

题目链接: 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 Description After the king's speech , everyone is encouraged. But the war is not ove

HDU 5643 King&#39;s Game | 约瑟夫环变形

经典约瑟夫环 1 int f[N] ={ 0 }; 2 for(int i=2; i<=n; i++) 3 { 4 f[i] = (f[i-1] + k) % i; 5 } 变形:k是变化的 #include <iostream> #include <stdio.h> #include <string.h> #include <algorithm> #include <stdlib.h> #include <queue> #in

HDU 4337 King Arthur&amp;#39;s Knights 它输出一个哈密顿电路

n积分m文章无向边 它输出一个哈密顿电路 #include <cstdio> #include <cstring> #include <iostream> using namespace std; const int N = 155; int n, m; bool mp[N][N]; int S, T, top, Stack[N]; bool vis[N]; void _reverse(int l,int r) { while (l<r) swap(Stack[l

HDU - 3002 King of Destruction(最小割)

http://acm.hdu.edu.cn/showproblem.php?pid=3002   最小割模板 #include<iostream> #include<cmath> #include<cstring> #include<queue> #include<vector> #include<cstdio> #include<algorithm> #include<map> #include<set