HDU 5056 Boring count

用数组a[26]记录每次字母出现的次数,用j标记起点坐标,对于每个字母a[i],以他结尾并且符合条件的字符串有i-j个


然后从第一个字母开始,但重复字母等于k的时候,将从j开始找第一个a[j]=a[i]

每次ans+=(i-j)


#include<iostream>
#include<cstdio>
#include<set>
#include<string>
#include<string.h>
#include<cstring>
#include<vector>
#include<map>
#include<queue>
#include<stack>
#include<cctype>
#include<algorithm>
#include<sstream>
#include<utility>
#include<cmath>
#include<functional>
#define mt(a) memset(a,0,sizeof (a))
#define fl(a,b,c) fill(a,b,c)
#define SWAP(a,b,t) (t=a,a=b,b=t)

#define inf 1000000000+7

using namespace std;
typedef long long ll;
int main()
{
	int T;
	cin >> T;
	while (T--)
	{
		int a[26] = { 0 };
		char s[100000 + 20];
		scanf("%s", &s[0]);
		int k;
		cin >> k;
		ll ans = 0, cot = 0;
		int j = 0;
		for (int i = 0; s[i]; i++)
		{
			if (a[s[i] - 'a'] == k)
			{
				while (s[j] != s[i])
				{
					a[s[j] - 'a']--;
					j++;
					cot--;
				}
				a[s[j] - 'a']--;
				j++;
				cot--;
			}
			a[s[i] - 'a']++;
			cot++;
			ans += cot;
		}
		printf("%lld\n", ans);
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值