Substring Switcheroo

면접 대비

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

요약
길이가 같은 두 문자열 A와 B가 주어질 때, 문자를 재배열해 B의 어떤 부분 문자열로 만들 수 있는 A의 가장 앞쪽 최장 부분 문자열을 찾는다.
난이도

보통10점 중 5점

유형
슬라이딩 윈도우, 해시맵, 문자열, 투 포인터
정답자
아직 제출이 없습니다

문제

You and your young daughter have been playing a game to help teach her how to read. She of course loves learning her letters, rearranging them, and asking with each rearrangement, ‘what does this spell?’ Much of the time the letters are nonsense, but sometimes they form a real word.

She is interested in forming really long words, and as such, you have been constructing very long strings of run-together words. She then takes them and adds some letters, removes others, and generally rearranges the letters so that they form something that may be completely different.

You start writing down the strings before and after your daughter has changed them. The question is: what is the largest portion of the original string that was preserved after she edited it, allowing rearrangement of the letters?

For example, if the original string you wrote was fourscoreandsevenyearsago, she might have shuffled all of the letters to make ogasraeynevesdnaerocsruof. The second string is just a rearrangement of the first, so the entire original string was in some way preserved.

However, if she removes the y and adds a z and rearranges the letters again, the string might become reedcuraanonzovresafoegss, and the longest substring of the original that is still a (rearranged) substring in the result is ears (which is rearranged to resa in her version).

Write a program that takes as input two strings: the original one you constructed AA, and your daughter’s edited version BB. Find and report the longest substring of AA that is a permutation of some substring of BB. If there are multiple substrings of AA that satisfy this criterion with the same length, output the one that appears first in AA.

입력

Input starts with a line containing an integer 1≤n≤101 \leq n\leq 10, indicating the number of test cases. Following this are 2n2n lines, each pair representing one test case. The first line of each test case is AA, the second is BB. Each string contains between 11 and 1,0001\\, 000 characters, and uses only the lowercase letters a–z. Within each test case, the two strings have the same length.

출력

Output the longest substring of AA that is a permutation of some substring of BB. If there are multiple longest matches, print the one that occurs earliest in AA. If there are none, print NONE.

예제1

  1. 예제 1

    입력
    4
    fourscoreandsevenyearsago
    ogasraeynevesdnaerocsruof
    fourscoreandsevenyearsago
    reedcuraanonyovresafoegss
    fourscoreandsevenyearsago
    reedcuraanonzovresafoegss
    abcdef
    ghijkl
    
    예상 출력
    fourscoreandsevenyearsago
    fourscoreandsevenyearsago
    ears
    NONE