-
Notifications
You must be signed in to change notification settings - Fork 0
/
字符串哈希
31 lines (30 loc) · 640 Bytes
/
字符串哈希
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
typedef unsigned long long ull;
ull base=131;
ull a[10010];
int n,ans=1;
ull hashs(string s) {
int len=s.length();
ull ans=0;
for (int i=0;i<len;i++)
ans=ans*base+(ull)s[i];
return ans & 0x7fffffff;
}
int main() {
string s,t; cin>>s>>t;
int sum = hashs(t);
int lens = s.length();
int lent = t.length();
for(int i=0;i<=lens-lent;i++) {
string tmp = s.substr(i,lent);
int tmpsum = hashs(tmp);
if(tmpsum == sum) {
cout<<i+1<<endl;
}
}
return 0;
}