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. java版的下雪,大家圣诞快乐

    1. [代码][Java]代码    package com.yk.tools.game; import java.applet.AudioClip;import java.awt.Dimension ...

  2. BZOJ 1632 [Usaco2007 Feb]Lilypad Pond:spfa【同时更新:经过边的数量最小】【路径数量】

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1632 题意: 有一个n*m的池塘.0代表水,1代表荷花,2代表岩石,3代表起点,4代表终点 ...

  3. servlet与jsp理论知识讲解

    servlet是java服务器端编程,是运行在服务器上的.不同于以前的java小程序.                                                         ...

  4. CI核心文件分析之基准测试类 (Benchmark.php)

    <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); /** * CodeIgniter * * ...

  5. html5--5-8 绘制圆/弧

    html5--5-8 绘制圆/弧 学习要点 掌握绘制圆弧的方法 矩形的绘制方法 rect(x,y,w,h)创建一个矩形 strokeRect(x,y,w,hx,y,w,h) 绘制矩形(无填充) fil ...

  6. 【应用】图片翻转js

    图片翻转:图片随着鼠标指针划过进行替换 <img src="example.gif" onmouseover="this.src='exampleTwo.gif'& ...

  7. linux命令学习笔记 : install 命令

    install .作用 install命令的作用是安装或升级软件或备份数据,它的使用权限是所有用户. .格式 ()install [选项]... 来源 目的地 ()install [选项]... 来源 ...

  8. Ubuntu16.04 + cuda9.0 + cudnn7.1.4 + tensorflow安装

    安装前的准备 UEFI 启动GPT分区 Win10和Ubuntu16.04双系统安装 ubuntu16.04 NVIDIA 驱动安装 ubuntu16.04 NVIDIA CUDA8.0 以及cuDN ...

  9. window.open全屏

    window.open全屏   1. window.open(url,'资金计划项超支提醒','width='+(window.screen.availWidth-10)+',height='+(wi ...

  10. mysql : mysql 5.6.13 免安装版配置

    前言:真正用到mysql是在公司的第二个项目下,具体的一些在之前的博客文章(http://www.cnblogs.com/zhengzeze/p/5623440.html)中也提到了,其中涉及到,免安 ...