Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- #include <vector>
- #include <queue>
- #include <algorithm>
- #include <string>
- #include <stack>
- #include <set>
- #include <map>
- #define pii pair <int,int>
- #define vec vector
- using namespace std;
- using ll = long long;
- using ld = long double;
- using db = double;
- void cv(vector <int> &v){
- for (auto x: v) cout<<x<<' ';
- cout<<"\n";
- }
- void cvl(vector <ll> &v){
- for (auto x: v) cout<<x<<' ';
- cout<<"\n";
- }
- void cvv(vector <vector <int> > &v){
- for (auto x: v) cv(x);
- cout<<"\n";
- }
- void cvb(vector <bool> v){
- for (bool x: v) cout<<x<<' ';
- cout<<"\n";
- }
- const ll mod= 1e9+7, k = 29;
- vector <string> str;
- vector <ll> kpow;
- vector <vector <ll> > hp; //hash-pref
- ll h(int l, int r, int id){//какая строка
- // cout<<"l r = "<<l<<' '<<r<<"\n";
- //cout<<"id = "<<id<<"\n";
- //cout<<"l r = "<<l<<' '<<r<<"\n";
- ll res = hp[id][r];
- //cout<<"a = "<<res<<"\n";
- if (l > 0){
- //cout<<"b= "<<(hp[id][l-1] * kpow[r - l + 1] % mod)<<"\n";
- res -= hp[id][l-1] * kpow[r - l + 1] % mod;
- }
- //cout<<"a - b = "<< res<<"\n";
- return (res + mod) % mod;
- }
- ll num(char x){
- return x - 'a' + 1;
- }
- int main()
- {
- /* ios::sync_with_stdio(0);
- cin.tie(0);
- cout.tie(0);*/
- int q = 2;
- str.resize(q);
- int mx_sz = -1;
- for (int i = 0;i<q;++i){
- cin>>str[i];
- int sz = str[i].size();
- mx_sz = max(mx_sz, sz);
- }
- hp.resize(q);
- kpow.assign(mx_sz, 1);
- //cout<<"str\n";
- //for (auto s: str) cout<<s<<"\n";
- //cout<<"hash\n";
- for (int i=1;i<mx_sz;++i){
- kpow[i] = kpow[i-1] * k % mod;
- }
- //cout<<"kpow\n";
- //cvl(kpow);
- for (int i = 0; i<q;++i){
- //cout<<"i = "<<i<<"\n";
- hp[i].resize(str[i].size());
- hp[i][0] = num(str[i][0]);
- for (int j = 1; j < str[i].size(); ++j){
- // cout<<j<<' ';
- hp[i][j] = ( hp[i][j-1] * k % mod + num(str[i][j]) ) % mod;
- }
- //cout<<"\n";
- }
- //cout<<"hash\n";
- //for (auto g: hp) cvl(g);
- //cout<<"COUNT\n";
- vector <int> ans;
- for (int i = 0; i <= str[0].size() - str[1].size();++i){
- //cout<<"i = "<<i<<"\n";
- //cout<<"h(S) h(T) = "<<h(i, i + str[1].size() - 1, 1)<<' '<<h(0, str[1].size()-1, 0)<<"\n";
- if (h(i, i + str[1].size() - 1, 0) == h(0, str[1].size()-1, 1) ){
- ans.push_back(i);
- }
- //cout<<"\n\n";
- }
- //cout<<"ans\n";
- cv(ans);
- }
- /*
- ababbababa
- aba
- */
Add Comment
Please, Sign In to add comment