码迷,mamicode.com
首页 > 其他好文 > 详细

模板:字符串哈希

时间:2018-07-19 21:17:44      阅读:141      评论:0      收藏:0      [点我收藏+]

标签:\n   include   href   tar   style   new   class   base   int   

https://www.luogu.org/problemnew/show/P3370

自然溢出

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 using namespace std;
 6 typedef long long ll;
 7 ll base=19260817;
 8 ll a[10010];
 9 string s;
10 int n, ans=1;
11 ll hashs(string s){
12     int len=s.size();
13     ll ans=0;
14     for (int i=0; i<len; i++)
15         ans=ans*base+(ll)s[i];
16     return ans&0x7fffffff;
17 }
18 int main(){
19     scanf("%d",&n);
20     for (int i=1; i<=n; i++){
21         cin>>s;
22         a[i]=hashs(s);
23     }
24     sort(a+1, a+n+1);
25     for(int i=2; i<=n; i++)
26         if(a[i]!=a[i-1])   ans++;
27     printf("%d\n",ans);
28     return 0;
29 }

 

模板:字符串哈希

标签:\n   include   href   tar   style   new   class   base   int   

原文地址:https://www.cnblogs.com/Aze-qwq/p/9337602.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!