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

Hash

时间:2018-06-06 12:37:00      阅读:156      评论:0      收藏:0      [点我收藏+]

标签:tin   例题   tar   nbsp   2-2   stc   字符串匹配   .com   char   

基本概念:传送门

补充:base取131,163,19260817

用unsigned long long自然溢出。返回ans&0x7fffffff,0x7fffffff为int,缩小ans的范围,限制在31位内。

 

例题1:UESTC1

题解:hash模板题。

技术分享图片
 1 //A
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <iostream>
 5 #include <algorithm>
 6 using namespace std;
 7 
 8 typedef unsigned long long ull;
 9 ull base=163;
10 
11 ull gethash(char s[]){
12     int len=strlen(s);
13     ull ans=0;
14     for(int i=0;i<len;i++)
15     ans=ans*base+(ull)s[i];
16     return ans;
17 }
18 
19 char s[1234];
20 ull p[30000+10];
21 
22 int main(){
23     int n;
24     scanf("%d",&n);
25     for(int i=1;i<=n;i++){
26         scanf("%s",s);
27         p[i]=gethash(s);
28     }
29     sort(p+1,p+1+n);
30     int ans=1;
31     for(int i=2;i<=n;i++)
32     if(p[i]!=p[i-1]) ans++;
33     printf("%d\n",ans);
34     return 0;
35 }
View Code

 

例题2:UESTC2

题解:字符串匹配,hash预处理。传送门

技术分享图片
 1 //C
 2 #include <queue>
 3 #include <cstdio>
 4 #include <cstring>
 5 #include <iostream>
 6 #include <algorithm>
 7 using namespace std;
 8 
 9 const int N=1e5+10;
10 typedef unsigned long long ull;
11 ull base=163;
12 char s[N];
13 ull p[N],Hash[N];
14 
15 void init(){
16     p[0]=1;
17     Hash[0]=0;
18     int n=strlen(s+1);
19     for(int i=1;i<N;i++) p[i]=p[i-1]*base;
20     for(int i=1;i<N;i++) Hash[i]=Hash[i-1]*base+(s[i]-a);
21 }
22 
23 ull get(int l,int r){
24     return Hash[r]-Hash[l-1]*p[r-l+1];
25 }
26 
27 int main(){
28     scanf("%s",s+1);
29     init();
30     int n;
31     scanf("%d",&n);
32     for(int i=1;i<=n;i++){
33         ull a,b;
34         scanf("%llu%llu",&a,&b);
35         printf("%llu\n",get(a,b));
36     }
37     return 0;
38 }
View Code

 

例题3:HDU 4300

题解:每组样例有两行,第一行为26个字母相应对应什么,比如第二个样例的q翻译成a。下一行给出密文和明文的结合,明文可能是不完全的,需要补充并且使得最短。最好情况就是那一行刚好密文一半,明文一半;最坏情况就是全部都是密文。我们对给出的字符串分别进行全明文和全密文hash,找到一段最长的明文前缀和对应暗文前缀相匹配,尽量缩减字符串。

技术分享图片
 1 //http://acm.hdu.edu.cn/showproblem.php?pid=4300
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <iostream>
 5 #include <algorithm>
 6 using namespace std;
 7 
 8 typedef unsigned long long ull;
 9 const int N=100000+10;
10 char s[30],str[N];
11 int m[30];
12 ull p[N],hash1[N],hash2[N];
13 ull base=163;
14 
15 void init(){
16     p[0]=1;
17     for(int i=1;i<N;i++) p[i]=p[i-1]*base;
18 }
19 
20 ull get(int l,int r,ull g[]){
21     return g[r]-g[l-1]*p[r-l+1];
22 }
23 
24 void solve(){
25     for(int i=1;i<=26;i++) m[s[i]-a+1]=i;
26     hash1[0]=0;hash2[0]=0;
27     int n=strlen(str+1);
28     for(int i=1;i<=n;i++){
29         hash1[i]=hash1[i-1]*base+(str[i]-a+1);
30         hash2[i]=hash2[i-1]*base+(m[str[i]-a+1]);
31     }
32     int ans=n;
33     for(int i=n;i<2*n;i++){
34         if(i&1) continue;
35         int len=i/2;
36         ull c1=get(1,n-len,hash2);
37         ull c2=get(len+1,n,hash1);
38         if(c1==c2){
39             ans=len;
40             break;
41         }
42     }
43     for(int i=1;i<=ans;i++) printf("%c",str[i]);
44     for(int i=1;i<=ans;i++) printf("%c",m[str[i]-a+1]-1+a);
45     printf("\n");
46 }
47 
48 int main(){
49     init();
50     int t;
51     scanf("%d",&t);
52     while(t--){
53         scanf("%s",s+1);
54         scanf("%s",str+1);
55         solve();
56     }
57     return 0;
58 }
View Code

 

例题4:HDU 1800

题解:

暴力

技术分享图片
 1 #include <map>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <iostream>
 5 #include <algorithm>
 6 using namespace std;
 7 
 8 map <int,int> m;
 9 
10 int fun(char s[]){
11     int len=strlen(s);
12     int ans=0;
13     for(int i=0;i<len;i++){
14         ans=10*ans+(s[i]-0);
15     }
16     return ans;
17 }
18 char s[123];
19 
20 int main(){
21     int n;
22     while(scanf("%d",&n)!=EOF){
23         m.clear();
24         int res=0;
25         for(int i=1;i<=n;i++){
26             scanf("%s",s);
27             int num=fun(s);
28             m[num]++;
29             res=max(res,m[num]);
30         }
31         printf("%d\n",res);
32     }
33     return 0;
34 }
View Code

hash

技术分享图片
 1 #include <map>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <iostream>
 5 #include <algorithm>
 6 using namespace std;
 7 
 8 typedef unsigned long long ull;
 9 ull base=163;
10 
11 ull gethash(char s[]){
12     int len=strlen(s);
13     ull res=0;
14     int i=0;
15     for(;i<len;i++)
16     if(s[i]!=0) break;
17     for(;i<len;i++) res=res*base+(ull)s[i];
18     return res;
19 }
20 
21 char s[100];
22 map <ull,int> m;
23 
24 int main(){
25     int n;
26     while(scanf("%d",&n)!=EOF){
27         m.clear();
28         int ans=0;
29         for(int i=1;i<=n;i++){
30             scanf("%s",s);
31             ull cnt=gethash(s);
32             m[cnt]++;
33             ans=max(ans,m[cnt]);
34         }
35         printf("%d\n",ans);
36     }
37     return 0;
38 }
View Code

 

Hash

标签:tin   例题   tar   nbsp   2-2   stc   字符串匹配   .com   char   

原文地址:https://www.cnblogs.com/ehanla/p/9142152.html

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