B. Flip the Bits
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
There is a binary string aa of length nn. In one operation, you can select any prefix of aa with an equal number of 00 and 11 symbols. Then all symbols in the prefix are inverted: each 00 becomes 11 and each 11 becomes 00.
For example, suppose a=0111010000a=0111010000.
Can you transform the string aa into the string bb using some finite number of operations (possibly, none)?
Input
The first line contains a single integer tt (1≤t≤1041≤t≤104) — the number of test cases.
The first line of each test case contains a single integer nn (1≤n≤3⋅1051≤n≤3⋅105) — the length of the strings aa and bb.
The following two lines contain strings aa and bb of length nn, consisting of symbols 00 and 11.
The sum of nn across all test cases does not exceed 3⋅1053⋅105.
Output
For each test case, output "YES" if it is possible to transform aa into bb, or "NO" if it is impossible. You can print each letter in any case (upper or lower).
Example
input
Copy
5 10 0111010000 0100101100 4 0000 0000 3 001 000 12 010101010101 100110011010 6 000111 110100
output
Copy
YES YES NO YES NO
Note
The first test case is shown in the statement.
In the second test case, we transform aa into bb by using zero operations.
In the third test case, there is no legal operation, so it is impossible to transform aa into bb.
In the fourth test case, here is one such transformation:
In the fifth test case, the only legal operation is to transform aa into 111000111000. From there, the only legal operation is to return to the string we started with, so we cannot transform aa into bb.
只能翻转前缀是突破口,既然从头开始翻转,那么我们就可以从最后开始匹配,这样匹配完一个再匹配下一个的时候不会产生影响,只需要记录当前翻转次数,当前位置相等的时候,翻转次数是偶数那匹配成功,如果是奇数那么还需要保证本次能够反转成偶数,也就是前缀和的二倍等于当前位置。同理两者不等的时候也是如此。很精巧的一个好题
- #include
- #include
- #include
- #include
- #include
- #include
-
- using namespace std;
- typedef long long ll;
-
- int sum[300000+10];
- char s[300000+10],t[300000+10];
-
- int main()
- {
-
- int tt;
-
- cin>>tt;
-
- while(tt--)
- {
- int len;
-
- cin>>len;
-
- scanf("%s",s+1);
-
- scanf("%s",t+1);
-
- for(int i=1; i<=len; i++)
- {
- sum[i]=sum[i-1]+(s[i]=='1');
-
-
- }
-
- int nowcnt=0;
- int flag=0;
- for(int i=len; i>=1; i--)
- {
- if(s[i]==t[i])
- {
- if(nowcnt%2==0)
- {
- continue;
- }
-
- else
- {
- if(sum[i]*2!=i)
- {
- flag=1;
- break;
- }
- else
- {
- nowcnt++;
- }
- }
- }
- else if(s[i]!=t[i])
- {
- if(nowcnt%2)
- {
- continue;
- }
- else
- {
- if(sum[i]*2!=i)
- {
- flag=1;
- break;
- }
- else
- {
- nowcnt++;
- }
- }
- }
-
- }
-
- if(flag)
- {
- cout<<"NO"<
- }
- else
- {
- cout<<"YES"<
- }
- }
- return 0;
- }
-
相关阅读:
C语言——扫雷游戏
使用Python进行自然语言处理(NLP):NLTK与Spacy的比较【第133篇—NLTK与Spacy】
基于享元模式实现连接池
小程序开发(王红元)
【燃】是时候展现真正的实力了!一文看懂2022华为开发者大赛技术亮点
我的创作纪念日——创作者2年
令人困惑的 Go time.AddDate
【Axios封装示例Vue2】
网络规划与设计实验+配置案例报告+pkt
Linux socket编程(5):三次握手和四次挥手分析和SIGPIPE信号的处理
-
原文地址:https://blog.csdn.net/jisuanji2606414/article/details/126190572