PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#BS0000418. Dãy con trong xâu lặp (SSttrriinngg in StringString)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

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 XX, define f(X,k)f(X,k) by concatenating kk copies of XX. Define g(X,k)g(X,k) by expanding each letter into kk consecutive copies. For instance, f(abc,2)=abcabcf(\texttt{abc},2)=\texttt{abcabc} and g(abc,3)=aaabbbcccg(\texttt{abc},3)=\texttt{aaabbbccc}. Both are empty at k=0k=0. Given positive integer NN and words S,TS,T, maximize nonnegative kk such that g(T,k)g(T,k) can be obtained from f(S,N)f(S,N) by deleting letters without reordering.

Input

The three lines contain integer NN, string SS, and string TT, respectively.

Output

Print the largest feasible nonnegative integer kk.

Subtasks

  • Subtask 1 (20 points): 1≤N≤101\le N\le 10; 1≤∣S∣,∣T∣≤501\le |S|,|T|\le 50; both strings use lowercase English letters.
  • Subtask 2 (30 points): 1≤N≤1031\le N\le 10^3; 1≤∣S∣,∣T∣≤1031\le |S|,|T|\le 10^3; both strings use lowercase English letters.
  • Subtask 3 (50 points): 1≤N≤10121\le N\le 10^{12}; 1≤∣S∣,∣T∣≤1051\le |S|,|T|\le 10^5; both strings use lowercase English letters.

Examples

Input 1

3
abc
ab

Output 1

2

Explanation 1

In abcabcabc, positions 1,4,5,81,4,5,8 form aabb. For k=3k=3, the three a letters require positions 1,4,71,4,7, leaving only one b. Thus 22.

Input 2

3
abc
arc

Output 2

0

Explanation 2

The source has no r, so every positive kk is impossible. Only the empty target at k=0k=0 works.

Input 3

1000000000000
kzazkakxkk
azakxk

Output 3

344827586207

Explanation 3

There are 101210^{12} 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 1000000000000010000000000000. At k+1k+1 the earliest final position is 10000000000021>1000000000000010000000000021>10000000000000, so the stated answer is maximal.