hash 建表 query 统计重复个数


WLS的数列
难度级别:A; 运行时间限制:1000ms; 运行空间限制:51200KB; 代码长度限制:2000000B
试题描述
WLS喜欢数学,有一天,老师给了他一个长度为N的数列A,问他有多少不同的数。WLS觉得数列太长了,你能不帮他吗?
输入
第一行为一个正整数N,表示数列的长度。
第二行为N个正整数表示这个数列。
输出
输出数列中有多少不同的数。
输入示例
5
1 1 4 2 1
输出示例
3
其他说明
1<=N<=1000
1<=Ai<=10000


//============================================================================
// Name        : hashtable.cpp
// Author      : judyge
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<ctime>
#include<cmath>
#include<string>
using namespace std;

#define N 12350
#define MAX 12345
#define MAXSUM 12500000
#define CLR(arr, what) memset(arr, what, sizeof(arr))

template<class T>
class Hash
{
private:
    int Key[N], Head[N], Next[N], Same[N];
    int top;
public:
    int count;
    void search( int x);
    void push(int x);
    bool pre(int x);
    void clear();
};

template<class T>
inline void Hash<T>::clear()
{
    top = 0;
    count = 0;
    CLR(Head, -1);
    CLR(Next, -1);
    CLR(Same, 0);
}

template<class T>
inline bool Hash<T>::pre(int x)
{
    int temp;
    temp = abs(x) % MAX;
    for(int i = Head[temp]; i != -1; i = Next[i]) //记录重复
    {
        if(x == Key[i])
        {
            Same[i]++;
            return true;
        }
    }
    return false;
}

template<class T>
inline void Hash<T>::push(int x)
{
    if(pre(x) == true) //出现过,Same记录
        return ;
    else //没出现过
    {
        int temp;
        temp = abs(x) % MAX;
        Key[top] = x;
        Next[top] = Head[temp];
        Head[temp] = top;
        Same[top] = 1;
        top++;
    }
}

template<class T>
inline void Hash<T>::search(int x)
{
    int temp;
    temp = abs(x) % MAX;
    for(int i = Head[temp]; i != -1; i = Next[i])
    {
        if(x == -Key[i])
            count += Same[i];
    }
}

int main()
{
	  int m[100005];
      Hash<int> h;
      h.clear();
      int n;
       cin>>n;
    for(int i=0;i<n;i++)
   {
	int j;
    cin>>j;
     m[i]=j;
    h.push(j);
    }

    for(int k=0;k<n;k++)
       {
        h.search(m[k]);
       }
  cout<<h.count;

return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值