码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2017-03-27 15:44:43      阅读:158      评论:0      收藏:0      [点我收藏+]

标签:ios   clu   ast   ace   inf   turn   div   pre   else   

思路:

我用的尺取。

注意题目描述为恰好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字符串

标签:ios   clu   ast   ace   inf   turn   div   pre   else   

原文地址:http://www.cnblogs.com/wangyiming/p/6626812.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!