fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. const int oo = 3e5 + 3;
  5. const int inf = 1e9;
  6.  
  7. int n;
  8. string a, b;
  9.  
  10. void solve() {
  11. cin >> n;
  12. cin >> a >> b;
  13.  
  14. a = " " + a;
  15. b = " " + b;
  16. int bal[n + 3];
  17. int cnt1 = 0, cnt0 = 0;
  18. for (int i = 1; i <= n; i++) {
  19. if (a[i] == '1') {
  20. cnt1++;
  21. } else {
  22. cnt0++;
  23. }
  24. bal[i] = cnt1 - cnt0;
  25. }
  26.  
  27. int flip = 0;
  28. for (int i = n; i >= 1; i--) {
  29. a[i] ^= flip;
  30.  
  31. if (a[i] == b[i]) {
  32. continue;
  33. }
  34.  
  35. if (bal[i] != 0) {
  36. cout << "NO\n";
  37. return;
  38. }
  39. flip = 1 - flip;
  40. }
  41.  
  42. cout << "YES\n";
  43. }
  44.  
  45. int main() {
  46.  
  47. int t;
  48. cin >> t;
  49. while (t--) {
  50. solve();
  51. }
  52.  
  53. return 0;
  54. }
Success #stdin #stdout 0s 544KB
stdin
5
10
0111010000
0100101100
4
0000
0000
3
001
000
12
010101010101
100110011010
6
000111
110100
stdout
YES
YES
NO
YES
NO