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 ...
随机推荐
- 什么是runtime?什么是webgl?
一 什么是Runtime? Egret官方解释:https://www.egret.com/products/runtime.html 二.什么是WebGL渲染? egret官方解释:http://d ...
- python之xlwt模块列宽width、行高Heights详解
今天用python操作excel时,发现xlwt的API中没有对width.height有更多介绍,且使用时也不知道width取多少合适.现在这做个详细介绍 使用版本: python:2.7.5 xl ...
- [python] python单元测试经验总结
python写单元大多数都会用到unittest和mock,测试代码覆盖率都会用到coverage,最后再用nose把所有的东西都串起来,这样每次出版本,都能把整个项目的单元测试都运行一遍. Unit ...
- github团队协作教程
跟着笔者魔鬼般的步伐,我们一起来瞅瞅一个团队协作的任务如何进行版本管理吧~ 要跟上哦~ =============================================== 首先我们先来看下 ...
- python-social-auth with Django: ImportError: No module named 'social_django' 解决方法
To use Django with python social auth, you need to install the Django app as well. You can specify t ...
- PHP 的“魔术常量”
名称 说明 __LINE__ 文件中的当前行号. __FILE__ 文件的完整路径和文件名.如果用在被包含文件中,则返回被包含的文件名.自 PHP 4.0.2 起,__FILE__ 总是包含一个绝对路 ...
- sql 中如何将返回的记录某一条置顶
将table1中id 为2的记录置顶select * from table1order by case when id='2' then 0 else 1 end 例子:将已发布的置顶,status ...
- ubuntu 安装搜狗
先按照这个选择fcitx: https://blog.csdn.net/qq_40563761/article/details/82664851 然后重启 右上角会出现键盘图片点击选configura ...
- Replication--复制与CDC和镜像
复制和CDC 都是使用logreader来从日志中读取数据的变更,然后写入到分发库(复制)或变更表中. 1> 单独创建复制发布 在该情况下,会在分发服务器上创建日志读取代理作业(ServerNa ...
- mysql索引详解(转)
什么是索引 索引用来快速地寻找那些具有特定值的记录,所有MySQL索引都以B-树的形式保存.如果没有索引,执行查询时MySQL必须从第一个记录开始扫描整个表的所有记录,直至找到符合要求的记录.表里面的 ...