zoukankan      html  css  js  c++  java
  • hihocoder offer收割编程练习赛11 A hiho字符串

    思路:

    我用的尺取。

    注意题目描述为恰好2个'h',1个'i',1个'o'。

    实现:

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <string>
     4 #include <algorithm>
     5 using namespace std;
     6 
     7 const int INF = 0x3f3f3f3f;
     8 
     9 int ch[100005], ci[100005], co[100005];
    10 
    11 bool check(int pos, int a, int b, int c)
    12 {
    13     return ch[pos] <= a - 2 && ci[pos] <= b - 1 && co[pos] <= c - 1;
    14 }
    15 
    16 int main()
    17 {
    18     string s;
    19     cin >> s;
    20     int n = s.length();
    21     ch[0] += (s[0] == 'h');
    22     ci[0] += (s[0] == 'i');
    23     co[0] += (s[0] == 'o');
    24     int last = 0, min_len = INF;
    25     for (int i = 1; i < n; i++)
    26     {
    27         ch[i] = ch[i - 1] + (s[i] == 'h');
    28         ci[i] = ci[i - 1] + (s[i] == 'i');
    29         co[i] = co[i - 1] + (s[i] == 'o');
    30         if (ch[i] >= 2 && ci[i] >= 1 && co[i] >= 1)
    31         {
    32             while (last < i && check(last, ch[i], ci[i], co[i]))
    33             {
    34                 last++;
    35             }
    36             if (ch[i] - ch[last - 1] == 2 && 
    37                 ci[i] - ci[last - 1] == 1 && 
    38                 co[i] - co[last - 1] == 1) 
    39                 min_len = min(min_len, i - last + 1);
    40         }
    41     }
    42     if (min_len != INF)
    43         cout << min_len << endl;
    44     else
    45         cout << -1 << endl;
    46     return 0;
    47 }
  • 相关阅读:
    Evanyou Blog 彩带
    Evanyou Blog 彩带
    Evanyou Blog 彩带
    Evanyou Blog 彩带
    Evanyou Blog 彩带
    Evanyou Blog 彩带
    Evanyou Blog 彩带
    Evanyou Blog 彩带
    Evanyou Blog 彩带
    《C++常见问题及解答》
  • 原文地址:https://www.cnblogs.com/wangyiming/p/6626812.html
Copyright © 2011-2022 走看看