问题 4832 --特殊子串

4832: 特殊子串★★★

时间限制: 1 Sec  内存限制: 128 MB
提交: 21  解决: 8
[提交][状态][命题人:]

题目描述

Ashish has a string ss of length nn containing only characters 'a', 'b' and 'c'.

He wants to find the length of the smallest substring, which satisfies the following conditions:

  • Length of the substring is at least 22
  • 'a' occurs strictly more times in this substring than 'b'
  • 'a' occurs strictly more times in this substring than 'c'

Ashish is busy planning his next Codeforces round. Help him solve the problem.

A string aa is a substring of a string bb if aa can be obtained from bb by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end.

Ashish有一个长度为n的仅由字符a,b,c组成的串。他想找到这个串中满足以下条件的最小子串:

1)子串长度至少为2;

2)子串中字符a出现次数比b多;

3)字符a出现次数比c多。
请你帮助Ashish解决这个问题。

输入

The first line contains a single integer tt (1≤t≤10^5)  — the number of test cases. The description of test cases follows.

The first line of each test case contains a single integer nn (2≤n≤10^6)  — the length of the string s.

The second line of each test case contains a string s consisting only of characters 'a', 'b' and 'c'.

It is guaranteed that the sum of nn over all test cases does not exceed 10^6.

第一行为t(1=<t<=100000),为测试数据组数。每组数据第一行为n(2=<n<=1000000),串s的长度。第二行为仅由字符a,b,c组成的串s。保证各组测试数据的n的总和不超过1000000.

输出

For each test case, output the length of the smallest substring which satisfies the given conditions or print -1 if there is no such substring.

对于每组测试数据,输出符合条件的最小子串的长度。如果没有这样的子串,输出-1。

样例输入
Copy
3
2
aa
5
cbabb
8
cacabccc
样例输出
Copy
2
-1
3

提示

Consider the first test case. In the substring "aa", 'a' occurs twice, while 'b' and 'c' occur zero times. Since 'a' occurs strictly more times than 'b' and 'c', the substring "aa" satisfies the condition and the answer is 22. The substring "a" also satisfies this condition, however its length is not at least 22.

In the second test case, it can be shown that in none of the substrings of "cbabb" does 'a' occur strictly more times than 'b' and 'c' each.

In the third test case, "cacabccc", the length of the smallest substring that satisfies the conditions is 3.

说明:

数据1:在子串"aa"中,a出现2次,b,c各出现0次。a出现次数多于b,c,由于要求子串长度至少为2,所以子串"a"不满足条件,只能选择子串"aa".
数据2:没有任何一个子串满足a出现次数严格多于b和c.
数据3:子串"aca" 是满足条件的长度最小的子串,长度为3.

来源

[提交][状态]