Virtual Data

5

2 votes
String, Algorithms, String Algorithms, Regular Expressions
Problem

VirtualData has just found two binary set A1,A2,,An and  B1,B2,,Bn (Ai,Bi{0, 1} for all 1in) from his virtual machine! He would like to perform the operation described below exactly twice, so that Ai=Bi holds for all 1in after the two operations.

The operation is: Select two integers l and r (1lrn), change Ai to (1Ai) for all lir. VirtualData would like to know the number of ways to do so.

We use the following rules to determine whether two ways are different:

  • Let Operation X=(a1,a2,a3,a4), where 1a1a2n, 1a3a4n, be a valid operation pair denoting that VirtualData selects integers a1 and a2 for the first operation and integers a3 and a4 for the second operation.
  • Let Operation Y=(b1,b2,b3,b4), where 1b1b2n, 1b3b4n, be another valid operation pair denoting that VirtualData selects integers b1 and b2 for the first operation and integers b3 and b4 for the second operation.
  • Operation X and Operation Y are considered different if there exists an integer k (1k4) such that .akbk.

Input format

  • The first line contains an integer n, denoting the length of binary sequence A and B.
  • The second line contains a binary string of size n, denoting the set A.
  • The third line contains a binary string of size n, denoting the set B.

Output format

Print an integer in a new line, representing the number of unique ways given operation can be performed.

Constraints

1n106

 

Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

In this case, we have the following options, (0 based indexing):

  • Select [2, 3] , set A become 00110, then we select interval [5, 5] and set A becomes 00111 . So, this is one way to make the sequences identical.
  • Select [5, 5] , set A become 01011, then we select interval [2, 3] and set A becomes 00111 . So, this is one way to make the sequences identical.
  • Select [2, 5] , set A become 00101, then we select interval [4, 4] and set A becomes 00111 . So, this is one way to make the sequences identical.
  • Select [4, 4] , set A become 01000, then we select interval [2, 5] and set A becomes 00111 . So, this is one way to make the sequences identical.
  • Select [2, 4] , set A become 00100, then we select interval [4, 5] and set A becomes 00111 . So, this is one way to make the sequences identical.
  • Select [4, 5] , set A become 01001, then we select interval [2, 4] and set A becomes 00111 . So, this is one way to make the sequences identical.

So, there are a total of 6 distinct ways to perform the operations and make both sequences identical, hence the output is 6.

Editor Image

?