B. Diverse Substrings
B. Diverse Substrings
A non-empty digit string is diverse if the number of occurrences of each character in it doesn't exceed the number of distinct characters in it.
For example:
- string "" is diverse because appears in it time and the number of distinct characters in it is ;
- string "" is not diverse because appears in it times and the number of distinct characters in it is ;
- string "" is diverse because both and appear in it times and the number of distinct characters in it is ;
- string "" is not diverse because appears in it times and the number of distinct characters in it is .
You are given a string of length , consisting of only digits to . Find how many of its substrings are diverse.
A string is a substring of a string if can be obtained from by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end.
Note that if the same diverse string appears in multiple times, each occurrence should be counted independently. For example, there are two diverse substrings in "" both equal to "", so the answer for the string "" is .
Input
Each test contains multiple test cases. The first line contains a single integer — the number of test cases.
The first line of each test case contains a single integer — the length of the string .
The second line of each test case contains a string of length . It is guaranteed that all characters of are digits from to .
It is guaranteed that the sum of over all test cases does not exceed .
Output
For each test case print one integer — the number of diverse substrings of the given string .
Example
input
7 1 7 2 77 4 1010 5 01100 6 399996 5 23456 18 789987887987998798
output
1 2 10 12 10 15 106
Note
In the first test case, the diverse substring is "".
In the second test case, the only diverse substring is "", which appears twice, so the answer is .
In the third test case, the diverse substrings are "" ( times), "", "", "" ( times), "" ( times), "" and "".
In the fourth test case, the diverse substrings are "" ( times), "", "", "", "" ( times), "", "", "" and "".
In the fifth test case, the diverse substrings are "", "", "", "", "" ( times), "" and "".
In the sixth test case, all non-empty substrings of "" are diverse.
解题思路
这题其实很简单。比赛的时候一直想着用减法来求出答案,就是先找到所有不满足条件的子串,然后用总的子串数量减去不满足条件的子串数量就是答案,然后发现这种做法特别麻烦,需要讨论的情况很多。跳到这个坑就出不来,一直想着怎么用这种方法去做,而不去想别的方法。
其实只要发现关键的条件,最多只有个不同的字符,这意味着满足条件的子串长度不会超过,因为如果子串的长度超过,那么必然会存在某个字符的出现频率会超过,而最多只有个不同的字符。
接下来暴力枚举就好了,直接把长度不超过的子串都枚举出来,然后判断是否满足条件。
时间复杂度为,计算量是级别。
AC代码如下:
1 #include <bits/stdc++.h> 2 using namespace std; 3 4 const int N = 1e5 + 10; 5 6 char str[N]; 7 8 void solve() { 9 int n; 10 scanf("%d %s", &n, str); 11 int ret = 0; 12 for (int i = 0; i < n; i++) { // 枚举子串左端点 13 int cnt[10] = {0}, maxv = 0, s = 0; 14 for (int j = i; j < n && j < i + 100; j++) { // 枚举子串右端点,且子串的长度不超过100 15 int t = str[j] - '0'; 16 maxv = max(maxv, ++cnt[t]); 17 if (cnt[t] == 1) s++; 18 if (maxv <= s) ret++; 19 } 20 } 21 printf("%d\n", ret); 22 } 23 24 int main() { 25 int t; 26 scanf("%d", &t); 27 while (t--) { 28 solve(); 29 } 30 31 return 0; 32 }
参考资料
Codeforces Round #833 (Div. 2) Editorial:https://codeforces.com/blog/entry/108319
本文来自博客园,作者:onlyblues,转载请注明原文链接:https://www.cnblogs.com/onlyblues/p/16890462.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
· SQL Server 2025 AI相关能力初探
· 为什么 退出登录 或 修改密码 无法使 token 失效