POJ 3320 Jessica's Reading Problem

时间:2022-09-23 21:12:12
Jessica's Reading Problem
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 6001   Accepted: 1800

Description

Jessica's a very lovely girl wooed by lots of boys. Recently she has a problem. The final exam is coming, yet she has spent little time on it. If she wants to pass it, she has to master all ideas included in a very thick text book. The author of that text book, like other authors, is extremely fussy about the ideas, thus some ideas are covered more than once. Jessica think if she managed to read each idea at least once, she can pass the exam. She decides to read only one contiguous part of the book which contains all ideas covered by the entire book. And of course, the sub-book should be as thin as possible.

A very hard-working boy had manually indexed for her each page of Jessica's text-book with what idea each page is about and thus made a big progress for his courtship. Here you come in to save your skin: given the index, help Jessica decide which contiguous part she should read. For convenience, each idea has been coded with an ID, which is a non-negative integer.

Input

The first line of input is an integer P (1 ≤ P ≤ 1000000), which is the number of pages of Jessica's text-book. The second line contains P non-negative integers describing what idea each page is about. The first integer is what the first page is about, the second integer is what the second page is about, and so on. You may assume all integers that appear can fit well in the signed 32-bit integer type.

Output

Output one line: the number of pages of the shortest contiguous part of the book which contains all ideals covered in the book.

Sample Input

5
1 8 8 8 1

Sample Output

2
题目大意:输入一串数字,球连续数字串的最短长度,使得数字串包含数字串中的所有字符。
解题方法:用哈希表统计每个数字出现的次数,然后求最短长度,关于这道题很多人用哈希表+二分,这样时间复杂度为O(n*logn),我采用的方法是用i,j两个下标直接遍历,时间复杂度为O(n)。
#include <stdio.h>
#include <iostream>
using namespace std; #define MAX_VAL 1000050 typedef struct
{
int x;
int nCount;
}Hash; Hash HashTable[];//哈希表,统计数字出现的次数
int Maxn = ;//统计总共有多少个不同的数字
int ans[];//ans[i]代表当出现的不同数字个数为i的时候的最短长度
int num[];//输入的数字 //插入哈希表
void InsertHT(int n)
{
int addr = n % MAX_VAL;
while(HashTable[addr].nCount != && HashTable[addr].x != n)
{
addr = (addr + ) % MAX_VAL;
}
HashTable[addr].nCount++;
HashTable[addr].x = n;
} //得到哈希表中元素的地址
int GetAddr(int n)
{
int addr = n % MAX_VAL;
while(HashTable[addr].nCount != && HashTable[addr].x != n)
{
addr = (addr + ) % MAX_VAL;
}
return addr;
} int main()
{
int n;
scanf("%d", &n);
if (n == )
{
printf("1\n");
return ;
}
for (int i = ; i <= n; i++)
{
ans[i] = ;
}
for (int i = ; i < n; i++)
{
scanf("%d", &num[i]);
}
int i = , j = ;
InsertHT(num[]);
while(j < n)
{
//如果某个数字的计数为0,则说明这是一个新数字,所以Maxn加1
if (HashTable[GetAddr(num[j])].nCount == )
{
Maxn++;
}
InsertHT(num[j]);//将数字插入到哈希表
//i从前向后遍历,如果某个数字的出现次数大于1,则i加1
while(HashTable[GetAddr(num[i])].nCount > )
{
HashTable[GetAddr(num[i])].nCount--;
i++;
}
//每次记录当前不同数字为Maxn的最短长度
ans[Maxn] = min(ans[Maxn] ,j - i + );
j++;//j加1,跳转到下一个数字
}
printf("%d\n", ans[Maxn]);//最后打印的结果即为所有数字都出现的最短连续子序列
return ;
}

POJ 3320 Jessica's Reading Problem的更多相关文章

  1. 尺取法 POJ 3320 Jessica&&num;39&semi;s Reading Problem

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

  2. POJ 3061 Subsequence 尺取法 POJ 3320 Jessica's Reading Problem map&plus;set&plus;尺取法

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

  3. POJ 3320 Jessica&&num;39&semi;s Reading Problem 尺取法&sol;map

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

  4. POJ 3320 Jessica&&num;39&semi;s Reading Problem 尺取法

    Description Jessica's a very lovely girl wooed by lots of boys. Recently she has a problem. The fina ...

  5. POJ 3320 Jessica&OpenCurlyQuote;s Reading Problem(哈希、尺取法)

    http://poj.org/problem?id=3320 题意:给出一串数字,要求包含所有数字的最短长度. 思路: 哈希一直不是很会用,这道题也是参考了别人的代码,想了很久. #include&l ...

  6. &lt&semi;挑战程序设计竞赛&gt&semi; poj 3320 Jessica&&num;39&semi;s Reading Problem 双指针

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

  7. poj 3320 Jessica&&num;39&semi;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&Tab;Jessica&&num;39&semi;s Reading Problem (尺取法)

    Jessica's a very lovely girl wooed by lots of boys. Recently she has a problem. The final exam is co ...

  9. 题解报告:poj 3320 Jessica&&num;39&semi;s Reading Problem(尺取法)

    Description Jessica's a very lovely girl wooed by lots of boys. Recently she has a problem. The fina ...

随机推荐

  1. Struts2 easy UI插件

    一.easy UI是类似于jQuery UI的插件库,它提供了丰富的各种常用插件:tree.datagrid... tree插件: 语法:$(selector).tree([settings]); 常 ...

  2. aspcms标签

    [newslist:date style=yy-m-d] 日期格式 {aspcms:sitepath}/Templates/{aspcms:defaulttemplate} 幻灯片标签{aspcms: ...

  3. github神器--Atom编辑器初体验

    Atom 1.0正式式版已经出来好几天,自从听说github出了这神器之后,一直想体验一吧,这两天终于体验上. 下载: https://atom.io/ 其实,我的网速还不错,但总是下载到一半就没网速 ...

  4. Part 3 ViewData and ViewBag in mvc

    ViewBag and ViewData is a mechanism(机制) to pass data from controller to view. We use '@' symbol(符号) ...

  5. &lbrack;转&rsqb;&period;NET程序破解仅需三步

    近期开发公司商城,为了简化开发用了V5Shop网店程序.本来预计一个月完工,哪知道出现一堆问题大大增加了我的工作量(早知道还不如全部自己写了). 破V5Shop真不地道,说是免费的,结果程序一大堆问题 ...

  6. RAID磁盘阵列及CentOS7系统启动流程

    磁盘阵列(Redundant Arrays of Independent Disks,RAID),有“独立磁盘构成的具有冗余能力的阵列”之意,,数据读取无影响.将数据切割成许多区段,分别存放在各个硬盘 ...

  7. Intellij IDEA超好用的快捷键

    1.首推 Ctrl+Shift+Enter,自动跳出括号,在行尾添加分号,并自动回车. 2.Ctrl+Shift+J,将多行代码折叠成一行. 当你遇到的时候,这个快捷键非常实用.比如你在写painle ...

  8. 数据结构实习 Problem H 迷宫的最短路径

    数据结构实习 Problem H 迷宫的最短路径 题目描述 设计一个算法找一条从迷宫入口到出口的最短路径. 输入 迷宫的行和列m n 迷宫的布局 输出 最短路径 样例输入 6 8 0 1 1 1 0 ...

  9. node 垃圾回收

    一些思考 回收 nodejs垃圾回收 跟浏览器js不同,  以下代码会找出内存泄露 var theThing = null var replaceThing = function () { var o ...

  10. 规约模式的ef拼接

    public class LamadaExtention<Dto> where Dto : new() { private List<Expression> m_lstExpr ...