Advertisement
mickypinata

TAChi-T007: Anagram Substring

Dec 5th, 2021
697
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.75 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. typedef long long lli;
  5.  
  6. const int N = 100 + 5;
  7. const int B = 1e9 + 7;
  8.  
  9. lli enc[N];
  10. char str[N];
  11.  
  12. int main(){
  13.  
  14.     scanf(" %s", str + 1);
  15.     int len = strlen(str + 1);
  16.     enc[0] = 1;
  17.     for(int i = 1; i < 26; ++i){
  18.         enc[i] = enc[i - 1] * B;
  19.     }
  20.     lli ans = 0;
  21.     for(int i = 1; i <= len; ++i){
  22.         map<lli, int> mp;
  23.         lli hsh = 0;
  24.         for(int j = 1; j <= i; ++j){
  25.             hsh += enc[str[j] - 'a'];
  26.         }
  27.         ++mp[hsh];
  28.         for(int j = i + 1; j <= len; ++j){
  29.             hsh -= enc[str[j - i] - 'a'];
  30.             hsh += enc[str[j] - 'a'];
  31.             ans += mp[hsh];
  32.             ++mp[hsh];
  33.         }
  34.     }
  35.     cout << ans;
  36.  
  37.     return 0;
  38. }
  39.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement