出现次数 哈希表

问题描述

依次读入 N(0 < N < 1,000,000)个整数( <=2×109

),计算每个数是第几次出现。

样例输入

10
10 30 20 1 1 30 30 20 2 1

样例输出

1 1 1 1 2 2 3 2 1 3

限制和约定

时间限制:1s

空间限制:128MB

#include<cstdio>
#include<cmath>
using namespace std;
int p=1000007,a[1000001]={0},first[1000008]={0},next[1000001],noat[1000001];
int main()
{
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        bool found=false;
        scanf("%d",&a[i]);
        int s=a[i]%p;
        for(int j=first[s];j>0;j=next[j])
        {
            if(a[i]==a[j])
            {
                found=true;
                noat[i]=noat[j]+1;
                noat[j]++;
                break;
            }
        }
        if(found==false) {next[i]=first[s];first[s]=i;}
        printf("%d ",noat[i]+1);
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/hfang/p/11240025.html
今日推荐