CF937Div4E :分段比较
Nearly Shortest Repeating Substring
题面翻译
给你一个长度为 \(n\) 的字符串 \(s\) ,它由小写字母组成。求最短字符串 \(k\) 的长度,使得多个(可能是一个) \(k\) 可以连接在一起,形成一个长度与 \(s\) 相同的字符串,且最多只有一个不同的字符。
更正式地说,求最短字符串 \(k\) 的长度,使得 \(c = \underbrace{k + \cdots + k}_{x\rm\ \text{times}}\) 为某个正整数 \(x\) ,字符串 \(s\) 和 \(c\) 的长度相同,且 \(c_i \neq s_i\) 中最多有一个 \(i\) (即存在 \(0\) 或 \(1\) 这样的位置)。
题目描述
You are given a string $ s $ of length $ n $ consisting of lowercase Latin characters. Find the length of the shortest string $ k $ such that several (possibly one) copies of $ k $ can be concatenated together to form a string with the same length as $ s $ and, at most, one different character.
More formally, find the length of the shortest string $ k $ such that $ c = \underbrace{k + \cdots + k}_{x\rm\ \text{times}} $ for some positive integer $ x $ , strings $ s $ and $ c $ has the same length and $ c_i \neq s_i $ for at most one $ i $ (i.e. there exist $ 0 $ or $ 1 $ such positions).
输入格式
The first line contains a single integer $ t $ ( $ 1 \leq t \leq 10^3 $ ) — the number of test cases.
The first line of each test case contains a single integer $ n $ ( $ 1 \leq n \leq 2\cdot10^5 $ ) — the length of string $ s $ .
The second line of each test case contains the string $ s $ , consisting of lowercase Latin characters.
The sum of $ n $ over all test cases does not exceed $ 2\cdot10^5 $ .
输出格式
For each test case, print the length of the shortest string $ k $ satisfying the constraints in the statement.
样例 #1
样例输入 #1
5
4
abaa
4
abba
13
slavicgslavic
8
hshahaha
20
stormflamestornflame
样例输出 #1
1
4
13
2
10
提示
In the first test case, you can select $ k = \texttt{a} $ and $ k+k+k+k = \texttt{aaaa} $ , which only differs from $ s $ in the second position.
In the second test case, you cannot select $ k $ of length one or two. We can have $ k = \texttt{abba} $ , which is equal to $ s $ .
本题思路:
(有长度L|n时,长度为L的串才是s的子串)降低枚举频率,此时枚举最小子串长度L(有L * x = s)。
接下来考虑其,最多不匹配位置为1(当不匹配位置为2时直接弹出)
题解认为:不同的字母也可能出现在前缀中(例如, 𝚑𝚜𝚑𝚊𝚑𝚊和 𝑙=2),因此我们也要对长度为的后缀进行同样的检查。如果其中之一为真,则输出 𝑙。
不匹配字符出现的问题(可能会出现前缀中,导致枚举前缀超过限制1这个前提)所以对应的这里也要同样枚举后缀消除影响
当时思路:
乱七八糟的思路有很多,想到计数每个字符出现次数,这样重复符合要求即最终长度,但很快被否定,再次的想法是找到最大子串即符合原理(夭折)····
看完题解后:
相应的总是想将黑盒数据取出,或者知道黑盒数据是什么,忽略了数据本身就是有价值的,引申出copy模版串匹配应该也是可以的,但相较于原串比较更麻烦
浙公网安备 33010602011771号