#P1950E. Nearly Shortest Repeating Substring

    暂无评定 ID: 9141 远端评测题 2000ms 256MiB 尝试: 13 已通过: 4 难度: 9 上传者: 标签>brute forceimplementationnumber theorystrings

Nearly Shortest Repeating Substring

题目描述

给你一个长度为 nn 的字符串 ss ,它由小写字母组成。求最短字符串 kk 的长度,使得多个(可能是一个) kk 可以连接在一起,形成一个长度与 ss 相同的字符串,且最多只有一个不同的字符。

更正式地说,求最短字符串 kk 的长度,使得 $c = \underbrace{k + \cdots + k}_{x\rm\ \text{times}}$ 为某个正整数 xx ,字符串 sscc 的长度相同,且 cisic_i \neq s_i 中最多有一个 ii (即存在 0011 这样的位置)。

输入格式

The first line contains a single integer t t ( 1t103 1 \leq t \leq 10^3 ) — the number of test cases.

The first line of each test case contains a single integer n n ( 1n2105 1 \leq n \leq 2\cdot10^5 ) — the length of string s s .

The second line of each test case contains the string s s , consisting of lowercase Latin characters.

The sum of n n over all test cases does not exceed 2105 2\cdot10^5 .

样例 #1

样例输入 #1

5
4
abaa
4
abba
13
slavicgslavic
8
hshahaha
20
stormflamestornflame

样例输出 #1

1
4
13
2
10