POJ 3320 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的更多相关文章
- 尺取法 POJ 3320 Jessica's Reading Problem
题目传送门 /* 尺取法:先求出不同知识点的总个数tot,然后以获得知识点的个数作为界限, 更新最小值 */ #include <cstdio> #include <cmath> ...
- 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 尺取法/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(哈希、尺取法)
http://poj.org/problem?id=3320 题意:给出一串数字,要求包含所有数字的最短长度. 思路: 哈希一直不是很会用,这道题也是参考了别人的代码,想了很久. #include&l ...
- <挑战程序设计竞赛> 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 ...
- 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 ...
随机推荐
- jenkins插件 查看job修改历史
文章来自:http://www.ciandcd.com文中的代码来自可以从github下载: https://github.com/ciandcd 插件jobConfigHistory(https:/ ...
- 无线客户端框架设计(5.1):将JSON映射为实体对象(iOS篇)
iOS开发人员已经习惯于将JSON转换为字典或者数组来进行操作了,接下来我要做的事情,可能匪夷所思,但是,对WP和Android开发人员而言,他们更倾向于将JSON转换为实体对象进行操作. 我所设计的 ...
- atitit.android模拟器使用报告
atitit.android模拟器使用报告 靠谱助手 仅仅7--15M,只助手,没android模拟器.. BlueStacks新版本App Player采用名为Layercake的技术,可以让针对A ...
- Rails下cloud datastore的使用
Rails下cloud datastore的使用 背景 部门有一个项目要用Ruby做 WebAPI,DB使用关系型数据库Cloud Sql和非关系型数据库Cloud Datastore . 还不了 ...
- Java的从浅至深绕坑而行的学习
package day02; /** * 1:java初学习,避免面试时一些HR挖的坑. * @author biexiansheng * */ public class Test02 { publi ...
- Android 学习之--android多线程断点下载
我们平时都用"迅雷"下载软件,当下载到一半的时候突然断网,下次开启的时候能够从上次下载的地方继续下载,而且下载速度很快,那么这是怎么做到的呢! 其实它的“快”其实就是多线程的下载实 ...
- 解决android SwipeRefreshLayout recyclerview 不能下拉
http://stackoverflow.com/questions/25178329/recyclerview-and-swiperefreshlayout 23down vote write th ...
- C#:如何解决WebBrowser.DocumentCompleted事件的多次调用
关于DocumentCompleted事件,MSDN给出的解释是在文档加载完毕后执行,但是在我的程序中DocumentCompleted却被多次调用,查了一下资料,大概出现了以下几种情况. 1.Web ...
- mysql 优化配置参数详解
在 my.cnf 文件中 各设置参数的含义如下: innodb_data_home_dir 这是InnoDB表的目录共用设置.如果没有在 my.cnf 进行设置,InnoDB 将使用MySQL的 da ...
- 利用Android Studio、MAT对Android进行内存泄漏检测
利用Android Studio.MAT对Android进行内存泄漏检测 Android开发中难免会遇到各种内存泄漏,如果不及时发现处理,会导致出现内存越用越大,可能会因为内存泄漏导致出现各种奇怪的c ...