• 哈希表


    哈希表

    例题:

    不重复数字

    #include <cstdio>
    #include <cstring>
    #include <iostream>
    #include <algorithm>
    
    using namespace std;
    typedef long long ll;
    const int N=5e5+15;
    const int P=100003;
    inline int read() {
    	int x=0,f=1;char ch=getchar();
    	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    	while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    	return f*x;
    }
    struct HASH{
    	int nxt[N],hd[P+100],val[N],tot,to[N];
    	inline void ins(int v) {
    		int x=(v%P+P)%P;
    		for(int i=hd[x];i;i=nxt[i]) {
    			if(to[i]==v) {
    				++val[i];return;
    			}
    		}
    		to[++tot]=v;val[tot]=1;nxt[tot]=hd[x];hd[x]=tot;
    	}
    	inline int query(int v) {
    		int x=(v%P+P)%P;
    		for(int i=hd[x];i;i=nxt[i]) 
    			if(to[i]==v) return val[i];
    		return 0;
    	}
    	inline void clear() {
    //		for(int i=0;i<=tot;i++) hd[i]=0;
    		memset(hd,0,sizeof(hd));
    		tot=0;
    	}
    }H;
    int n,a[N];
    int main() {
    	int T=read();
    	while(T--) {
    		H.clear();
    		n=read();
    		for(int i=1;i<=n;i++) {
    			a[i]=read();
    			if(H.query(a[i])) continue;
    			H.ins(a[i]);
    			printf("%d ",a[i]);
    		}
    		puts("");
    	}
    	return 0;
    }
    

    CF776C Molly's Chemicals

    #include <cstdio>
    #include <cstring>
    #include <iostream>
    #include <algorithm>
    
    using namespace std;
    typedef long long ll;
    const int N=1e5+10;
    const ll inf=1e15;
    const int P = 100003;
    inline int read() {
    	int x=0,f=1;char ch=getchar();
    	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    	while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    	return f*x;
    }
    int n,k;
    ll a[N],mi[65];
    struct HashTable {
    	int nxt[N],val[N],hd[N],tot;
    	ll to[N];//hash 
    	inline void ins(ll v) {
    		int x=v%P;
    		if(x<0) x+=P;
    		for(int i=hd[x];i;i=nxt[i]) {
    			ll y=to[i];
    			if(y==v) {++val[i];return;}
    		}
    		to[++tot]=v;val[tot]=1;nxt[tot]=hd[x];hd[x]=tot;
    	}
    	inline int query(ll v) {
    		int x=v%P;
    		if(x<0) x+=P;
    		for(int i=hd[x];i;i=nxt[i]) {
    			ll y=to[i];
    			if(y==v) return val[i];
    		}
    		return 0;
    	}
    	inline void clear() {
    		memset(hd,0,sizeof(hd));
    		tot=0;
    	}
    }H;
    int main() {
    	H.clear();
    	n=read();k=read();
    	int len=0;
    	if(k==1) {
    		mi[0]=1;len=1;
    	} else if(k==-1) {
    		mi[0]=1;mi[1]=-1;
    		len=2;
    	} else {
    		ll x=1;
    		while(-inf<=x&&x<=inf) 
    			mi[len++]=x,x*=k;
    	}
    	H.ins(0);
    	ll ans=0,sum=0;
    	for(int i=1;i<=n;i++) {
    		int x=read();sum+=x;
    		for(int j=0;j<len;j++) 
    			ans+=H.query(sum-mi[j]);
    		H.ins(sum);
    	}
    	printf("%lld
    ",ans);
    	return 0;
    }
    

    11.10 模拟赛t1

    https://www.qizhishu.com/codeadmin/front/client/competition_taskProblemDetail.zhtml?TaskID=4898&ClazzID=673&CourseID=3601&ChapterID=360101

    2e9以内的斐波那契数只有41个,我们开个 哈希表,每次枚举这41个数查hash表里有没有(f[j]-a[i]) ,有的话就清空表,答案加一

    #include <bitset>
    #include <cstdio>
    #include <cstring>
    #include <iostream>
    #include <algorithm>
    
    using namespace std;
    typedef long long ll;
    const int N=1e5+10;
    inline int read() {
    	int x=0,f=1;char ch=getchar();
    	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    	while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    	return f*x;
    }
    void write(int x) {
    	static short st[32];short tp=0;
    	do st[++tp]=x%10,x/=10;while(x);
    	while(tp) putchar('0'|st[tp--]);
    	putchar('
    ');
    }
    int n,f[50],a[N];
    const int P=133331;
    struct node{
    	int to[P+5],nxt[P+5],hd[P+5],val[P+5],tot;
    	inline void ins(int x) {
    		int v=(x%P+P)%P;
    		for(int i=hd[v];i;i=nxt[i]) {
    			if(to[i]==x) {
    				++val[i];
    				return;
    			}
    		}
    		to[++tot]=x;val[tot]=1;nxt[tot]=hd[v];hd[v]=tot;
    	}
    	inline int query(int x) {
    		int v=(x%P+P)%P;
    		for(int i=hd[v];i;i=nxt[i]) 
    			if(to[i]==x) 
    				return val[i];
    		return 0;
    	}
    	inline void clear() {
    		memset(hd,0,sizeof(hd));
    		tot=0;
    	}
    }Hash; 
    int main() {
    //	freopen("f1.in","r",stdin);
    //	freopen("my.out","w",stdout);
    	n=read();
    	for(int i=1;i<=n;i++) a[i]=read();
    	f[1]=1;f[2]=2;
    	for(int i=3;i<=41;i++) 
    		f[i]=f[i-1]+f[i-2];
    	int ans=1;
    	Hash.ins(a[1]);
    	for(int i=2;i<=n;i++) {
    		for(int j=1;j<=41;j++) {
    			if(Hash.query(f[j]-a[i])) {
    				Hash.clear();
    				ans++;
    				break;				
    			}
    		}
    		Hash.ins(a[i]);
    	}
    	write(ans);
    	return 0;
    }
    
    
    
    
    
    
  • 相关阅读:
    算法:最小公倍数的求解方法
    使用C语言中qsort()函数对浮点型数组无法成功排序的问题
    用两个栈模拟实现一个队列
    单链表反向查找
    单链表逆序
    斐波那契(Fibonacci)数列的几种计算机解法
    最大子列和问题
    Visual Studio个人常用快捷键
    数字根(digital root)
    秦九韶算法(霍纳算法)求解多项式
  • 原文地址:https://www.cnblogs.com/ke-xin/p/13953112.html
Copyright © 2020-2023  润新知