返回题解分享
讨论 / 题解分享/ 帖子详情

子串简写(编程题) - 题解

#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N=5e5+5;
int k;
string str;
char c1,c2;
int a[N],b[N];
int ans;
signed main(){
	cin>>k;
	cin>>str>>c1>>c2;
	int cnt1=0,cnt2=0;

	for(int i=0;i<(int)str.size();i++){
		if(str[i]==c1) a[cnt1++]=i;
		else if(str[i]==c2) b[cnt2++]=i; 
	}
	int j=0;
	for(int i=0;i<cnt1;i++){
		for(;j<cnt2;j++){
			if(b[j]-a[i]+1>=k){
				ans+=cnt2-j;
				break;
			}
		}
	}
	cout<<ans<<endl; 
	return 0;
}
0 回复 0 转发 0 喜欢 6 阅读
回复 (0)
默认 最新
暂无回复,快来抢沙发!