in c++ solve this task J. The Phantom Menace time limit per test4 с memory limit per…
- User
in c++ solve this task J. The Phantom Menace time limit per test4 с memory limit per test1024 МБ Putata has brought his newest string problem to this contest. You are given two string sequences A,B , each of the sequences contains exactly n strings, and all strings have a length of m . You are asked to reorder the strings so that concatenation of the strings in the two sequences are cyclic isomorphic after concatenation.
Formally, you should choose two permutations p,q of 1,2,…,n , so that Ap1+Ap2+⋯+Apn and Bq1+Bq2+⋯+Bqn are cyclic isomorphic. String R=S+T satisfy that for i≤|S|,Ri=Si , otherwise Ri=Ti−|S| . Two strings S,T are said to be cyclic isomorphic if and only if there exists an integer d , such that Si=T((i+d)mod|S|)+1 for all 1≤i≤|S| , and |S|=|T| .
Please help Budada to find p and q , or report that there is no such p,q .
Input The first line contains one integer t (1≤t≤106 ), denoting the number of test cases.
For each test case, the first line contains two integers n,m (1≤n,m≤106,1≤n⋅m≤106 ).
Then n line follows, the i -th of which contains one string Ai (|Ai|=m) .
Then n line follows, the i -th of which contains one string Bi (|Bi|=m) .
It is guaranteed that all input strings only contain lowercase English letters.
It is also guaranteed that the sum of n⋅m over all test cases does not exceed 106 .
Output For each test case, if permutation p and q exists, output them in two lines, and the elements in one permutation are seperated by spaces. Otherwise output −1 in one line.
Example InputСкопировать 2 3 3 abc ghi def bcd efg hia 1 3 abc def OutputСкопировать 1 3 2 1 2 3 -1
1 using this solution (speak with me in English) 给你两个串集合,都有 n 个长度为 m 的串。问是否存在方案讲两个集 合重新排列拼接,使得他们循环同构。 浙江大学 10.23.2023 12 / 50 J. The Phantom Menace Solution 先枚举循环同构的偏移量 x,那么就是要求每个第一个集合中的串长度 为 x 的后缀和第二个串中长度为 x 的前缀匹配,第二个集合中长度为 n − x 的后缀和第一个集合中串长度为 n − x 的前缀匹配。 将对应长度的前后缀看成点,字符串看成边,那么就是要求解欧拉回路。 时间复杂度 Θ(nm
- Assistant