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[1000050];//哈希表,统计数字出现的次数 int Maxn = 0;//统计总共有多少个不同的数字 int ans[1000050];//ans[i]代表当出现的不同数字个数为i的时候的最短长度 int num[1000050];//输入的数字 //插入哈希表 void InsertHT(int n) { int addr = n % MAX_VAL; while(HashTable[addr].nCount != 0 && HashTable[addr].x != n) { addr = (addr + 1) % MAX_VAL; } HashTable[addr].nCount++; HashTable[addr].x = n; } //得到哈希表中元素的地址 int GetAddr(int n) { int addr = n % MAX_VAL; while(HashTable[addr].nCount != 0 && HashTable[addr].x != n) { addr = (addr + 1) % MAX_VAL; } return addr; } int main() { int n; scanf("%d", &n); if (n == 1) { printf("1\n"); return 0; } for (int i = 0; i <= n; i++) { ans[i] = 100000000; } for (int i = 0; i < n; i++) { scanf("%d", &num[i]); } int i = 0, j = 1; InsertHT(num[0]); while(j < n) { //如果某个数字的计数为0,则说明这是一个新数字,所以Maxn加1 if (HashTable[GetAddr(num[j])].nCount == 0) { Maxn++; } InsertHT(num[j]);//将数字插入到哈希表 //i从前向后遍历,如果某个数字的出现次数大于1,则i加1 while(HashTable[GetAddr(num[i])].nCount > 1) { HashTable[GetAddr(num[i])].nCount--; i++; } //每次记录当前不同数字为Maxn的最短长度 ans[Maxn] = min(ans[Maxn] ,j - i + 1); j++;//j加1,跳转到下一个数字 } printf("%d\n", ans[Maxn]);//最后打印的结果即为所有数字都出现的最短连续子序列 return 0; }