fork(1) download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define int long long
  4.  
  5. int32_t main() {
  6. ios::sync_with_stdio(false);
  7. cin.tie(NULL);
  8.  
  9. int t;
  10. cin >> t;
  11. while (t--) {
  12. int n;
  13. cin >> n;
  14. string s;
  15. cin >> s;
  16.  
  17. vector<int> a;
  18. for (int i = 0; i < n; i++) {
  19. if (s[i] == '*') a.push_back(i);
  20. }
  21.  
  22. int b = a.size();
  23. if (b <= 1) {
  24. cout << 0 << '\n';
  25. continue;
  26. }
  27.  
  28. int mid = b / 2;
  29. int center = a[mid];
  30.  
  31. int cost = 0;
  32. for (int i = 0; i < b; i++) {
  33. cost += abs(a[i] - (center + (i - mid)));
  34. }
  35.  
  36. cout << cost << '\n';
  37. }
  38. return 0;
  39. }
  40.  
Success #stdin #stdout 0s 5320KB
stdin
5
6
**.*..
5
*****
3
.*.
3
...
10
*.*...*.**
stdout
1
0
0
0
9