#P3989. [SHOI2013] 阶乘字符串

[SHOI2013] 阶乘字符串

Description

Given a string SS composed of the first nn lowercase letters. The string SS is a factorial string if and only if every permutation of the first nn lowercase letters (in total n!n! permutations) appears as a subsequence (not necessarily contiguous).

From this definition, a simple enumeration can be used to verify it, but it is far too slow. Please design an algorithm to determine within 11 second whether the given string is a factorial string.

Input Format

The first line contains an integer TT, indicating that there are TT test cases in this file.

Then TT blocks follow, each with 22 lines:

  • Line 11: a positive integer nn, indicating that SS is composed of the first nn lowercase letters.
  • Line 22: a string ss.

Output Format

For each test case, output one line: YES or NO, indicating whether the corresponding string SS is a factorial string.

2
2
bbaa
2
aba
NO
YES

Hint

In the first test case, the string ab does not appear as a subsequence.

Translated by ChatGPT 5