hihocoder offer收割编程练习赛11 A hiho字符串

Posted 王宜鸣

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 }

 

以上是关于hihocoder offer收割编程练习赛11 A hiho字符串的主要内容,如果未能解决你的问题,请参考以下文章

hihocoder offer收割编程练习赛11 A hiho字符串

hihocoder offer收割编程练习赛8 A

hihocoder offer收割编程练习赛12 C 矩形分割

hihocoder offer收割编程练习赛12 D 寻找最大值

hihocoder - [Offer收割]编程练习赛17

[hihocoder][Offer收割]编程练习赛64