An ancient scroll contains two long strings of characters, which archaeologists have…
- User
An ancient scroll contains two long strings of characters, which archaeologists have named S and T. They hypothesize that a shorter, original message, let's call it W, was encoded in a peculiar way. They believe W can be formed by deleting zero or more characters from S (making W a subsequence of S), and also that W appears contiguously within T (making W a substring of T).
Your task is to determine the length of the longest possible original message W that satisfies both conditions.
Formal Definition:
Given two strings, S and T, find the length of the longest string W such that:
W is a subsequence of S.
W is a substring of T.
Input Format:
The first line contains the string S.
The second line contains the string T.
Output Format:
Print a single integer representing the length of the longest such string W.
Constraints:
1 le∣S∣,∣T∣ le5000
Both strings consist only of lowercase English letters ('a'-'z').
Example:
Input:
banana atendbana Output:
4 Explanation: The string "bana" can be formed by taking the 1st, 2nd, 3rd, and 6th characters of S="banana" (it is a subsequence of S). The string "bana" also appears contiguously inside T="atendbana" (it is a substring of T). No string of length 5 or greater satisfies both conditions. Therefore, the answer is 4.
- Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.