首页 > 代码库 > hihocoder offer收割编程练习赛11 A hiho字符串
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字符串
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。