Problem Description:
You are given a string S consisting of lowercase letters, and your task is counting the number of substring that the number of each lowercase letter in the substring is no more than K.
Input:
In the first line there is an integer T , indicates the number of test cases.
For each case, the first line contains a string which only consist of lowercase letters. The second line contains an integer K.
[Technical Specification]
1<=T<= 100
1 <= the length of S <= 100000
1 <= K <= 100000
For each case, the first line contains a string which only consist of lowercase letters. The second line contains an integer K.
[Technical Specification]
1<=T<= 100
1 <= the length of S <= 100000
1 <= K <= 100000
Output:
For each case, output a line contains the answer.
Sample Input:
3
abc
1
abcabc
1
abcabc
2
Sample Output:
6
15
21
题意:给出一个字符串s和一个整数k,问有多少s的子串满足条件,条件是子串中每个字母出现的个数不能大于k(只有小写字母)。
这里用到一个公式,一个长度为n的字符串的连续子串的个数为1+2+3+……+n。证明:举例如abcabc:我们可以分别得到该字符串长度为1,2,3,4,5,6的子串:
1:a b c a b c;
2:ab bc ca ab bc;
3:abc bca cab abc;
4:abca bcab cabc;
5:abcab bcabc;
6:abcabc;
不难发现连续子串的个数和是1+2+3+……+n。
#include<stdio.h> #include<string.h> #include<queue> #include<math.h> #include<stdlib.h> #include<algorithm> using namespace std; const int N=1e5+10; const int INF=0x3f3f3f3f; const int MOD=1e9+7; typedef long long LL; char s[N]; int vis[N]; ///标记序号为i的字母出现的次数 int main () { int T, k, i, j; LL ans; ///这里必须用LL,不然会wa scanf("%d", &T); while (T--) { scanf("%s %d", s, &k); j = 0; ///当发现一个字母出现的次数>k时,j的值会改变 ans = 0; memset(vis, 0, sizeof(vis)); for (i = 0; s[i] != '