http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1202&judgeId=225600

这题看起来挺复杂,但是真正的dp还是挺好理解的。唯独是想不到的,应该把样例模拟一遍。

比如1、2、4、2

考虑第一个,只有“1”这一个子序列

考虑前两个,有:“1”, “12”, “2”

前三个,有:“1”, “12”, “2”, “14”,“124”,“24”,“4”

可以发现,dp[i]是可以从dp[i - 1]推过来的,第一,打破dp[i - 1]的合法情况要保留,第二,可以加入字符a[i]和前i - 1个组成的不重复的情况结合,得到新的结果,第三,a[i]自己单独做一个。

所以如果不考虑重复,那么dp[i] = 2 * dp[i - 1] + 1

但是考虑前4个,则会出现重复的情况。

首先,dp[3]的应该全部照写下来。“1”, “12”, “2”, “14”,“124”,“24”,“4”

然后,用a[4]去结合 的话。会有,"12"(重复), "122", "22", "142", "1242", "242", "42", "2"(重复)

那么需要减去重复的部分,减去多少呢?

观察到,最近出现相同数字(那个数字2)的位置是2,而且dp[2] =3,哪么,就是用a[2]生成dp[2]的时候的合法情况和现在的重复了,因为用a[2]生成dp[2]的时候,也就是从dp[1]递推过来,我们也保存了dp[1]的合法情况,那么既然用dp[1] + a[2]生成了某些情况,那么当a[4] == a[2]的时候,就没必要再用dp[1]那些合法情况来生成dp[4],因为已经保存在dp[2]中了。所以这个需要减去。

所以需要不断hash到最右的位置。

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <assert.h>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL; #include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <bitset>
const int MOD = 1e9 + ;
int a[ + ];
int tohash[ + ];
long long int dp[ + ];
void work () {
int n;
cin >> n;
for (int i = ; i <= n; ++i) {
cin >> a[i];
}
dp[] = ;
tohash[a[]] = ;
for (int i = ; i <= n; ++i) {
if (tohash[a[i]]) {
dp[i] = (dp[i - ] * + - (dp[tohash[a[i]] - ] + ) + MOD) % MOD;
} else {
dp[i] = (dp[i - ] * + ) % MOD;
}
tohash[a[i]] = i;
}
printf("%d\n", dp[n]);
}
int main () {
#ifdef local
freopen("data.txt", "r", stdin);
// freopen("data.txt", "w", stdout);
#endif
work ();
return ;
}

51NOD 1202 子序列个数 DP的更多相关文章

  1. 51nod 1202 子序列个数

    1202 子序列个数  题目来源: 福州大学 OJ 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题  收藏  关注 子序列的定义:对于一个序列a=a[1],a[2] ...

  2. 1202 子序列个数(DP)

    1202 子序列个数 题目来源: 福州大学 OJ 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题 子序列的定义:对于一个序列a=a[1],a[2],......a[ ...

  3. 51nod 1202 不同子序列个数 [计数DP]

    1202 子序列个数 题目来源: 福州大学 OJ 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题 收藏 关注 子序列的定义:对于一个序列a=a[1],a[2],.. ...

  4. 51nod 1202 不同子序列个数(计数DP)

    1202 子序列个数 基准时间限制:1 秒 空间限制:131072 KB 分值: 40      子序列的定义:对于一个序列a=a[1],a[2],......a[n].则非空序列a'=a[p1],a ...

  5. 51nod 1202 线性dp

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1202 1202 子序列个数 题目来源: 福州大学 OJ 基准时间限制:1 ...

  6. 51nod1202 子序列个数

    看到a[i]<=100000觉得应该从这个方面搞.如果a[x]没出现过,f[x]=f[x-1]*2;否则f[x]=f[x-1]*2-f[pos[a[x]]-1];ans=f[n]-1,然后WA了 ...

  7. FZU 2129 子序列个数 (递推dp)

    题目链接:http://acm.fzu.edu.cn/problem.php?pid=2129 dp[i]表示前i个数的子序列个数 当a[i]在i以前出现过,dp[i] = dp[i - 1]*2 - ...

  8. hdu4632 Palindrome subsequence 回文子序列个数 区间dp

    Palindrome subsequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65535 K (Java/ ...

  9. fzuoj Problem 2129 子序列个数

    http://acm.fzu.edu.cn/problem.php?pid=2129 Problem 2129 子序列个数 Accept: 162    Submit: 491Time Limit: ...

随机推荐

  1. tomcat 启动增加参数

    linux: JAVA_OPTS="$JAVA_OPTS  -Dconfig.type=inte2 -Xms2048m -Xmx2048m -XX:PermSize=128m -XX:Max ...

  2. Css的使用细谈

    Css的使用细谈 Css可以通过简单的更改CSS文件,改变网页的整体表现形式,可以减少我们的工作量,所以她是每一个网页设计人员的必修课. Css简介              (1) CSS是用于布局 ...

  3. Spring 事务管理高级应用难点剖析: 第 3 部分

    本文是“Spring 事务管理高级应用难点剖析” 系列文章的第 3 部分,作者将继续深入剖析在实际 Spring 事务管理应用中容易遇见的一些难点,包括在使用 Spring JDBC 时如果直接获取 ...

  4. bzoj 4827 [Hnoi2017] 礼物 —— FFT

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4827 首先,旋转对应,可以把 b 序列扩展成2倍,则 a 序列对应到的还是一段区间: 再把 ...

  5. HDU2586(LCA应用:在带权树中求任意两点之间的距离)

    How far away ? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  6. 配置android-studio应用的快捷键

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

  7. vue render函数使用jsx语法 可以使用v-model语法 vuex实现数据持久化

    render函数使用jsx语法: 安装插件  transform-vue-jsx 可以使用v-model语法安装插件 jsx-v-model .babelrc文件配置: vuex实现数据持久化 安装插 ...

  8. __attribute__((noreturn))的用法

    外文地址:http://www.unixwiz.net/techtips/gnu-c-attributes.html __attribute__ noreturn    表示没有返回值 This at ...

  9. html5 滚动小球

    <html> <head> <meta charset="utf-8"/> </head> <body onkeydown=& ...

  10. TypeScript完全解读(26课时)_20.声明文件

    首先学习识别已有的js库的类型 识别已有的js库的类型 UMD既可以作为全局库使用,也可以作为模块使用 先在着手来编写一个全局的库 新建文件 接收一个title,改变页面title的值 这里用到 &a ...