有一个只含0和1的长度为n的串,问不含有101的所有串的个数。

——不存在连续的101、010、111的字符串数量

HDU:https://cn.vjudge.net/problem/HDU-3485

51nod:

https://blog.csdn.net/Viscu/article/details/52669071

https://blog.csdn.net/lwlldd/article/details/70941554

https://blog.csdn.net/xtulollipop/article/details/52689159

https://blog.csdn.net/f_zyj/article/details/52663012

https://blog.csdn.net/LuRiCheng/article/details/52673193

JOJ:https://blog.csdn.net/kongming_acm/article/details/5377198

https://blog.csdn.net/jcwkyl/article/details/4153057

http://blog.sina.com.cn/s/blog_944759ba0100vmz9.html


记录后两位,共有4种情况

00->0

01->1

10->2

11->3;

【101的时候】

dp[i][0]=dp[i-1][0]+dp[i-1][2];
dp[i][1]=dp[i-1][0];
dp[i][2]=dp[i-1][1]+dp[i-1][3];
dp[i][3]=dp[i-1][1]+dp[i-1][3];

  1. #include<cstdio>
  2. #include<string>
  3. #include<cstdlib>
  4. #include<cmath>
  5. #include<iostream>
  6. #include<cstring>
  7. #include<set>
  8. #include<queue>
  9. #include<algorithm>
  10. #include<vector>
  11. #include<map>
  12. #include<cctype>
  13. #include<stack>
  14. #include<sstream>
  15. #include<list>
  16. #include<assert.h>
  17. #include<bitset>
  18. #include<numeric>
  19. #define debug() puts("++++")
  20. #define gcd(a,b) __gcd(a,b)
  21. #define lson l,m,rt<<1
  22. #define rson m+1,r,rt<<1|1
  23. #define fi first
  24. #define se second
  25. #define pb push_back
  26. #define sqr(x) ((x)*(x))
  27. #define ms(a,b) memset(a,b,sizeof(a))
  28. #define sz size()
  29. #define be begin()
  30. #define mp make_pair
  31. #define pu push_up
  32. #define pd push_down
  33. #define cl clear()
  34. #define lowbit(x) -x&x
  35. #define all 1,n,1
  36. #define rep(i,x,n) for(int i=(x); i<=(n); i++)
  37. #define in freopen("in.in","r",stdin)
  38. #define out freopen("out.out","w",stdout)
  39. #define mod 9997
  40. using namespace std;
  41. typedef long long ll;
  42. typedef unsigned long long ULL;
  43. typedef pair<int,int> P;
  44. const ULL base = ;//
  45. const int INF = 0x3f3f3f3f;
  46. const ll LNF = ;
  47. const int maxn = +;
  48. const int maxm = 1e6 + ;
  49. const double PI = acos(-1.0);
  50. const double eps = 1e-;
  51. const int dx[] = {-,,,,,,-,-};
  52. const int dy[] = {,,,-,,-,,-};
  53. int dir[][] = {{,},{,-},{-,},{,}};
  54. const int mon[] = {, , , , , , , , , , , , };
  55. const int monn[] = {, , , , , , , , , , , , };
  56. /*
  57. 010
  58. 00 - 0
  59. 01 - 1
  60. 10 - 2
  61. 11 - 3
  62. */
  63.  
  64. ll dp[maxn][],ans[maxn];
  65. void init()
  66. {
  67. //
  68. ms(dp,);
  69. dp[][]=;
  70. dp[][]=;dp[][]=;dp[][]=;dp[][]=;
  71. for(int i=;i<;i++)
  72. {
  73. dp[i][]=(dp[i-][]+dp[i-][])%mod;
  74. dp[i][]=(dp[i-][]+dp[i-][])%mod;
  75. dp[i][]=(dp[i-][]+dp[i-][])%mod;
  76. dp[i][]=(dp[i-][])%mod;
  77. }
  78. }
  79. void init1()
  80. {
  81. //
  82. ms(dp,);
  83. dp[][]=;
  84. dp[][]=;dp[][]=;dp[][]=;dp[][]=;
  85. for(int i=;i<;i++)
  86. {
  87. dp[i][]=(dp[i-][]+dp[i-][])%mod;
  88. dp[i][]=(dp[i-][])%mod;
  89. dp[i][]=(dp[i-][]+dp[i-][])%mod;
  90. dp[i][]=(dp[i-][]+dp[i-][])%mod;
  91. }
  92. }
  93.  
  94. void init2()
  95. {
  96. //
  97. ms(dp,);
  98. dp[][]=;
  99. dp[][]=;dp[][]=;dp[][]=;dp[][]=;
  100. for(int i=;i<;i++)
  101. {
  102. dp[i][]=(dp[i-][]+dp[i-][])%mod;
  103. dp[i][]=(dp[i-][]+dp[i-][])%mod;
  104. dp[i][]=(dp[i-][])%mod;
  105. dp[i][]=(dp[i-][]+dp[i-][])%mod;
  106. }
  107. }
  108. int main()
  109. {
  110. int n;
  111. while(cin>>n)
  112. {
  113. init();
  114. if(n==-) break;
  115. cout<<(dp[n][]+dp[n][]+dp[n][]+dp[n][])%mod<<endl;
  116. }
  117. }
  118. /*
  119. 【题意】
  120.  
  121. 【类型】
  122.  
  123. 【分析】
  124.  
  125. 【时间复杂度&&优化】
  126.  
  127. 【trick】
  128. 首先dp打表找个规律:
  129. 如何DP?
  130. 这里我们找合法串:010
  131. 假设末尾i是1,那么i-1位置上无论是0和还是1都合法,不会出现010的情况,那么
  132. 就是dp[i][1] = dp[i-1][0] + dp[i-1][1];
  133. 如果末尾i是0,
  134. 如果i-1位置上是0,那么无论如何也是合法的,
  135. 如果i-1的位置上是1,会出现010/101这样的情况,那么讨论第i-2位上的,i-2如果是0,那么会出现010的情况,
  136.  
  137. 如果i-2位置上是1的话,无论如何都是合法串,那就是dp[i][0] = dp[i-1][0] + dp[i-2][1];
  138. 【数据】
  139. 0 1(i-3)-0
  140. 00 01 10 11(i-2)x1
  141. 000 001 【010/1】 011 100 【110/1】 111 (i-1) x10
  142. 010-1
  143. 110-1
  144.  
  145. */

01字符串递推DP

  1. #include<cstdio>
  2. #include<string>
  3. #include<cstdlib>
  4. #include<cmath>
  5. #include<iostream>
  6. #include<cstring>
  7. #include<set>
  8. #include<queue>
  9. #include<algorithm>
  10. #include<vector>
  11. #include<map>
  12. #include<cctype>
  13. #include<stack>
  14. #include<sstream>
  15. #include<list>
  16. #include<assert.h>
  17. #include<bitset>
  18. #include<numeric>
  19. #define debug() puts("++++")
  20. #define gcd(a,b) __gcd(a,b)
  21. #define lson l,m,rt<<1
  22. #define rson m+1,r,rt<<1|1
  23. #define fi first
  24. #define se second
  25. #define pb push_back
  26. #define sqr(x) ((x)*(x))
  27. #define ms(a,b) memset(a,b,sizeof(a))
  28. #define sz size()
  29. #define be begin()
  30. #define mp make_pair
  31. #define pu push_up
  32. #define pd push_down
  33. #define cl clear()
  34. #define lowbit(x) -x&x
  35. #define all 1,n,1
  36. #define rep(i,x,n) for(int i=(x); i<=(n); i++)
  37. #define in freopen("in.in","r",stdin)
  38. #define out freopen("out.out","w",stdout)
  39. #define mod 9997
  40. using namespace std;
  41. typedef long long ll;
  42. typedef unsigned long long ULL;
  43. typedef pair<int,int> P;
  44. const ULL base = ;//
  45. const int INF = 0x3f3f3f3f;
  46. const ll LNF = ;
  47. const int maxn = +;
  48. const int maxm = 1e6 + ;
  49. const double PI = acos(-1.0);
  50. const double eps = 1e-;
  51. const int dx[] = {-,,,,,,-,-};
  52. const int dy[] = {,,,-,,-,,-};
  53. int dir[][] = {{,},{,-},{-,},{,}};
  54. const int mon[] = {, , , , , , , , , , , , };
  55. const int monn[] = {, , , , , , , , , , , , };
  56. /*
  57. 0
  58. 001
  59. 0 1
  60. 00 01 10 11
  61. 0 2 4 7
  62. */
  63.  
  64. ll dp[maxn][],ans[maxn];
  65. void init()
  66. {
  67. ms(dp,);
  68. ms(ans,);
  69. ans[]=;ans[]=;ans[]=;
  70. dp[][]=,dp[][]=;
  71. dp[][]=,dp[][]=;
  72. //dp[3][0]=4;dp[3][1]=3;
  73. for(int i=;i<;i++)
  74. {
  75. dp[i][]=(dp[i-][]+dp[i-][])%mod;
  76. dp[i][]=(dp[i-][]+dp[i-][])%mod;
  77. ans[i]=(dp[i][]%mod+dp[i][]%mod)%mod;
  78. }
  79. }
  80. int main()
  81. {
  82. int n;
  83. while(cin>>n)
  84. {
  85. init();
  86. if(n==-) break;
  87. cout<<ans[n]%mod<<endl;
  88. }
  89. }
  90. /*
  91. 【题意】
  92.  
  93. 【类型】
  94.  
  95. 【分析】
  96.  
  97. 【时间复杂度&&优化】
  98.  
  99. 【trick】
  100. 首先dp打表找个规律:
  101. 如何DP?
  102. 这里我们找合法串:010
  103. 假设末尾i是1,那么i-1位置上无论是0和还是1都合法,不会出现010的情况,那么
  104. 就是dp[i][1] = dp[i-1][0] + dp[i-1][1];
  105. 如果末尾i是0,
  106. 如果i-1位置上是0,那么无论如何也是合法的,
  107. 如果i-1的位置上是1,会出现010/101这样的情况,那么讨论第i-2位上的,i-2如果是0,那么会出现010的情况,
  108.  
  109. 如果i-2位置上是1的话,无论如何都是合法串,那就是dp[i][0] = dp[i-1][0] + dp[i-2][1];
  110. 【数据】
  111. 0 1(i-3)-0
  112. 00 01 10 11(i-2)x1
  113. 000 001 【010/1】 011 100 【110/1】 111 (i-1) x10
  114. 010-1
  115. 110-1
  116.  
  117. */

dp[i][j]:长度i,j代表末尾0 or 1

HDU 3485【101】 51nod 1668【010】 joj 2171【111】动态规划的更多相关文章

  1. 51Nod 1668 非010串

    这是昨天上课ChesterKing dalao讲线代时的例题 当时看到这道题就觉得很水,记录一下后面两位的情况然后讨论一下转移即可 由于之前刚好在做矩阵题,所以常规的矩阵快速幂优化也很简单 好我们开始 ...

  2. Hdu OJ 5115 Dire Wolf (2014ACM/ICPC亚洲区北京站) (动态规划-区间dp)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5115 题目大意:前面有n头狼并列排成一排, 每一头狼都有两个属性--基础攻击力和buff加成, 每一头 ...

  3. 【51Nod】1055 最长等差数列 动态规划

    [题目]1055 最长等差数列 [题意]给定大小为n的互不不同正整数集合,求最长等差数列的长度.\(n \leq 10000\). [算法]动态规划 两个数之间的差是非常重要的信息,设\(f_{i,j ...

  4. 51Nod 1083 矩阵取数问题 | 动态规划

    #include "bits/stdc++.h" using namespace std; #define LL long long #define INF 0x3f3f3f3f3 ...

  5. 51nod 最长单增子序列(动态规划)

    最长单增子序列 (LIS Longest Increasing Subsequence)给定一个数列,从中删掉任意若干项剩余的序列叫做它的一个子序列,求它的最长的子序列,满足子序列中的元素是单调递增的 ...

  6. HDU——1799循环多少次(杨辉三角/动态规划/C(m,n)组合数)

    循环多少次? Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Sub ...

  7. HDU 5375 Gray code(2015年多校联合 动态规划)

    题目连接 : 传送门 题意: 给定一个长度为的二进制串和一个长度为n的序列a[],我们能够依据这个二进制串得到它的Gray code. Gray code中假设第i项为1的话那么我们就能够得到a[i] ...

  8. 题解【AtCoder - CODE FESTIVAL 2017 qual B - D - 101 to 010】

    题目:https://atcoder.jp/contests/code-festival-2017-qualb/tasks/code_festival_2017_qualb_d 题意:给一个 01 串 ...

  9. 2013长春网赛1001 hdu 4759 Poker Shuffle

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4759 题意:有一堆2^n的牌,牌原先按(1,2,....k)排序,每一次洗牌都将牌分成两种情况:(1, ...

随机推荐

  1. [LeetCode] 大数问题,相加和相乘,题 Multiply Strings

    Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...

  2. AngularJs学习——常用表单指令练习

    一.知识点 ng-show.ng-hide.ng-if(控制元素显示隐藏,区别在于ng-hide是是否显示隐藏元素,而ng-if是是否移除元素): ng-src.ng-class(为元素添加路径和cl ...

  3. [Luogu 1168] 中位数

    中位数可以转化为区间第k大问题,当然是选择Treap实现名次树了啊.(笑) 功能十分简单的Treap即能满足需求--只需要插入与查找第大的功能. 插入第i个数时,如果i是奇数,随即询问当前排名第(i+ ...

  4. CentOS6.8 安装rar解压缩

    wget http://www.rarsoft.com/rar/rarlinux-x64-5.4.0.tar.gz tar -zxvf rarlinux-x64-5.4.0.tar.gz cd rar ...

  5. Item 1----------考虑用静态工厂方法代替构造器

    读书,有时候,我感觉总是有点绕和不具体.我阅读了代码,理解代码后,才有一种理解和把握的感觉. 优点三.   把某个对象的构建放给客户端来实现. 比如下面的实现,客户端Test,获取Service的实例 ...

  6. Android项目分包---总结-------直接使用

    注: 本文是从该文摘抄而来的.简单的说,就是阅读了该文,然后,再自己复述,复制形成该文.   1.罗列Android项目的分包规则   微盘使用分包规则   如下:     1).第一层com.sin ...

  7. 【TYVJ】P1039 忠诚2

    [算法]线段树 [注意]修改或查询区间时,若区间能包含某棵子树就立即返回,否则线段树就失去了意义. #include<cstdio> #include<algorithm> u ...

  8. Spring cookie 实战(山东数漫江湖)

    Cookie是什么 简单来说,cookie就是浏览器储存在用户电脑上的一小段文本文件.cookie 是纯文本格式,不包含任何可执行的代码.一个web页面或服务器告知浏览器按照一定规范来储存这些信息,并 ...

  9. Coursera在线学习---第十节.大规模机器学习(Large Scale Machine Learning)

    一.如何学习大规模数据集? 在训练样本集很大的情况下,我们可以先取一小部分样本学习模型,比如m=1000,然后画出对应的学习曲线.如果根据学习曲线发现模型属于高偏差,则应在现有样本上继续调整模型,具体 ...

  10. Android控件——监听按钮的点击事件

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAroAAAFTCAIAAABZPDiZAAAgAElEQVR4nOy9918UWfb///1jdu2uBs