Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // #### Zeinab
- #include<bits/stdc++.h>
- using namespace std;
- #define nl "\n"
- #define fi first
- #define se second
- #define pb push_back
- #define ll long long
- #define RV return void
- #define sz(x) int(x.size())
- #define all(v) v.begin(), v.end()
- #define rall(v) v.rbegin(), v.rend()
- #define cin(v) for(auto&x:v) cin >> x;
- #define cout(v) for(auto&x:v) cout << x << " ";
- int n;
- string s;
- const int N = 1e6 + 5 , MD = 1e9+7;
- int p[2]= {31 , 37} , mod[2] = {MD , MD+2};
- int pref[N][2] , pw[N][2] , inv[N][2];
- int mul(int a , int b , int md){
- return ( ( 1ll * a % md) * ( 1ll*b % md) ) % md;
- }
- int add(int a , int b , int md){
- return ( (a % md) + ( b % md) ) % md;
- }
- int fastPower(int a , int b , int md){
- int ret =1;
- while(b){
- if(b & 1 ) ret = mul( ret, a , md);
- a= mul(a , a , md );
- b>>=1;
- }
- return ret ;
- }
- void pre(){
- pw[0][0] = pw[0][1] = inv[0][0] = inv[0][1] =1;
- for(int j =0 ;j < 2 ; j++){
- int minv = fastPower(p[j] , mod[j]-2 , mod[j]);
- for(int i =1 ; i < N ; i++){
- pw[i][j] = mul(pw[i-1][j], p[j] , mod[j]);
- inv[i][j] = mul(inv[i-1][j] , minv , mod[j]);
- }
- }
- }
- void calc(){
- for(int j =0 ; j < 2 ; j++){
- for(int i =0 ; i < n ; i++){
- int ch = s[i] - 'a' + 1;
- pref[i][j] = mul(ch , pw[i][j] , mod[j]);
- if(i){
- pref[i][j] = add(pref[i][j] , pref[i-1][j] , mod[j]);
- }
- }
- }
- }
- int reCalc(int l , int r , int step){
- int ret = pref[r][step];
- if(l){
- ret = (ret - pref[l-1][step] + mod[step]) % mod[step];
- }
- ret = mul(ret , inv[l][step] , mod[step]);
- return ret;
- }
- pair < int , int > get_Hash(int l , int r){
- return {reCalc(l,r,0) , reCalc(l,r,1)};
- }
- void solve(){
- int k ;
- string t;
- cin >> s >> t;
- n = sz(s) , k = sz(t);
- if(k > n) RV(cout <<"Not Found");
- calc();
- int ret2[] ={0,0};
- for(int j =0 ; j < 2 ; j++){
- int val =0;
- for(int i =0 ; i < k ; i++){
- int ch = t[i] - 'a' + 1;
- int curr = mul(ch , pw[i][j] , mod[j]);
- val = add(val ,curr , mod[j]);
- }
- ret2[j] = val;
- }
- vector < int > ans;
- for(int i =0 ; i + k -1 < n ; i++){
- auto ret1 = get_Hash(i,i+k-1) ;
- if(ret1.fi == ret2[0] and ret1.se == ret2[1])
- ans.pb(i+1);
- }
- if(ans.empty()) RV(cout <<"Not Found");
- cout << sz(ans) << nl;
- for(auto& x : ans) cout << x << " ";
- }
- int main(){
- ios_base::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr);
- int t=1;
- cin >> t ;
- pre();
- for(int i=1 ; i <= t ; i++){
- // cout << "Case #"<< i <<": ";
- if(i != 1) cout << nl;
- solve();
- cout << nl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement