• POJ 3320 Jessica's Reading Problem


    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
    ");
            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
    ", ans[Maxn]);//最后打印的结果即为所有数字都出现的最短连续子序列
        return 0;
    }
  • 相关阅读:
    MSSQL 链接Oracle 表
    ORACLE FORM ZA 常用子程序
    FORM 中提交报表并查看结果
    Form界面设置只读
    FRM30403
    oracle 分区表的建立方法
    在Oracle Form中,如何实现自动编号(行号)的功能
    Form中格式化数字、金额
    MSC_SYSTEM_ITEMS,mtl_system_items_b区别
    C#中判断一个字符串是不是包含非法字符
  • 原文地址:https://www.cnblogs.com/lzmfywz/p/3262476.html
Copyright © 2020-2023  润新知