欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

hash 建表 query 统计重复个数

发布时间:2025/6/15 编程问答 41 豆豆
生活随笔 收集整理的这篇文章主要介绍了 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 统计重复个数的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。