子序列 NYOJ (尺取法+队列+hash) (尺取法+离散化)
子序列
- 描述
-
给定一个序列,请你求出该序列的一个连续的子序列,使原串中出现的所有元素皆在该子序列中出现过至少1次。
如2 8 8 8 1 1,所求子串就是2 8 8 8 1。
- 输入
- 第一行输入一个整数T(0<T<=5)表示测试数据的组数
每组测试数据的第一行是一个整数N(1<=N<=1000000),表示给定序列的长度。
随后的一行有N个正整数,表示给定的序列中的所有元素。
数据保证输入的整数都不会超出32位整数的范围。 - 输出
- 对于每组输入,输出包含该序列中所有元素的最短子序列的长度
- 样例输入
-
2
5
1 8 8 8 1
6
2 8 8 8 1 1 - 样例输出
-
2
5 - 来源
- POJ月赛改编
-
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<sstream>
#include<algorithm>
#include<queue>
#include<deque>
#include<iomanip>
#include<vector>
#include<cmath>
#include<map>
#include<stack>
#include<set>
#include<fstream>
#include<memory>
#include<list>
#include<string>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
#define MAXN 1000044
#define INF 1000000009
#define eps 0.00000001
/*
利用队列从前到后遍历数组元素
当前元素未在之前出现过,更新ans为队列元素个数
当前元素在之前出现过
如果当前队首元素在hash表中出现次数多于1次,pop
*/
int T, n, aim, a[MAXN],b[MAXN];
typedef struct Hashnode
{
int data;
int cnt;
struct Hashnode* next;
}*List;
typedef struct Hashtb
{
List* L;
}*HashTable;
HashTable Init()
{
HashTable H = (HashTable)malloc(sizeof(Hashtb));
H->L = (List*)malloc(sizeof(List)*MAXN);
for (int i = ; i < MAXN; i++)
{
H->L[i] = (List)malloc(sizeof(Hashnode));
H->L[i]->data = ;
H->L[i]->cnt = ;
H->L[i]->next = NULL;
}
return H;
}
int Hash(int x)
{
return x%MAXN;
}
void clear(HashTable H)
{
for (int i = ; i < MAXN; i++)
{
H->L[i]->data = ;
H->L[i]->cnt = ;
H->L[i]->next = NULL;
}
}
List find(int x, HashTable H)
{
List l = H->L[Hash(x)];
List p = l->next;
while (p != NULL&&p->data != x)
p = p->next;
return p;
}
int cnt(int x, HashTable H)
{
List p = find(x, H);
if (p == NULL)
return -;
else
{
if (p->cnt > )
{
p->cnt--;
return p->cnt + ;
}
else
return p->cnt;
}
}
bool Insert(int x, HashTable H)
{
List l = H->L[Hash(x)];
List p = find(x, H);
if (!p)
{
List tmp = (List)malloc(sizeof(Hashnode));
tmp->data = x;
tmp->cnt = ;
tmp->next = l->next;
l->next = tmp;
return false;
}
else
{
p->cnt++;
return true;
}
}
int main()
{
scanf("%d", &T);
HashTable H = Init();
while (T--)
{
clear(H);
scanf("%d", &n);
int ans = INF,tmp;
queue<int> q;
for (int i = ; i < n; i++)
{
scanf("%d", &tmp);
if (!Insert(tmp,H))
{
q.push(tmp);
ans = q.size();
}
else
{
q.push(tmp);
while (!q.empty()&&cnt(q.front(),H) > )
{
q.pop();
}
ans = min(ans,(int)q.size());
}
}
printf("%d\n", ans);
}
return ;
}做法2 离散化+ 尺取
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<sstream>
#include<algorithm>
#include<queue>
#include<deque>
#include<iomanip>
#include<vector>
#include<cmath>
#include<map>
#include<stack>
#include<set>
#include<fstream>
#include<memory>
#include<list>
#include<string>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
#define MAXN 1000044
#define INF 1000000009
#define eps 0.00000001
int T, n;
int a[MAXN], b[MAXN], cnt[MAXN];
int bsearch(int l, int r, int aim)
{
while (l <= r)
{
int mid = (l + r) / ;
if (b[mid] == aim)
{
return mid;
}
else if (b[mid] < aim)
{
l = mid + ;
}
else
r = mid - ;
}
}
int main()
{
scanf("%d", &T);
while (T--)
{
scanf("%d", &n);
for (int i = ; i < n; i++)
scanf("%d", &a[i]);
memcpy(b, a, sizeof(a));
memset(cnt, , sizeof(cnt));
sort(b, b + n);
int size = unique(b, b + n) - b;
for (int i = ; i < n; i++)
{
a[i] = bsearch(, size, a[i]);
}
//for (int i = 0; i < n; i++)
// cout << a[i] << endl;
int ans = n, l = , sum = ;
cnt[a[]] = ;
for (int r = ; r < n; r++)
{
while (sum == size)
{
cnt[a[l]]--;
if (cnt[a[l]] == ) sum--;
l++;
ans = min(ans, r - l + );
}
if (cnt[a[r]] == ) sum++;
cnt[a[r]]++;
}
printf("%d\n", ans);
}
return ;
}
子序列 NYOJ (尺取法+队列+hash) (尺取法+离散化)的更多相关文章
- POJ 3320 尺取法,Hash,map标记
1.POJ 3320 2.链接:http://poj.org/problem?id=3320 3.总结:尺取法,Hash,map标记 看书复习,p页书,一页有一个知识点,连续看求最少多少页看完所有知识 ...
- UVA 11019 Matrix Matcher(二维hash + 尺取)题解
题意:在n*m方格中找有几个x*y矩阵. 思路:二维hash,总体思路和一维差不太多,先把每行hash,变成一维的数组,再对这个一维数组hash变成二维hash.之前还在想怎么快速把一个矩阵的hash ...
- CF452F Permutations/Luogu2757 等差子序列 树状数组、Hash
传送门--Luogu 传送门--Codeforces 如果存在长度\(>3\)的等差子序列,那么一定存在长度\(=3\)的等差子序列,所以我们只需要找长度为\(3\)的等差子序列.可以枚举等差子 ...
- BZOJ2124: 等差子序列(树状数组&hash -> bitset 求是否存在长度为3的等差数列)
2124: 等差子序列 Time Limit: 3 Sec Memory Limit: 259 MBSubmit: 2354 Solved: 826[Submit][Status][Discuss ...
- bzoj 2124 等差子序列 树状数组维护hash+回文串
等差子序列 Time Limit: 3 Sec Memory Limit: 259 MBSubmit: 1919 Solved: 713[Submit][Status][Discuss] Desc ...
- 【BZOJ2124】等差子序列 树状数组维护hash值
[BZOJ2124]等差子序列 Description 给一个1到N的排列{Ai},询问是否存在1<=p1<p2<p3<p4<p5<…<pLen<=N ...
- 栈 队列 hash表 堆 算法模板和相关题目
什么是栈(Stack)? 栈(stack)是一种采用后进先出(LIFO,last in first out)策略的抽象数据结构.比如物流装车,后装的货物先卸,先转的货物后卸.栈在数据结构中的地位很重要 ...
- bzoj2124 等差子序列(树状数组+hash)
题意 给你一个1~n排列,问有没有一个等差数列(长度至少为3) 题解 我居然自己想到了正解. 但我最后写挂了,所以我又看了题解. 我们维护了一个以权值为下标的01序列. 我们扫描整个序列.对于每一个正 ...
- SSY的队列 hash+记忆化
题目描述 \(SSY\) 是班集体育委员,总喜欢把班级同学排成各种奇怪的队形,现在班级里有 \(N\) 个身高互不相同的同学,请你求出这 \(N\) 个人的所有排列中任意两个相邻同学的身高差均不为给定 ...
随机推荐
- linux命令 - free -m
如下显示free是显示的当前内存的使用,-m的意思是M字节来显示内容.我们来一起看看. [root@zabbix ~]# free -m total used free shared buffers ...
- PCB ODB++(Gerber)图形绘制实现方法
这里讲解一下用net解析PCB图形绘制实现方法 一.解析PCB图形绘制实现 解析PCB图形,说简单也非常简单,先说一下,PCB Gerber图形由:点,线,弧,铜皮,文字 5类元素组成,通常简写为:P ...
- Oh,mygoddess
很早的时候就看了这一道题目 , 当时不会做 , 现在 边听歌边写无压力 ........ 题意 : 光辉骑士 一直都在 迷宫的右上角 , 第一行给你迷宫的规格 , 下面是迷宫 "O" ...
- Codeforces 792D
题意:给定一棵拥有n个节点的满二叉树(即n==2^x-1),q个查询,每次给出一个节点的编号,再给出一个由L,R,U组成的字符串序列,依次表示向左子节点.右子节点.父节点移动,如果移动不合法,则忽略. ...
- [转]Linux 正则表达式详解
转自:http://www.jb51.net/article/42989.htm 一.linux文本查找命令 在说linux正规表达式之前,还介绍下linux中查找文本文件常用的三个命令: 1.gre ...
- 大话设计模式--DI(依赖注入)
1.背景 想象一个场景:有个功能通过某个参数决定了路由到不同的方法上或者几个方法模块可以自由搭配,咋办?一般人会对每个方法写一个helper(比如SendMessageForEmail.SendMes ...
- JS——全选与全不选
1.每个子input标签都需要进行判断 2.使用开闭原则,一旦满足条件就改变默认值 3.在给主input标签注册事件时,要求主input标签的checked值赋值给子标签 <!DOCTYPE h ...
- python 分割文件、组合文件
import glob big_file = open('index.sql', 'rb') bak_file = 'index_bak' i = 1 while True: chunk = big_ ...
- nested exception is java.io.FileNotFoundException: class path resource [jdbc.properties] cannot be opened because it does not exist
Could not load properties; nested exception is java.io.FileNotFoundException: class path resource [j ...
- 【Caffe】Ubuntu16.04上配置安装caffe(Only CPU)
一.首先看看自己的系统,Ubuntu16.04,cpu,没有Nvidia,没有opencv 二.安装依赖包 安装protobuf,leveldb,snappy,OpenCV,hdf5, protobu ...