Occurrences hash table

Problem Description

Successively reading N (0 <N <1,000,000) integer ( < = 2 × 10 . 9

), Calculated for each count how many times appear.

Sample input

10
10 30 20 1 1 30 30 20 2 1

Sample Output

1 1 1 1 2 2 3 2 1 3

Restrictions and conventions

Time limit: 1s

Space limitations: 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;
}

 

Guess you like

Origin www.cnblogs.com/hfang/p/11240025.html