洛谷 B4240:[海淀区小学组 2025] 最短字符串 ← unordered_set + unordered_map

【题目来源】
https://www.luogu.com.cn/problem/B4240

https://www.zkcsp.cn/problem.php?id=1755

【题目描述】
给定一个仅由大小写字母构成的长度为 n 的字符串 S,求字符串 S 的一个子串 T,使得字符串 T 中包含字符串 S 中所有种类的字母(区分大小写),输出子串 T 的最小长度。例如,如果 S=aaBCCe,则 S 中包含的不同种类的字母有 a,
BC,e,要子串中包含全部的这四类字母,则子串的索引区间为 [2,6],即 S 中的第 2 个字符到第 6 个字符,即字符串 aBCCe

【输入格式】
第一行仅有一个整数 n,第二行包含一个符合题目要求的长度为 n 的字符串 S。

【输出格式】
仅有一个不超过 n 的正整数,表示符合题目要求的子串的最小长度。

【输入样例1】
7
bcAAcbc

【输出样例1】
3

【输入样例2】
6
aaBCCe

【输出样例2】
5

【说明/提示】
对于所有数据,字符串的长度不超过
10^5,且只包含大小写字母。

【算法分析】
● 利用 
unordered_set(s.begin(), s.end()).size() 求解字符串 s 中不同字符的个数

#include 
using namespace std;

int main() {
    string s;
    cin>>s;
    auto t=unordered_set(s.begin(), s.end());
    cout<


【算法代码】

#include 
using namespace std;

int minStr(string s) {
    unordered_map freq;
    int tot=unordered_set(s.begin(), s.end()).size();
    int le=0, cnt=0, min_len=INT_MAX;

    for(int ri=0; ri>n>>s;
    cout<




【参考文献】
https://www.luogu.com.cn/problem/solution/B4240
 

你可能感兴趣的:(信息学竞赛,#,字符串与高精度,数据结构,字符串)