can choose any substring of a containing exactly k characters 1 (and arbitrary number of characters 0) and reverse it. Formally, if a=a1a2…an, you can choose any integers l and r (1≤l≤r≤n) such that there are exactly k ones among characters al,al+1,…,ar, and set a to a1a2…al−1arar−1…alar+1ar+2…an.

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

Correct answer will be upvoted else downvoted.

 

you can choose any substring of a containing exactly k characters 1 (and arbitrary number of characters 0) and reverse it. Formally, if a=a1a2…an, you can choose any integers l and r (1≤l≤r≤n) such that there are exactly k ones among characters al,al+1,…,ar, and set a to a1a2…al−1arar−1…alar+1ar+2…an.

 

Find a way to make a equal to b using at most 4n reversals of the above kind, or determine that such a way doesn't exist. The number of reversals doesn't have to be minimized.

 

Input

Each test contains multiple test cases. The first line contains the number of test cases t (1≤t≤2000). Description of the test cases follows.

 

Each test case consists of three lines. The first line of each test case contains two integers n and k (1≤n≤2000; 0≤k≤n).

 

The second line contains string a of length n.

 

The third line contains string b of the same length. Both strings consist of characters 0 and 1.

 

It is guaranteed that the sum of n over all test cases does not exceed 2000.

 

Output

For each test case, if it's impossible to make a equal to b in at most 4n reversals, print a single integer −1.

 

Otherwise, print an integer m (0≤m≤4n), denoting the number of reversals in your sequence of steps, followed by m pairs of integers li,ri (1≤li≤ri≤n), denoting the boundaries of the substrings of a to be reversed, in chronological order. Each substring must contain exactly k ones at the moment of reversal.

Expert Solution
steps

Step by step

Solved in 3 steps with 1 images

Blurred answer
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education