7.6 KPOJ 3320Jessica's Reading Problem

来源:互联网 发布:傲剑金蛇升级数据大全 编辑:程序博客网 时间:2024/05/22 07:02

题意:

给一串长为n的数字串,求恰好包含所有数字种类的最小串长度。

思路:

用双指针即可。

代码:

#include <iostream>using namespace std;const int h_max=1000007;int hkey[h_max],hmap[h_max];int page[h_max],cnt[h_max];int n,amt=0;void add(const int& x){    int u=x%h_max;    while(hmap[u]!=0&&hkey[u]!=x){        u=(u+1)%h_max;    }    hkey[u]=x;    hmap[u]++;}int f(const int& x){    int u=x%h_max;    while(hmap[u]!=0&&hkey[u]!=x){        u=(u+1)%h_max;    }    return u;}int main(){    ios::sync_with_stdio(0);    cin>>n;    if(n==1){cout<<1<<endl;return 0;}    for (int i = 0; i <= n; i++)        cnt[i] = 100000000;    for(int i=0;i<n;i++)        cin>>page[i];    int l=0,r=1;    add(page[0]);    while(r<n){        if(hmap[f(page[r])]==0) amt++;        add(page[r]);        while(hmap[f(page[l])]>1){            hmap[f(page[l])]--;            l++;        }        cnt[amt]=min(cnt[amt],r-l+1);        r++;    }    cout<<cnt[amt]<<endl;    return 0;}


0 0