Josif_tepe

Untitled

Feb 4th, 2026
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.99 KB | None | 0 0
  1. #include <iostream>
  2. #include <set>
  3. #include <map>
  4. #include <vector>
  5. using namespace std;
  6. const int INF = 2e9;
  7. const int MOD = 1e9 + 7;
  8.  
  9. int main() {
  10.     ios_base::sync_with_stdio(false);
  11.    
  12.     int n;
  13.     cin >> n;
  14.    
  15.     string s;
  16.     cin >> s;
  17.    
  18.     vector<int> pos;
  19.     map<char, int> cnt;
  20.     int k = 0;
  21.     for(int i = 0; i < n; i++) {
  22.         if(cnt.find(s[i]) == cnt.end()) {
  23.             cnt[s[i]] = k;
  24.             ++k;
  25.         }
  26.         pos.push_back(cnt[s[i]]);
  27.     }
  28.    
  29.     vector<int> at(k, 0);
  30.     map<vector<int>, int> mapa;
  31.     int res = 0;
  32.     for(int i = 0; i < n; i++) {
  33.         mapa[at]++;
  34.         at[pos[i]]++;
  35.        
  36.         int min_value = INF;
  37.         for(int j = 0; j < k; j++) {
  38.             min_value = min(min_value, at[j]);
  39.         }
  40.        
  41.         for(int j = 0; j < k; j++) {
  42.             at[j] -= min_value;
  43.         }
  44.        
  45.         res += mapa[at];
  46.         res %= MOD;
  47.     }
  48.    
  49.     cout << res << endl;
  50.     return 0;
  51. }
  52.  
Advertisement
Add Comment
Please, Sign In to add comment