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. 日志系统实战 AOP静态注入

    http://www.cnblogs.com/mushroom/p/3932698.html http://www.cnblogs.com/mushroom/p/4124878.html http:/ ...

  2. 【 Android】使手机屏幕常亮,不进入待机状态

    Android中,申请WakeLock可以让你的进程持续执行即使手机进入睡眠模式,比较实用的是比如后台有网络功能,可以保证操作持续进行. 需要权限 <uses-permission androi ...

  3. linux 统计文件数量

    查找当前目录下compose文件的数量 ls -lr | grep "compose" | wc -l

  4. V2EX的RSS订阅地址

    1.全站RSS输出: https://www.v2ex.com/index.xml 2.单独节点RSS输出: http://www.v2ex.com/feed/{节点名}.xml  以shadowso ...

  5. 在idea中为函数自动生成注释(解决注释无法出现形参的情况)

    1 点击“File”-->“Settings”-->“Live Templates”打开如下对话框,点击右边绿色的加号,创建一个自定义的Template Group,如“Java” 2.选 ...

  6. HanLP https://pypi.python.org/pypi/sumy/

    HanLP - 汉语言处理包 http://hanlp.linrunsoft.com/doc.html https://pypi.python.org/pypi/sumy/

  7. Qt:QPushButton 单击、双击响应区分

    开发环境:win10+vs2015+qt5.9.1 背景:QPushButton的双击事件虽然一直有,但是在双击完成之前,总会响应到单击的事件处理或者连接槽,使用很不方便.自己子类化了一个QPushB ...

  8. Python开发【Django】:组合搜索、JSONP、XSS过滤

    组合搜索 做博客后台时,需要根据文章的类型做不同的检索 1.简单实现 关联文件: from django.conf.urls import url from . import views urlpat ...

  9. django 多数据分库

    路由策略 # -*- coding: utf-8 -*- from django.conf import settings class DatabaseAppsRouter(object): &quo ...

  10. thinkphp处理jQuery EasyUI form表单问题

    jQuery EasyUI form表单不是ajax方式提交,而是在提交的时候新建一个隐藏的iframe并在iframe里面创建一个与绑定表单一样的表单,然后在iframe里面进行同步提交而不是异步提 ...