Guest User

Untitled

a guest
Apr 30th, 2020
612
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.25 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define int long long
  5.  
  6. const int MOD = (1LL<<32);
  7. const int N = 1e4+10;
  8.  
  9. int pw[N];
  10. int first[N];
  11. bool can[N];
  12.  
  13. signed main()
  14. {
  15.     int n;
  16.     string t,s;
  17.    
  18.    cin >> n >> t >> s;
  19.  
  20.    int tot = t.length();
  21.    
  22.    pw[0] = 1;
  23.    for(int i=1;i<=n;i++) pw[i] = (pw[i-1]*tot)%MOD;
  24.  
  25.    int sz = s.length();  
  26.    
  27.    //can[i] = 1 if first i characters of and last i characters of S are same.
  28.    for(int i=1;i<=sz;i++)
  29.    {
  30.        can[i] = 1;  
  31.        for(int j=0;j<i;j++) can[i]&=(s[j] == s[sz-i+j]);        
  32.    }
  33.    
  34.    //first[i] = number of strings of length i such that S ends at i for the first time
  35.    //1 - indexed
  36.    for(int i=1;i<sz;i++) first[i] = 0;
  37.    
  38.    for(int i=sz;i<=n;i++)
  39.    {
  40.        first[i] = pw[i-sz];
  41.        
  42.        for(int j=sz;j<=i-sz;j++)
  43.            first[i] = (first[i] - (first[j]*pw[i-sz-j])%MOD + MOD)%MOD;
  44.        
  45.        for(int j=max(sz,i-sz+1);j<i;j++)
  46.            if(can[j-i+sz])
  47.            {
  48.                  first[i] = (first[i] - first[j] + MOD)%MOD;
  49.            }
  50.    }
  51.    
  52.    int end = 0;
  53.    
  54.    for(int i=1;i<=n;i++) end = (end + (first[i]*pw[n-i])%MOD)%MOD;
  55.    
  56.    int res = (pw[n] - end + MOD)%MOD;
  57.    
  58.    cout << res << '\n';  
  59. }
Add Comment
Please, Sign In to add comment