fork download
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. typedef long long ll;
  6. typedef pair<int, int> ii;
  7.  
  8. const int INF = 1e9;
  9. const ll LINF = 1e18;
  10.  
  11. const int MOD = 1e9 + 7;
  12. const int N = 5e5 + 5;
  13.  
  14. void add(int& a, int b) {
  15. a += b;
  16. if (a >= MOD) a -= MOD;
  17. }
  18.  
  19. int n;
  20. string s;
  21.  
  22. int nxt[N][26]; // nxt[i][c] = Vị trí i' gần nhất > i sao cho s[i'] = c
  23.  
  24. void precompute() {
  25. for (int c = 0; c <= 25; c++) nxt[n][c] = n + 1;
  26.  
  27. for (int i = n - 1; i >= 0; i--) {
  28. for (int c = 0; c <= 25; c++) nxt[i][c] = nxt[i + 1][c];
  29. nxt[i][s[i + 1] - 'a'] = i + 1;
  30. }
  31. }
  32.  
  33. int dp[N]; // dp[i] = Số xâu con phân biệt kết thúc tại vị trí i
  34.  
  35. int main() {
  36. ios::sync_with_stdio(false);
  37. cin.tie(nullptr);
  38. cin >> s;
  39. n = s.size();
  40. s = ' ' + s;
  41.  
  42. precompute();
  43.  
  44. dp[0] = 1;
  45. for (int i = 0; i < n; i++) {
  46. for (int c = 0; c <= 25; c++) {
  47. add(dp[nxt[i][c]], dp[i]);
  48. }
  49. }
  50.  
  51. int ans = 0;
  52. for (int i = 1; i <= n; i++) add(ans, dp[i]);
  53.  
  54. cout << ans << '\n';
  55. }
Success #stdin #stdout 0.01s 5600KB
stdin
abc
stdout
7