给定小写字母组成的字符串 s ,请你找出包含所有小写字母(任意顺序)的最短子串长度
第一行包含1个小写字母组成的字符串 s
输出一个正整数,代表包含 a-z(就是26个字母都出现过)的最短子串长度,如果无解输出 −1
xcprvkpcyiludxoazfwlcnfmrqvzgjbimlhqzekoaaunctetsa
43
对于40%的数据,∣s∣≤500
对于100%的数据,1≤∣s∣≤105
这道题我看出来是逛画展的双倍经验,但是检查了好几遍没有检查出和逛画展的代码有什么区别。80pts。
#include <iostream>
using namespace std;
int b[1001] = {};
int main()
{
string a;
cin >> a;
b[a[1]]++;
int minl = 1, minr = 2147483647, l = 1, r = 1, k = 1;
while (r < a.size())
{
while (k < 26 && r < a.size())
{
r++;
b[a[r]]++;
if (b[a[r]] == 1)
k++;
}
while (k == 26)
{
if (r - l < minr - minl)
{
minr = r;
minl = l;
}
b[a[l]]--;
if (b[a[l]] == 0)
k--;
l++;
}
}
if (minr - minl + 1 == 2147483647)
cout << -1 << endl;
else cout << minr - minl + 1 << endl;
return 0;
}