fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. const long long MOD = 1e9 +7;
  4. const int maxn = 1e6 +5;
  5. bool u[maxn];
  6.  
  7. long long fun(string t){
  8.  
  9. long long ans = 0;
  10.  
  11. map <char, int> m;
  12.  
  13. for(int i=0; i<t.size(); i++){
  14. m[t[i]]++;
  15. }
  16.  
  17. for(auto x: m){
  18. if(x.second==1){
  19. ans++;
  20. }
  21. }
  22.  
  23. return ans%MOD;
  24. }
  25.  
  26. long long f(string s){
  27. long long ans = s.size();
  28.  
  29. string t;
  30.  
  31. for(int i=0; i<s.size(); i++){
  32. for(int j=i+1; j<s.size(); j++){
  33. t = s.substr(i, (j-i)+1);
  34. //cout<<t<<" "<<fun(t)<<'\n';
  35. ans += fun(t);
  36. }
  37. }
  38.  
  39. return ans%MOD;
  40. }
  41.  
  42. int main(){
  43. ios_base::sync_with_stdio(0);
  44. cin.tie(0);
  45. cout.tie(0);
  46.  
  47. string s;
  48. cin>>s;
  49.  
  50. int n = s.size();
  51.  
  52. set <char> st;
  53.  
  54. char c1, c2;
  55.  
  56. bool c = true;
  57. bool T1 = true;
  58.  
  59. int id1 = 0;
  60. int id2 = 0;
  61.  
  62. map <char, int> m;
  63. int cnt = 0;
  64.  
  65. string T;
  66.  
  67. for(int i=0; i<s.size(); i++){
  68. st.insert(s[i]);
  69.  
  70. if(s[i]!=s[i+1] && i!=s.size()-1){
  71. c1 = s[i];
  72. c2 = s[i+1];
  73.  
  74. id1 = i;
  75. id2 = i+1;
  76.  
  77. cnt++;
  78. //cout<<s[i]<<" "<<s[i+1]<<'\n';
  79. }
  80.  
  81. m[s[i]]++;
  82.  
  83. if(u[s[i]]){
  84. T1 = false;
  85. }
  86. else if(T1){
  87. T+=s[i];
  88. }
  89.  
  90. u[s[i]] = 1;
  91.  
  92. }
  93.  
  94. //cout<<T<<"\n";
  95. //cout<<cnt<<"\n";
  96. if(m.size()==T.size()){
  97. int VAL = T.size();
  98.  
  99. cout<<(VAL * (n - VAL +1))*(VAL);
  100. //cout<<"ok";
  101. return 0;
  102. }
  103.  
  104. if(st.size()==1){
  105. cout<<s.size();
  106. return 0;
  107. }
  108.  
  109. if(st.size()==2 && cnt==1){
  110. cout<<s.size() * 2;
  111. return 0;
  112. }
  113.  
  114. cout<<f(s);
  115. }
Success #stdin #stdout 0.01s 5440KB
stdin
aba
stdout
8