日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

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

编程问答

hash 建表 query 统计重复个数

發布時間:2025/6/15 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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 统计重复个数的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。