版权声明:作者水平有限,博客中难免不少纰漏甚至严重错误,希望大家指正。同时撰写最大的目的也在于交流学习,而不在关注和传播。任重而道远,与您共勉。 https://blog.csdn.net/yexiaohhjk/article/details/88171612
尺取做法,注意这题是恰好为2个h,1个i,1个o。
#include<bits/stdc++.h>
using namespace std;
string Str;
int main(){
while(cin>>Str){
int l= 0,r=-1,n = Str.size(),ans = -1;
map<char,int> cnt;
for(;;){
while(r<n&&!(cnt['h']>=2&&cnt['i']>=1&&cnt['o']>=1)){
r++;
if(r<n){
cnt[Str[r]]++;
}
}
if(r==n) break;
if(cnt['h']==2&&cnt['i']==1&&cnt['o']==1){
if(ans==-1||r-l+1<ans) ans = r-l+1;
}
cnt[Str[l++]]--;
}
cout<<ans<<endl;
}
}