String HDU5672 尺取法
题目大意:给你字符串,和一个数字N。让你在它所有子串中找存在至少N个不同字母的子串,求出个数。
1 #include<algorithm> 2 #include<cstdio> 3 #include<iostream> 4 #include<queue> 5 #include<vector> 6 #include<map> 7 #include<stack> 8 #include<string> 9 #include<cstring> 10 #define ll long long 11 using namespace std; 12 const int maxn = 1e6 + 10; 13 ll n,vis[maxn]; 14 char str[maxn]; 15 int main() 16 { 17 ll T; 18 scanf("%lld", &T); 19 while (T--) 20 { 21 memset(vis, 0, sizeof(vis)); 22 scanf("%s", str); 23 scanf("%lld", &n); 24 ll l = 0, r = 0,len = strlen(str), k = 0, ans = 0; 25 while (l < len) 26 { 27 while (r < len && k < n) 28 { 29 if (vis[str[r] - 'a'] == 0)k++; 30 vis[str[r] - 'a']++; 31 r++; 32 } 33 if (k < n) 34 break; 35 ans += len - r + 1; 36 vis[str[l] - 'a']--; 37 if (vis[str[l] - 'a'] == 0)k--; 38 l++; 39 } 40 printf("%lld\n", ans); 41 } 42 }