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

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

Common Anagrams

면접 대비

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

요약
A의 부분 문자열 중 B의 같은 길이 부분 문자열과 문자 구성이 같은 것의 개수를 센다.
난이도

보통10점 중 4점

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

문제

Ayla has two strings A and B, each of length L, and each of which is made of uppercase English alphabet letters. She would like to know how many different substrings of A appear as anagrammatic substrings of B. More formally, she wants the number of different ordered tuples (i, j), with 0 ≤ i ≤ j < L, such that the i-th through j-th characters of A (inclusive) are the same multiset of characters as at least one contiguous substring of length (j - i + 1) in B.

입력

The first line of the input gives the number of test cases, T. T test cases follow. Each test case starts with one line, containing L: the length of the string. The next two lines contain one string of L characters each: these are strings A and B, in that order.

출력

For each test case, output one line containing Case #x: y, where x is the test case number (starting from 1) and y is the answer Ayla wants, as described above.

제한

  • 1 ≤ T ≤ 100.
  • 1 ≤ L ≤ 50.

힌트

In Sample Case #1, L = 3, A = ABB, and B = BAB There are 6 substrings of A:

  • A. The substring A in B is (trivially) an anagram.
  • B. The substring B in B is (trivially) an anagram.
  • B. The substring B in B is (trivially) an anagram.
  • AB. The substring AB in B is (trivially) an anagram.
  • BB. There is no corresponding anagrammatic substring in B.
  • ABB. The substring BAB in B is an anagram.

In total, there are 5 substrings with a corresponding anagrammatic substring in B, so the answer is 5.

In Sample Case #2, note that it is the same as Sample Case #1, except that A and B are swapped. This changes the answer to 6!

In Sample Case #3, note that the substring CAT in A has the corresponding substring TAC in B which is an anagram. This still counts, even though the strings are at different indices in their respective strings.

In Sample Case #4, note that although the substring SUB in A has several corresponding substrings in B which are anagrams, it only counts once.

In Sample Case #5, note that every substring of A has a corresponding anagrammatic substring in B, so the answer is 10.

예제1

  1. 예제 1

    입력
    6
    3
    ABB
    BAB
    3
    BAB
    ABB
    6
    CATYYY
    XXXTAC
    9
    SUBXXXXXX
    SUBBUSUSB
    4
    AAAA
    AAAA
    19
    PLEASEHELPIMTRAPPED
    INAKICKSTARTFACTORY
    
    예상 출력
    Case #1: 5
    Case #2: 6
    Case #3: 6
    Case #4: 6
    Case #5: 10
    Case #6: 9