HDU 3485【101】 51nod 1668【010】 joj 2171【111】动态规划
有一个只含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];
#include<cstdio>
#include<string>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<cstring>
#include<set>
#include<queue>
#include<algorithm>
#include<vector>
#include<map>
#include<cctype>
#include<stack>
#include<sstream>
#include<list>
#include<assert.h>
#include<bitset>
#include<numeric>
#define debug() puts("++++")
#define gcd(a,b) __gcd(a,b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a,b,sizeof(a))
#define sz size()
#define be begin()
#define mp make_pair
#define pu push_up
#define pd push_down
#define cl clear()
#define lowbit(x) -x&x
#define all 1,n,1
#define rep(i,x,n) for(int i=(x); i<=(n); i++)
#define in freopen("in.in","r",stdin)
#define out freopen("out.out","w",stdout)
#define mod 9997
using namespace std;
typedef long long ll;
typedef unsigned long long ULL;
typedef pair<int,int> P;
const ULL base = ;//
const int INF = 0x3f3f3f3f;
const ll LNF = ;
const int maxn = +;
const int maxm = 1e6 + ;
const double PI = acos(-1.0);
const double eps = 1e-;
const int dx[] = {-,,,,,,-,-};
const int dy[] = {,,,-,,-,,-};
int dir[][] = {{,},{,-},{-,},{,}};
const int mon[] = {, , , , , , , , , , , , };
const int monn[] = {, , , , , , , , , , , , };
/*
010
00 - 0
01 - 1
10 - 2
11 - 3
*/ ll dp[maxn][],ans[maxn];
void init()
{
//
ms(dp,);
dp[][]=;
dp[][]=;dp[][]=;dp[][]=;dp[][]=;
for(int i=;i<;i++)
{
dp[i][]=(dp[i-][]+dp[i-][])%mod;
dp[i][]=(dp[i-][]+dp[i-][])%mod;
dp[i][]=(dp[i-][]+dp[i-][])%mod;
dp[i][]=(dp[i-][])%mod;
}
}
void init1()
{
//
ms(dp,);
dp[][]=;
dp[][]=;dp[][]=;dp[][]=;dp[][]=;
for(int i=;i<;i++)
{
dp[i][]=(dp[i-][]+dp[i-][])%mod;
dp[i][]=(dp[i-][])%mod;
dp[i][]=(dp[i-][]+dp[i-][])%mod;
dp[i][]=(dp[i-][]+dp[i-][])%mod;
}
} void init2()
{
//
ms(dp,);
dp[][]=;
dp[][]=;dp[][]=;dp[][]=;dp[][]=;
for(int i=;i<;i++)
{
dp[i][]=(dp[i-][]+dp[i-][])%mod;
dp[i][]=(dp[i-][]+dp[i-][])%mod;
dp[i][]=(dp[i-][])%mod;
dp[i][]=(dp[i-][]+dp[i-][])%mod;
}
}
int main()
{
int n;
while(cin>>n)
{
init();
if(n==-) break;
cout<<(dp[n][]+dp[n][]+dp[n][]+dp[n][])%mod<<endl;
}
}
/*
【题意】 【类型】 【分析】 【时间复杂度&&优化】 【trick】
首先dp打表找个规律:
如何DP?
这里我们找合法串:010
假设末尾i是1,那么i-1位置上无论是0和还是1都合法,不会出现010的情况,那么
就是dp[i][1] = dp[i-1][0] + dp[i-1][1];
如果末尾i是0,
如果i-1位置上是0,那么无论如何也是合法的,
如果i-1的位置上是1,会出现010/101这样的情况,那么讨论第i-2位上的,i-2如果是0,那么会出现010的情况, 如果i-2位置上是1的话,无论如何都是合法串,那就是dp[i][0] = dp[i-1][0] + dp[i-2][1];
【数据】
0 1(i-3)-0
00 01 10 11(i-2)x1
000 001 【010/1】 011 100 【110/1】 111 (i-1) x10
010-1
110-1 */
01字符串递推DP
#include<cstdio>
#include<string>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<cstring>
#include<set>
#include<queue>
#include<algorithm>
#include<vector>
#include<map>
#include<cctype>
#include<stack>
#include<sstream>
#include<list>
#include<assert.h>
#include<bitset>
#include<numeric>
#define debug() puts("++++")
#define gcd(a,b) __gcd(a,b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a,b,sizeof(a))
#define sz size()
#define be begin()
#define mp make_pair
#define pu push_up
#define pd push_down
#define cl clear()
#define lowbit(x) -x&x
#define all 1,n,1
#define rep(i,x,n) for(int i=(x); i<=(n); i++)
#define in freopen("in.in","r",stdin)
#define out freopen("out.out","w",stdout)
#define mod 9997
using namespace std;
typedef long long ll;
typedef unsigned long long ULL;
typedef pair<int,int> P;
const ULL base = ;//
const int INF = 0x3f3f3f3f;
const ll LNF = ;
const int maxn = +;
const int maxm = 1e6 + ;
const double PI = acos(-1.0);
const double eps = 1e-;
const int dx[] = {-,,,,,,-,-};
const int dy[] = {,,,-,,-,,-};
int dir[][] = {{,},{,-},{-,},{,}};
const int mon[] = {, , , , , , , , , , , , };
const int monn[] = {, , , , , , , , , , , , };
/*
0
001
0 1
00 01 10 11
0 2 4 7
*/ ll dp[maxn][],ans[maxn];
void init()
{
ms(dp,);
ms(ans,);
ans[]=;ans[]=;ans[]=;
dp[][]=,dp[][]=;
dp[][]=,dp[][]=;
//dp[3][0]=4;dp[3][1]=3;
for(int i=;i<;i++)
{
dp[i][]=(dp[i-][]+dp[i-][])%mod;
dp[i][]=(dp[i-][]+dp[i-][])%mod;
ans[i]=(dp[i][]%mod+dp[i][]%mod)%mod;
}
}
int main()
{
int n;
while(cin>>n)
{
init();
if(n==-) break;
cout<<ans[n]%mod<<endl;
}
}
/*
【题意】 【类型】 【分析】 【时间复杂度&&优化】 【trick】
首先dp打表找个规律:
如何DP?
这里我们找合法串:010
假设末尾i是1,那么i-1位置上无论是0和还是1都合法,不会出现010的情况,那么
就是dp[i][1] = dp[i-1][0] + dp[i-1][1];
如果末尾i是0,
如果i-1位置上是0,那么无论如何也是合法的,
如果i-1的位置上是1,会出现010/101这样的情况,那么讨论第i-2位上的,i-2如果是0,那么会出现010的情况, 如果i-2位置上是1的话,无论如何都是合法串,那就是dp[i][0] = dp[i-1][0] + dp[i-2][1];
【数据】
0 1(i-3)-0
00 01 10 11(i-2)x1
000 001 【010/1】 011 100 【110/1】 111 (i-1) x10
010-1
110-1 */
dp[i][j]:长度i,j代表末尾0 or 1
HDU 3485【101】 51nod 1668【010】 joj 2171【111】动态规划的更多相关文章
- 51Nod 1668 非010串
这是昨天上课ChesterKing dalao讲线代时的例题 当时看到这道题就觉得很水,记录一下后面两位的情况然后讨论一下转移即可 由于之前刚好在做矩阵题,所以常规的矩阵快速幂优化也很简单 好我们开始 ...
- Hdu OJ 5115 Dire Wolf (2014ACM/ICPC亚洲区北京站) (动态规划-区间dp)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5115 题目大意:前面有n头狼并列排成一排, 每一头狼都有两个属性--基础攻击力和buff加成, 每一头 ...
- 【51Nod】1055 最长等差数列 动态规划
[题目]1055 最长等差数列 [题意]给定大小为n的互不不同正整数集合,求最长等差数列的长度.\(n \leq 10000\). [算法]动态规划 两个数之间的差是非常重要的信息,设\(f_{i,j ...
- 51Nod 1083 矩阵取数问题 | 动态规划
#include "bits/stdc++.h" using namespace std; #define LL long long #define INF 0x3f3f3f3f3 ...
- 51nod 最长单增子序列(动态规划)
最长单增子序列 (LIS Longest Increasing Subsequence)给定一个数列,从中删掉任意若干项剩余的序列叫做它的一个子序列,求它的最长的子序列,满足子序列中的元素是单调递增的 ...
- HDU——1799循环多少次(杨辉三角/动态规划/C(m,n)组合数)
循环多少次? Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Sub ...
- HDU 5375 Gray code(2015年多校联合 动态规划)
题目连接 : 传送门 题意: 给定一个长度为的二进制串和一个长度为n的序列a[],我们能够依据这个二进制串得到它的Gray code. Gray code中假设第i项为1的话那么我们就能够得到a[i] ...
- 题解【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 串 ...
- 2013长春网赛1001 hdu 4759 Poker Shuffle
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4759 题意:有一堆2^n的牌,牌原先按(1,2,....k)排序,每一次洗牌都将牌分成两种情况:(1, ...
随机推荐
- ICPC2017 Urumqi - K - Sum of the Line
题目描述 Consider a triangle of integers, denoted by T. The value at (r, c) is denoted by Tr,c , where 1 ...
- 51Nod 1087 1 10 100 1000 | 数学
Input示例 3 1 2 3 Output示例 1 1 0 #include "bits/stdc++.h" using namespace std; #define LL lo ...
- 【poj1222-又一道开关问题】高斯消元求解异或方程组
题意:给出一个5*6的图,每个灯泡有一个初始状态,1表示亮,0表示灭.每对一个灯泡操作时,会影响周围的灯泡改变亮灭,问如何操作可以使得所有灯泡都关掉. 题解: 这题和上一题几乎完全一样..就是要输出解 ...
- bzoj 1776: [Usaco2010 Hol]cowpol 奶牛政坛——树的直径
农夫约翰的奶牛住在N (2 <= N <= 200,000)片不同的草地上,标号为1到N.恰好有N-1条单位长度的双向道路,用各种各样的方法连接这些草地.而且从每片草地出发都可以抵达其他所 ...
- 【BZOJ】3038: 上帝造题的七分钟2 && 3211: 花神游历各国
[算法]线段树||树状数组&&并查集 [题解]修改必须暴力单点修改,然后利用标记区间查询. 优化:一个数经过不断开方很快就会变成1,所以维护区间最大值. 修改时访问到的子树最大值< ...
- HDU 1599 find the mincost route (最短路 floyd)
题目链接 Problem Description 杭州有N个景区,景区之间有一些双向的路来连接,现在8600想找一条旅游路线,这个路线从A点出发并且最后回到A点,假设经过的路线为V1,V2,....V ...
- ES6数组去重及ES5数组去重方法
ES6中新增了Set数据结构,类似于数组,但是 它的成员都是唯一的 ,其构造函数可以接受一个数组作为参数,如: let array = [1, 1, 1, 1, 2, 3, 4, 4, 5, 3]; ...
- 网页实现插入图片—css与html的区别
Q1.二者有何区别?A1.写在css里面的图片是以背景图形式存在的,而写在html里的是以<img>标签形式存在的,在网页加载的过程中,以css背景图存在的图片会等到结构加载完成(网页的内 ...
- 模型验证与模型集成(Ensemble)
作者:吴晓军 原文:https://zhuanlan.zhihu.com/p/27424282 模型验证(Validation) 在Test Data的标签未知的情况下,我们需要自己构造测试数据来验证 ...
- python进行EDA探索性数据分析
1.查看数据的类型概况 cols = [c for c in train.columns] #返回数据的列名到列表里 print('Number of features: {}'.format(l ...