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(哈希、尺取法)的更多相关文章

  1. POJ 3061 Subsequence 尺取法 POJ 3320 Jessica's Reading Problem map+set+尺取法

    Subsequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13955   Accepted: 5896 Desc ...

  2. 尺取法 POJ 3320 Jessica's Reading Problem

    题目传送门 /* 尺取法:先求出不同知识点的总个数tot,然后以获得知识点的个数作为界限, 更新最小值 */ #include <cstdio> #include <cmath> ...

  3. POJ 3320 Jessica's Reading Problem

    Jessica's Reading Problem Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6001   Accept ...

  4. POJ 3320 Jessica's Reading Problem 尺取法/map

    Jessica's Reading Problem Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7467   Accept ...

  5. 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 ...

  6. 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 ...

  7. 题解报告: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 ...

  8. <挑战程序设计竞赛> poj 3320 Jessica's Reading Problem 双指针

    地址 http://poj.org/problem?id=3320 解答 使用双指针 在指针范围内是否达到要求 若不足要求则从右进行拓展  若满足要求则从左缩减区域 代码如下  正确性调整了几次 然后 ...

  9. 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 ...

随机推荐

  1. 如何设置Eclipse工作区默认编辑宽度

    1)打开Window  => Preferences窗口 2)打开Formatter属性页从Java => CodeStyle => Formatter 3) 单击New创建一个自己 ...

  2. thinkphp---定义前台视图模板

    具体可以参考: http://document.thinkphp.cn/manual_3_2.html#template_define 方法一:在入口文件中定义 // 定义模板路径 define(&q ...

  3. angularJS中的ng-repeat指令!

    ng-repeat 指令: ng-repeat 指令用来遍历一个数组重复创建当前元素: <ul ng-app="myApp" ng-controller="myAp ...

  4. jconsole远程连接超时问题解决方法

    根据oracle网站上的文档,本地使用jconsole没有问题.但当我从windows连接到linux时(centos5.4)时,老是连接不上). 原因是Linux上JVM给jconsole的RMI配 ...

  5. 【gulp】前端自动化工具---gulp的使用(一)------【巷子】

    什么是gulp?   基于node的自动化构建工具   扩展:开发的时候分为2个节点一个是开发阶段  另一个是部署阶段        开发阶段:源文件不会被压缩            部署阶段:所有文 ...

  6. JavaScript三(对象思想)

    JavaScript并不是面向对象的程序设计语言,但它是基于对象的.JavaScript中的每个函数都可用于创建对象,返回的对象既是该对象的实例,也是object的实例 . 一.对象与关联数组 Jav ...

  7. IOS微信端软键盘收起后界面按钮失效问题

    问题描述: 1.在vue里封装了一个confirm的弹窗(即如下一个弹窗) 2.发现在IOS微信客户端中打开后,当需要在表单中输入内容的时候,很自然的点击了键盘右上角的[完成]按钮 3.啊~~~,惊人 ...

  8. linux加载硬盘过程

    查看系统可用磁盘大小: [root@i-mbyar7df ~]# df -h Filesystem      Size  Used Avail Use% Mounted on /dev/sda1    ...

  9. docker网络部分源码分析

    daemon初始化network controller daemon的配置,网络部分的内容在cmd/dockerd/config_common_unix.go中指定,默认设置一般都为空 // daem ...

  10. java-mybaits-00101-基础安装配制

    一.数据库安装 http://jingyan.baidu.com/article/363872ec2e27076e4ba16fc3.html 二.eclipse连接mysql http://jingy ...