hiho about the first 243 weeks 1485 hiho string

Disclaimer: the author is limited, blog inevitably a lot of flaws and even serious mistakes, I hope you correct. While writing the biggest goal is also to exchange learning, not paying attention and spread. Long way to go, and you encourage each other. https://blog.csdn.net/yexiaohhjk/article/details/88171612

Feet take practice, this problem is noted that exactly 2 h, 1 th i, 1 th 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;
    }
}

Guess you like

Origin blog.csdn.net/yexiaohhjk/article/details/88171612