zoukankan      html  css  js  c++  java
  • 双哈希模板

    我以前比较懒, (hash) 该学的时候没学, 现在来补一下。


    如题, 就是双哈希过洛谷模板题(字符串哈希)。
    因为我觉得双哈希似乎比较稳(但是它确实慢qwq)。

    Luogu数据AC代码

    #include<bits/stdc++.h>
    using namespace std;
    const int mod1 = 10000007;
    const int mod2 = 13333337;
    int base1, base2;
    int h1[10005], h2[10005];
    bool eq(int i,int j) {
    	return (h1[i]==h1[j] && h2[i]==h2[j]);
    }
    
    int n;
    char s[10005][1505];
    int main()
    {
    	srand((unsigned)time(0));
    	base1 = rand()%100+200, base2 = rand()%300+400;
    	
    	cin >> n; for(int i=1;i<=n;++i) scanf("%s", s[i]);
    	for(int i=1;i<=n;++i) {
    		int len = strlen(s[i]);
    		for(int j=0;j<len;++j)
    		{
    			h1[i] = (h1[i]*base1+s[i][j]) % mod1;
    			h2[i] = (h2[i]*base2+s[i][j]) % mod2;
    		}
    	}
    	int ans = n;
    	for(int i=1; i<=n; ++i)
    		for(int j=i-1;j>=1;--j)
    		if(eq(i,j)){
    			--ans;
    			break;
    		}
    	cout << ans;
    	return 0;
    }
    
  • 相关阅读:
    python urllib urllib2
    python 标准库获取网络信息
    dell N1500 安全配置
    python jinjia2模板使用
    flask-admin
    github使用
    python中文处理
    C++输入和输出中进制问题
    hdu1708(C++)
    hdu1017(C++)
  • 原文地址:https://www.cnblogs.com/tztqwq/p/12785364.html
Copyright © 2011-2022 走看看