fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. #define sz(s) long(s.size())
  4. #define ll long long
  5. #define F first
  6. #define S second
  7. #define pb push_back
  8. #define all(v) v.begin(), v.end()
  9. #define file(s) freopen(s".in", "r", stdin); freopen(s".out", "w", stdout)
  10. const int N=1e6+2, mod=1e9+7;
  11. char a[N];
  12. vector<vector<int> > g(28);
  13. int n;
  14. signed main() {
  15. string s;
  16. cin>>s;
  17. n=sz(s);
  18. for(int i=1; i<=n; i++)a[i]=s[i-1];
  19. for(int i=1; i<=n; i++) {
  20. g[int(a[i]-'A')].pb(i);
  21. }
  22. ll cnt=0;
  23. for(int i=1; i<=n; i++) {
  24. for(int j=0; j<=27; j++) {
  25. if(g[j].empty() || g[j].back()<i)continue;
  26. if(sz(g[j])==1) {
  27. if(g[j][0]>=i)cnt=(cnt+(n-g[j][0]+1))%mod;
  28. continue;
  29. }
  30. int l=0, r=sz(g[j])-1;
  31. while(l<=r) {
  32. int mid=(l+r)>>1;
  33. if(g[j][mid]>=i)r=mid-1;
  34. else l=mid+1;
  35. }
  36. if(l==sz(g[j])-1) {
  37. cnt=(cnt+(n-g[j][l]+1))%mod;
  38. continue;
  39. }
  40. cnt=(cnt+(g[j][l+1]-g[j][l]))%mod;
  41. }
  42. }
  43. cout << cnt%mod << '\n';
  44. }
Success #stdin #stdout 0.01s 5476KB
stdin
ABC
stdout
10