#BS0000418. Dãy con trong xâu lặp (SSttrriinngg in StringString)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Dãy con trong xâu lặp (SSttrriinngg in StringString)
SSttrriinngg in StringString
Source: AtCoder
Version: Phuoc Hung OJ Extended
Problem Statement
For a word , define by concatenating copies of . Define by expanding each letter into consecutive copies. For instance, and . Both are empty at . Given positive integer and words , maximize nonnegative such that can be obtained from by deleting letters without reordering.
Input
The three lines contain integer , string , and string , respectively.
Output
Print the largest feasible nonnegative integer .
Subtasks
- Subtask 1 (20 points): ; ; both strings use lowercase English letters.
- Subtask 2 (30 points): ; ; both strings use lowercase English letters.
- Subtask 3 (50 points): ; ; both strings use lowercase English letters.
Examples
Input 1
3
abc
ab
Output 1
2
Explanation 1
In abcabcabc, positions form aabb. For , the three a letters require positions , leaving only one b. Thus .
Input 2
3
abc
arc
Output 2
0
Explanation 2
The source has no r, so every positive is impossible. Only the empty target at works.
Input 3
1000000000000
kzazkakxkk
azakxk
Output 3
344827586207
Explanation 3
There are copies of kzazkakxkk. Match the six groups a, z, a, k, x, k from left to right. The earliest group endpoints are 1724137931033, 3448275862064, 5172413793096, 5862068965509, 9310344827578, 9999999999990. The final position is within . At the earliest final position is , so the stated answer is maximal.