fork download
  1. #include<iostream>
  2. #include<vector>
  3. using namespace std;
  4. int main()
  5. {
  6. int t;
  7. cin>>t;
  8. while(t--){
  9. long long int n;
  10. cin>>n;
  11. string s;
  12. cin>>s;
  13. char x;
  14. cin>>x;
  15. vector<long long int> v;
  16. for(int i=0;i<n;i++){
  17. if(s[i]==x)v.push_back(i);//pushing the positions of character ch in vector v
  18. }
  19. v.push_back(n);//assuming character x at position n
  20. long long int pre=-1;// assuming character x at position -1
  21. long long int tot=0;
  22. for(long long int j:v){
  23. long long int diff=j-pre-1;//calculating characters between current and previous positions of x
  24. pre=j;//updating previous position of x
  25. tot=tot+(diff+1)*(diff)/2;//number of substrings without any x
  26. }
  27. long long int ans=n*(n+1)/2-tot;//answer
  28. cout<<ans<<"\n";
  29. }
  30. }
Runtime error #stdin #stdout 0s 15240KB
stdin
Standard input is empty
stdout
Standard output is empty