#GL0000313. Hai cách viết tắt (Two Abbreviations)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Hai cách viết tắt (Two Abbreviations)
Two Abbreviations
Source: AtCoder
Version: Phuoc Hung OJ Extended
Problem Statement
You are given two lowercase English strings and of lengths and . Construct a string of length , where is divisible by both and .
Taking characters of at positions in this order must produce .
Similarly, the characters at positions must produce .
Find the minimum possible length of a string satisfying both conditions, or print -1 if no such string exists. Unconstrained positions may contain any lowercase letter.
Input
- The first line contains two integers .
- The second line contains string of length .
- The third line contains string of length .
Output
Print the smallest valid length , or -1 if no such string exists.
Subtasks
- Subtask 0 (20 points): ; , ; every character of is a lowercase English letter.
- Subtask 1 (30 points): ; , ; every character of is a lowercase English letter.
- Subtask 2 (50 points): ; , ; every character of is a lowercase English letter.
Examples
Example 1
Input
3 2
acp
ae
Output
6
Explanation
For , positions are a, c, p, while positions are a, e. The string accept satisfies both requirements; is also the least common positive multiple of and .
Example 2
Input
6 3
abcdef
abc
Output
-1
Explanation
At the least common multiple length , requires position to be c, but (selected at positions ) requires position to be b. These requirements conflict at every larger common multiple as well.
Example 3
Input
15 9
dnsusrayukuaiia
dujrunuma
Output
45
Explanation
The least common multiple of and is . The two selections overlap at positions, and the required characters match at each overlap, so is valid.