B. Diverse Substrings (2024.1.22灵茶)

30 篇文章 0 订阅
14 篇文章 0 订阅

链接 : 

Problem - 1748B - Codeforces

思路 : 

0-9一共十个字符,由于一个子串是diverse要满足每个字符出现的次数,不超过子串字符种类的数目,所以子串最长的情况也就是0-9每个10个,长度为100,所以可以暴力枚举所有子串 ;

详情请看代码 : 

代码:
 

#include<bits/stdc++.h>
#define IOS ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define endl '\n'
#define lowbit(x) (x&(-x))
#define sz(a) (int)a.size()
#define pb push_back
#define all(a) a.begin(), a.end()
typedef long long LL;
const int mod = 1e9+7;
const int N = 2e5+10;

using namespace std;

inline void solve(){
	int n ; cin >> n ;
	string s ; cin >> s ;
	if(n==1){
		cout << 1 << endl;
		return ;
	}
	LL ans = 0 ;
	for(int i=0;i<n;i++){
		int t[10]{} ;// 每个出现次数 , 初始化为0 
		int cnt = 0 ; // 种类个数
		int ma = 0 ;// 最频繁出现字符出现次数
		for(int j=i;j<n&&(++t[s[j]-'0'])<=10 ;j++){
			ma = max(ma , t[s[j]-'0']);
			if(t[s[j]-'0']==1) cnt ++;
			if(cnt >= ma) ans ++;
		} 
	}
	cout << ans << endl ;
}
 
signed main()
{
    IOS
    int _ = 1;
    cin >> _;
    while(_ --) solve();
    return 0;
}

  • 8
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值