DreamGrid has just found two binary sequences s1,s2,…,sn and t1,t2,…,tn(si,ti∈0,1 for all 1≤i≤n) from his virtual machine! He would like to perform the operation described below exactly twice, so that si=ti holds for all 1≤i≤n after the two operations.
The operation is: Select two integers l and r (1≤l≤r≤n), change si to (1−si) for all l≤i≤r.
DreamGrid would like to know the number of ways to do so.
We use the following rules to determine whether two ways are different:
There are multiple test cases. The first line of the input contains an integer T, indicating the number of test cases. For each test case:
The first line contains an integer n (1≤n≤106), indicating the length of two binary sequences.
The second line contains a string s1s2…sn (si∈‘0′,‘1′) of length n, indicating the first binary sequence.
The third line contains a string t1t2…tn (ti∈‘0′,‘1′) of length n, indicating the second binary sequence.
It’s guaranteed that the sum of n in all test cases will not exceed 107.
For each test case, output an integer denoting the answer.
1 | 3 |
1 | 0 |
For the second sample test case, there are two valid operation pairs: (1, 1, 2, 2) and (2, 2, 1, 1).
For the third sample test case, there are six valid operation pairs: (2, 3, 5, 5), (5, 5, 2, 3), (2, 5, 4, 4), (4, 4, 2, 5), (2, 4, 4, 5) and (4, 5, 2, 4).
First we denote the longest possible unequal subsequence as A, the longest possible equal subsequence as B.
So the string is consists of A and B.
For example:
1 | 0 10 1 0 |
The array is BABA where first B is 0, first A is 10, second B is 1, second A is 0.
We can just use a for loop to find all the A.
And we use cnt
to represents the number of A.
1 | for (int i = 0; i < n; i++) { |
There multiple answers to multiple situations:
cnt = 1
There is also two conditions. Just flip A and flip A and B.
Obviously, if we flip the whole A twice, we will get A and we want A to be B. So we must detach A to two parts. First we flip the first part, then we flip the second part. We happened to reverse s twice.
But how do we break A? Assume the length of A is n.
We can break A into
(A1),(A2,A3,⋯,An)
If your s is BAB (Total length of B is at least 1 or it will become the ‘break A’ problem). The only possible way of flipping is BA,B or AB,B
Let’s take a closer look at B. Suppose the length of B is n.
s is made up with
B1,⋯,Bk,A,Bk+1,⋯Bn(1≤k≤n)
A,Bx,⋯,By,Bx,⋯,By(1≤x≤y≤n)
cnt = 1
isLength(A)−1+Length(s)−Length(A)=Length(s)−1
cnt = 2
S is consists of BA1BA2B (The length of central B must not be 0 or it will be BAB). There are 3 ways of flipping:
A1BA2,B
A1,A2
A1B,BA2
cnt > 2
S is consists of BA1BA2BA3… Since you can just flip twice, you can only change two A of the s. So it’s impossible to make the flip.
cnt = 0
S is consists of only B. BBBBBB…
So, if we flip one B twice, we will reach t. Assume the length of B (which is the length of s) is n.
If we just choose a substring of B, and the length of substring is 1. We have n - 1 ways of splitting.
If the length of substring is 2, there will be n - 2 ways of splitting.
Until: If the length of substring is n, there will be 1 way of splitting.
So the total ways of splitting B is
(n−1)+(n−2)+⋯+1=(n−1+1)×(n−1)2=n×(n−1)2
Different order of flip is regards as two ways. {A}, {B} is not equal to {B}, {A}
These are all the situations of the string s. Use If else sentence in your code.
1 |
|