POJ 3320 Jessica‘s Reading Problem(哈希、尺取法)
http://poj.org/problem?id=3320
题意:
给出一串数字,要求包含所有数字的最短长度。
思路:
哈希一直不是很会用,这道题也是参考了别人的代码,想了很久。
#include<iostream>
#include<algorithm>
#include<string>
#include<cstring>
using namespace std; const int PRIME = ; int n;
int len; //开散列法,也就是用链表来存储,所以下面的len是从PRIME开始的,因为前面的都是头结点。 struct node //key是数值大小,num是该key出现的次数,因为是链表存储,所以next指向下一个结点
{
int key;
int num;
int next;
}p[]; int a[]; int _hash(int num)
{
int k = num%PRIME; //取余
while (p[k].next != -) //该数值已经出现过了
{
if (num > p[p[k].next].key) break; //按递减的方式排列
else if (num == p[p[k].next].key) return p[k].next; //如果已经出现过了,直接返回
k = p[k].next;
}
//没有出现过,添加新的结点
p[len].key = num;
p[len].num = ;
p[len].next = p[k].next;
p[k].next = len;
len++;
return len - ;
} int main()
{
//freopen("D:\\txt.txt", "r", stdin);
while (~scanf("%d", &n) && n)
{
for (int i = ; i < PRIME; i++)
p[i].next = -;
len = PRIME;
int left = ;
int ans;
for (int i = ; i < n; i++)
{
scanf("%d", &a[i]);
int temp = _hash(a[i]);
p[temp].num++;
if (p[temp].num == ) //说明第一次出现,所以肯定要包括进去,此时ans肯定等于i-left+1
{
ans = i - left + ;
continue;
}
//如果之前已经出现过
temp = _hash(a[left]);
//如果left指向的数值后面还有出现,那么可以右移一位
while (left<n - && p[temp].num>)
{
p[temp].num--;
left++;
temp = _hash(a[left]);
}
if (ans > i - left + ) ans = i - left + ;
}
printf("%d\n", ans);
}
return ;
}
接下来再附上尺取法的做法,主要思路和上面是差不多的。
#include<iostream>
#include<algorithm>
#include<string>
#include<cstring>
#include<set>
#include<map>
using namespace std; const int maxn = + ; int n;
int x[maxn];
set<int> p;
map<int, int> q; int main()
{
//freopen("D:\\txt.txt", "r", stdin);
while (~scanf("%d", &n))
{
p.clear();
q.clear();
for (int i = ; i <= n; i++)
{
scanf("%d", &x[i]);
} for (int i = ; i <= n; i++)
p.insert(x[i]); int ans = n;
int total = p.size(); //不重复的数
int sum = ;
int left = , right = ;
while (left<=n && right <= n)
{
if (q[x[right]] == ) sum++; //这个数没有出现过
q[x[right]]++; while (q[x[left]] > )
{
q[x[left]]--;
left++;
}
if (sum == total)
{
ans = min(ans, right - left + );
if (q[x[left]] == ) sum--;
q[x[left]]--;
left++;
}
right++;
}
printf("%d\n", ans);
}
return ;
}
POJ 3320 Jessica‘s Reading Problem(哈希、尺取法)的更多相关文章
- POJ 3061 Subsequence 尺取法 POJ 3320 Jessica's Reading Problem map+set+尺取法
Subsequence Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13955 Accepted: 5896 Desc ...
- 尺取法 POJ 3320 Jessica's Reading Problem
题目传送门 /* 尺取法:先求出不同知识点的总个数tot,然后以获得知识点的个数作为界限, 更新最小值 */ #include <cstdio> #include <cmath> ...
- POJ 3320 Jessica's Reading Problem
Jessica's Reading Problem Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6001 Accept ...
- POJ 3320 Jessica's Reading Problem 尺取法/map
Jessica's Reading Problem Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7467 Accept ...
- POJ 3320 Jessica's Reading Problem 尺取法
Description Jessica's a very lovely girl wooed by lots of boys. Recently she has a problem. The fina ...
- POJ 3320 Jessica's Reading Problem (尺取法)
Jessica's a very lovely girl wooed by lots of boys. Recently she has a problem. The final exam is co ...
- 题解报告:poj 3320 Jessica's Reading Problem(尺取法)
Description Jessica's a very lovely girl wooed by lots of boys. Recently she has a problem. The fina ...
- <挑战程序设计竞赛> poj 3320 Jessica's Reading Problem 双指针
地址 http://poj.org/problem?id=3320 解答 使用双指针 在指针范围内是否达到要求 若不足要求则从右进行拓展 若满足要求则从左缩减区域 代码如下 正确性调整了几次 然后 ...
- poj 3320 Jessica's Reading Problem(尺取法)
Description Jessica's a very lovely girl wooed by lots of boys. Recently she has a problem. The fina ...
随机推荐
- 【BZOJ1500】[NOI2005]维修数列 Splay
[BZOJ1500][NOI2005]维修数列 Description Input 输入的第1 行包含两个数N 和M(M ≤20 000),N 表示初始时数列中数的个数,M表示要进行的操作数目.第2行 ...
- opencv学习笔记——minMaxIdx函数的含义及用法
opencv中有时需要对Mat数据需要对其中的数据求取最大值和最小值.opencv提供了直接的函数 CV_EXPORTS_W void minMaxLoc(InputArray src, CV_OUT ...
- win10配置的静态/动态IP和 DNS的方法
1.配置静态IP和DNS netsh interface ip set address name="以太网" source=static addr=192.168.9.145 ma ...
- gitlab-runner ---CI
gitlab-runner: https://segmentfault.com/a/1190000007180257 https://mp.weixin.qq.com/s?__biz=MjM5MTA1 ...
- RabbitMQ(转)
add by zhj: 如果用Python,那可以用celery,它是一个分布式任务队列,它的broker可以选择Rabbitmq/Redis/Mongodb等, celery通过Kombu这个lib ...
- yum whatprovides 查找哪个包可以提供缺失的文件
yum whatprovides 查找哪个包可以提供缺失的文件
- format的用法:python
https://www.cnblogs.com/wongbingming/p/6848701.html 它通过{}和:来代替%.通过位置 In [1]: '{0},{1}'.format('kzc', ...
- Mysql中的auto_increment
Mysql中的auto_increment 1.创建 2.使用 [1]如果不写固定列,则必须要插入该列,可以直接写Null,否则会报错 [2]可以直接在auto_increment 列上直接插入显式值 ...
- 005-spring cache-配置缓存存储
一.概述 缓存抽象提供了多种存储集成.要使用它们,需要简单地声明一个适当的CacheManager - 一个控制和管理Caches的实体,可用于检索这些实体以进行存储. 1.1.基于JDK Concu ...
- redis error MISCONF Redis is configured to save RDB snapshots
在操作命令incr时发生错误: (error) MISCONF Redis is configured to save RDB snapshots, but is currently not able ...