Advertisement
mihaimarcel21

Sdistante_eu

Mar 23rd, 2021
672
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.71 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. ifstream fin("sdistante.in");
  4. ofstream fout("sdistante.out");
  5. const int mod = 1e9 + 7;
  6. int N;
  7. long long fr[52], last[52], presum[52], Sol, S;
  8. string s;
  9.  
  10. int numar(const char &c)
  11. {
  12.     if(c >= 'a')
  13.         return c - 'a';
  14.     return c - 'A' + 26;
  15. }
  16.  
  17. int main()
  18. {
  19.     fin>>s;
  20.     N=s.size();
  21.     for(int i=0; i<52; ++i)
  22.         last[i]=N;
  23.     for(int i=N-1; i>=0; --i)
  24.     {
  25.         int ch=numar(s[i]);
  26.         presum[ch]+=(last[ch]-i)*fr[ch];
  27.         ++fr[ch];
  28.         last[ch]=i;
  29.         Sol+=(i+1)*((N-i)*(N-i-fr[ch])-S+presum[ch]);
  30.         if(Sol>= 1e17L)
  31.             Sol%=mod;
  32.         S+=N-i;
  33.     }
  34.     fout<<Sol%mod<<'\n';
  35.     return 0;
  36. }
  37.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement