#abc346f. F - SSttrriinngg in StringString

F - SSttrriinngg in StringString

Score: 525525 points

问题陈述

对于长度为 nn 的字符串 XX,让 f(X,k)f(X,k) 表示通过重复字符串 XX 来获得的字符串 kk 次,让 g(X,k)g(X,k) 表示通过重复字符串 XX 的第一个字符、第二个字符、\dots、第 nn 个字符,按此顺序来获得的字符串 kk 次。例如,如果 X=X= abc,则 f(X,2)=f(X,2)= abcabc,而 g(X,3)=g(X,3)= aaabbbccc。此外,对于任何字符串 XXf(X,0)f(X,0)g(X,0)g(X,0) 都是空字符串。

给定一个正整数 NN 和字符串 SSTT。找到最大的非负整数 kk,使得 g(T,k)g(T,k)f(S,N)f(S,N) 的(不一定是连续的)子序列。请注意,根据定义,g(T,0)g(T,0) 总是 f(S,N)f(S,N) 的子序列。

什么是子序列?一个字符串 XX 的(不一定是连续的)子序列是通过从 XX 中删除零个或多个字符,然后不改变顺序地连接剩余元素而获得的字符串。例如,acatcoder 和(空字符串)都是 atcoder 的子序列,但 ta 不是。

以上为大语言模型 kimi 翻译,仅供参考。

Problem Statement

For a string XX of length nn, let f(X,k)f(X,k) denote the string obtained by repeating kk times the string XX, and g(X,k)g(X,k) denote the string obtained by repeating kk times the first character, the second character, \dots, the nn-th character of XX, in this order. For example, if X=X= abc, then f(X,2)=f(X,2)= abcabc, and g(X,3)=g(X,3)= aaabbbccc. Also, for any string XX, both f(X,0)f(X,0) and g(X,0)g(X,0) are empty strings.

You are given a positive integer NN and strings SS and TT. Find the largest non-negative integer kk such that g(T,k)g(T,k) is a (not necessarily contiguous) subsequence of f(S,N)f(S,N). Note that g(T,0)g(T,0) is always a subsequence of f(S,N)f(S,N) by definition.

What is a subsequence? A (not necessarily contiguous) subsequence of a string XX is a string obtained by removing zero or more characters from XX and then concatenating the remaining elements without changing the order. For example, ac, atcoder, and (empty string) are all subsequences of atcoder, but ta is not.

Constraints

  • NN is an integer.
  • 1N10121\leq N\leq 10^{12}
  • SS and TT are strings consisting of lowercase English letters with lengths between 11 and 10510^5, inclusive.

Input

The input is given from Standard Input in the following format:

NN

SS

TT

Output

Print the largest non-negative integer kk such that g(T,k)g(T,k) is a (not necessarily contiguous) subsequence of f(S,N)f(S,N).

Sample Input 1

3
abc
ab

Sample Output 1

2

We have f(S,3)=f(S,3)= abcabcabc. g(T,2)=g(T,2)= aabb is a subsequence of f(S,3)f(S,3), but g(T,3)=g(T,3)= aaabbb is not, so print 22.

Sample Input 2

3
abc
arc

Sample Output 2

0

Sample Input 3

1000000000000
kzazkakxkk
azakxk

Sample Output 3

344827586207

update @ 2024/5/16 17:18:22