生活随笔
收集整理的這篇文章主要介紹了
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;
}
總結
以上是生活随笔為你收集整理的hash 建表 query 统计重复个数的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。