尺取做法,注意这题是恰好为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;
}
}