Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
17 views2 pages

Hash

Uploaded by

Thang Giang
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views2 pages

Hash

Uploaded by

Thang Giang
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

#include <bits/stdc++.

h>
#define MASK(k) (1LL << (k))
#define BIT(x, i) (((x) >> (i)) & 1)
#define __builtin_popcount __builtin_popcountll
#define __builtin_ctz __builtin_ctzll
#define For(i, a, b) for(int i = (int)(a); i <= (int)(b); ++i)
#define Fod(i, a, b) for(int i = (int)(a); i >= (int)(b); --i)
//#define int long long
#define ll long long
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL);
#define whole(a) a.begin(), a.end()
#define vi vector<int>
#define mii map<int, int>
#define pii pair<int, int>
#define pb push_back
#define fi first
#define se second
#define mirai ""
template<class X, class Y>
bool minimize(X& x, const Y& y) {
X eps = 1e-9;
if (x > y + eps) {
x = y;
return true;
}
else return false;
}
template<class X, class Y>
bool maximize(X& x, const Y& y) {
X eps = 1e-9;
if (x + eps < y) {
x = y;
return true;
}
else return false;
}
template<class T>
T Abs(const T& x) {
return (x < 0 ? -x : x);
}
const int INF = 1e9 + 7;
const ll oo = 1e18 + 7;
const int MAX = 1000005;
const int MOD = 1e9 + 7;
using namespace std;
string a, b;
ll HashA, p[MAX], f[MAX];
ll gethash(int l, int r){
return (f[r] - ((f[l - 1] * p[r - l + 1]) % MOD) + MOD) % MOD;
}
void process(){
cin >> a >> b;
int n = a.size(), m = b.size();
a = " " + a;
b = " " + b;
p[0] = 1;
For(i, 1, 1e6){
p[i] = (p[i - 1] * 31) % MOD;
}
For(i, 1, n){
HashA = (HashA * 31 + a[i]) % MOD;
}
For(i, 1, m){
f[i] = (f[i - 1] * 31 + b[i]) % MOD;
}
vi res;
For(i, 1, m - n + 1){
if(HashA == gethash(i, i + n - 1)) res.pb(i);
}
cout << res.size() << endl;
for(int i : res) cout << i << " ";
}
signed main()
{
fastio
if (fopen(mirai".inp", "r")) {
freopen(mirai".inp", "r", stdin);
freopen(mirai".out", "w", stdout);
}
int Test = 1;
while (Test--) {
process();
}
return 0;
}

You might also like