fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define int long long int
  4. #define double long double
  5. #define print(a) for(auto x : a) cout << x << " "; cout << endl
  6.  
  7.  
  8. const int M = 1000000007;
  9. const int N = 3e5+9;
  10. const int INF = 2e9+1;
  11. const int LINF = 2000000000000000001;
  12.  
  13. inline int power(int a, int b, int mod=M) {
  14. int x = 1;
  15. a %= mod;
  16. while (b) {
  17. if (b & 1) x = (x * a) % mod;
  18. a = (a * a) % mod;
  19. b >>= 1;
  20. }
  21. return x;
  22. }
  23.  
  24.  
  25. //_ ***************************** START Below *******************************
  26.  
  27. // 1
  28. // 11010101
  29.  
  30.  
  31. string a;
  32.  
  33. int consistency(int n){
  34.  
  35. vector<int> s01(n);
  36. vector<int> s10(n);
  37.  
  38. for(int i=n-2; i>=0; i--){
  39. s01[i] += s01[i+1];
  40. s10[i] += s10[i+1];
  41.  
  42. if(a[i] == '0' && a[i+1] == '1'){
  43. s01[i]++;
  44. }
  45. else if(a[i] == '1' && a[i+1] == '0'){
  46. s10[i]++;
  47. }
  48. }
  49.  
  50. int p01 = 0;
  51. int p10 = 0;
  52.  
  53. int ans = 0;
  54.  
  55. for(int i=0; i<n; i++){
  56. if(i-1>=0 && a[i-1] == '0' && a[i] == '1') p01++;
  57. else if(i-1>=0 && a[i-1] == '1' && a[i] == '0') p10++;
  58.  
  59. int c01 = p01 + s01[i];
  60. int c10 = p10 + s10[i];
  61.  
  62. if(a[i] == '0'){
  63. if(i-1>=0 && a[i-1] == '1'){
  64. c10--;
  65. }
  66. else if(i-1>=0 && a[i-1] == '0'){
  67. c01++;
  68. }
  69.  
  70. if(i+1<n && a[i+1] == '0'){
  71. c10++;
  72. }
  73. else if(i+1<n && a[i+1] == '1'){
  74. c01--;
  75. }
  76.  
  77. }
  78. else if(a[i] == '1'){
  79. if(i-1>=0 && a[i-1] == '1'){
  80. c10++;
  81. }
  82. else if(i-1>=0 && a[i-1] == '0'){
  83. c01--;
  84. }
  85.  
  86. if(i+1<n && a[i+1] == '0'){
  87. c10--;
  88. }
  89. else if(i+1<n && a[i+1] == '1'){
  90. c01++;
  91. }
  92. }
  93.  
  94. if(c01 == c10) ans++;
  95.  
  96. }
  97.  
  98. return ans;
  99. }
  100.  
  101.  
  102.  
  103.  
  104.  
  105.  
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114.  
  115. int practice(int n){
  116.  
  117.  
  118. return 0;
  119. }
  120.  
  121.  
  122.  
  123.  
  124.  
  125. void solve() {
  126.  
  127. int n;
  128. cin >> a;
  129. n = a.size();
  130.  
  131. cout << consistency(n) << endl;
  132.  
  133.  
  134. }
  135.  
  136.  
  137.  
  138.  
  139.  
  140. int32_t main() {
  141. ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  142.  
  143. int t = 1;
  144. cin >> t;
  145. while (t--) {
  146. solve();
  147. }
  148.  
  149. return 0;
  150. }
Success #stdin #stdout 0s 5324KB
stdin
1
11010101
stdout
6