Advertisement
Dmaxiya

好串的数目 参考代码

Apr 12th, 2025
268
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.78 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. typedef long long LL;
  5. const LL MOD = 1000000007;
  6. const int maxn = 100000 + 100;
  7. int n, nxt, idx;
  8. LL ans;
  9. char str[maxn];
  10. LL len[maxn];
  11.  
  12. int findNxt(int idx) {
  13.     for (int i = idx + 1; i < n; ++i) {
  14.         if (str[i] != str[i - 1] && str[i] != str[i - 1] + 1) {
  15.             return i;
  16.         }
  17.     }
  18.     return n;
  19. }
  20.  
  21. int main() {
  22. #ifdef ExRoc
  23.     freopen("test.txt", "r", stdin);
  24. #endif // ExRoc
  25.  
  26.     cin >> str;
  27.     n = strlen(str);
  28.     for (int i = 0; i < n; i = nxt) {
  29.         nxt = findNxt(i);
  30.         len[idx] = nxt - i;
  31.         ans += len[idx] * (len[idx] + 1) / 2;
  32.         ++idx;
  33.     }
  34.     for (int i = 1; i < idx; ++i) {
  35.         ans += len[i] * len[i - 1];
  36.     }
  37.     cout << ans << endl;
  38.  
  39.     return 0;
  40. }
  41.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement