fork download
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. #define int int64_t
  6.  
  7. void solve() {
  8. string S;
  9. char X;
  10. int N;
  11. cin >> N;
  12. cin >> S >> X;
  13. vector<int> pos = {-1};
  14. for(int i = 0; i < N; i++) {
  15. if(S[i] == X) {
  16. pos.push_back(i);
  17. }
  18. }
  19. pos.push_back(N);
  20. int ans = N * (N + 1) / 2;
  21. for(size_t i = 1; i < pos.size(); i++) {
  22. int ln = pos[i] - pos[i - 1] - 1;
  23. ans -= ln * (ln + 1) / 2;
  24. }
  25. cout << ans << endl;
  26. }
  27.  
  28. signed main() {
  29. //freopen("input.txt", "r", stdin);
  30. ios::sync_with_stdio(0);
  31. cin.tie(0);
  32. int t;
  33. cin >> t;
  34. while(t--) {
  35. solve();
  36. }
  37. }
  38.  
Success #stdin #stdout 0s 15240KB
stdin
Standard input is empty
stdout
Standard output is empty