玄学问题
查看原帖
玄学问题
740325
wangboyue楼主2023/10/28 10:11
#include<bits/stdc++.h>
using namespace std;
int n;
string s;
int cnt[133];
int main(){
	int tot=0,ans=1e9;
	cin>>n>>s;
	for(int i=0;i<s.size();i++){
		cnt[s[i]]++;
		if(cnt[s[i]]==1){
			tot++;
		}
	}
//	cout<<n<<endl;
	memset(cnt,0,sizeof(cnt));
	int lt=0,rt=-1,tmp=0;
	while(rt<s.size()-1){
		while(rt<s.size()-1&&tmp<tot){
			rt++;
			cnt[s[rt]]++;
			if(cnt[s[rt]]==1){
				tmp++;
			}
		}
//		
		while(tmp==tot&&lt<=rt){
//			cout<<1;
			ans=min(ans,rt-lt+1);
			cnt[s[lt]]--;
			if(cnt[s[lt]]==0){
				tmp--;
			}
			lt++;
		}
	}
	cout<<ans;
	return 0;
}
 

为什么把第18、19行的s.size()改成n就过了?

2023/10/28 10:11
加载中...