题意: 给出一个字符串,计算它的所有子字符串,有多少满足多样性(多样性:该字符串出现,次数最多的字符,小于等于,该字符串不同字符种数,的数量)
分析:依次以第一个字符开始往后遍历,最多遍历100个字符(因为不同的字符有10个,超过100个,就必定不满足条件)
#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;
const int N = 1e5 + 10,M = 100;
int cnt[M];
int main()
{
int Q;
scanf("%d",&Q);
while(Q--)
{
int n;
char a[N];
scanf("%d %s",&n,a);
long long res=0;
for(int i=0;i<n;i++)
{
memset(cnt,0,sizeof cnt);//每次更新各个数字的数量
int diff=0,mx=0;
for(int k=0;k+i<n && k<M;k++)//k<100,是因为当子字符串超过100,绝对不满足条件
{
int c=(int)(a[k+i]-'0');//k的作用从i往后遍历k个字符
if(cnt[c]==0) diff++;//如果该字符第一次出现,就++
cnt[c]++;//每次记录各个字符的数量
mx=max(mx,cnt[c]);//每次取所有数字中出现次数最多的数量
if(mx<=diff) res++;//如果最多的数量小于等于字符不同的数量就++
}
}
cout<<res<<endl;
}
return 0;
}