아직 만들고 있는 페이지입니다.

이 페이지는 아직 만드는 중입니다. 보이는 내용은 바뀔 수 있습니다.

LCSLCSLCS

시간 제한10초메모리 제한1024 MB

요약
문자열 A를 n번, B를 m번 이어 붙인 두 문자열의 최장 공통 부분 수열 길이를 구한다. n과 m은 10^15까지 커질 수 있다.
난이도

보통10점 중 7점

유형
동적 계획법, 문자열, 그리디, 수학
정답자
아직 제출이 없습니다

문제

Suppose we have two non-empty strings AA and BB (∣A∣,∣B∣≤500\lvert A \rvert, \lvert B \rvert \le 500) of capital English letters, and two integers nn and mm such that 1≤n,m≤10151 \le n, m \le 10^{15}.

Let string AnA^n be a concatenation of nn copies of string AA. Let string BmB^m be a concatenation of mm copies of string BB. Your task is to find the longest common subsequence of AnA^n and BmB^m.

입력

On the first line, there are two integers nn and mm (1≤n,m≤10151 \le n, m \le 10^{15}).

On the second line, there is a non-empty string AA with length at most 500500.

On the third line, there is a non-empty string BB with length at most 500500.

Both strings consist of capital English letters.

출력

Output one integer: the length of the longest common subsequence of AnA^n and BmB^m.

예제2

  1. 예제 1

    입력
    10 10
    AB
    BA
    
    예상 출력
    19
    
  2. 예제 2

    입력
    100000000 100000000
    A
    AB
    
    예상 출력
    100000000