atcoder#ARC132D. [ARC132D] Between Two Binary Strings

[ARC132D] Between Two Binary Strings

Score : 700700 points

Problem Statement

Let us define the beauty of the string as the number of positions where two adjacent characters are the same. For example, the beauty of 00011 is 33, and the beauty of 10101 is 00.

Let Sn,mS_{n,m} be the set of all strings of length n+mn+m formed of nn 0s and mm 1s.

For s1,s2Sn,ms_1,s_2 \in S_{n,m}, let us define the distance of s1s_1 and s2s_2, d(s1,s2)d(s_1,s_2), as the minimum number of swaps needed when rearranging the string s1s_1 to s2s_2 by swaps of two adjacent characters.

Additionally, for s1,s2,s3Sn,ms_1,s_2,s_3\in S_{n,m}, s2s_2 is said to be between s1s_1 and s3s_3 when d(s1,s3)=d(s1,s2)+d(s2,s3)d(s_1,s_3)=d(s_1,s_2)+d(s_2,s_3).

Given s,tSn,ms,t\in S_{n,m}, print the greatest beauty of a string between ss and tt.

Constraints

  • 2n+m3×1052 \le n + m\le 3\times 10^5
  • 0n,m0 \le n,m
  • Each of ss and tt is a string of length n+mn+m formed of nn 0s and mm 1s.

Input

Input is given from Standard Input in the following format:

nn mm

ss

tt

Output

Print the greatest beauty of a string between ss and tt.

2 3
10110
01101
2

Between 10110 and 01101, whose distance is 22, we have the following strings: 10110, 01110, 01101, 10101. Their beauties are 11, 22, 11, 00, respectively, so the answer is 22.

4 2
000011
110000
4

Between 000011 and 110000, whose distance is 88, the strings with the greatest beauty are 000011 and 110000, so the answer is 44.

12 26
01110111101110111101001101111010110110
10011110111011011001111011111101001110
22