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 }
时间: 2024-12-12 13:26:26

hihocoder offer收割编程练习赛11 A hiho字符串的相关文章

hihocoder offer收割编程练习赛11 B 物品价值

思路: 状态压缩 + dp. 实现: 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 using namespace std; 6 7 const int INF = 0x3f3f3f3f; 8 9 int t, n, m, s; 10 int V[1005], S[1005], dp[1005][(1 << 11) +

hihocoder offer收割编程练习赛11 C 岛屿3

思路: 并查集的应用. 实现: 1 #include <iostream> 2 #include <cstdio> 3 using namespace std; 4 5 bool a[1005][1005]; 6 int n, x, y; 7 int par[1000005]; 8 int ran[1000005]; 9 int dx[4] = { 1, 0, -1, 0 }; 10 int dy[4] = { 0, 1, 0, -1 }; 11 12 void init(int

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

hihocoder - [Offer收割]编程练习赛17 题目1 : F1 Score 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi和他的小伙伴们一起写了很多代码.时间一久有些代码究竟是不是自己写的,小Hi也分辨不出来了. 于是他实现了一个分类算法,希望用机器学习实现自动分类. 为了评价这个分类算法的优劣,他选出了N份有标记的代码作测试集,并决定用F1 Score作为评价标准. 给出N份代码的实际作者是不是小Hi以及分类算法预测的结果,请你计算F1 Sco

[Offer收割]编程练习赛11 题目1 : hiho字符串

时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 如果一个字符串恰好包含2个'h'.1个'i'和1个'o',我们就称这个字符串是hiho字符串. 例如"oihateher"."hugeinputhugeoutput"都是hiho字符串. 现在给定一个只包含小写字母的字符串S,小Hi想知道S的所有子串中,最短的hiho字符串是哪个. 输入 字符串S 对于80%的数据,S的长度不超过1000 对于100%的数据,S的长度不超过100000 输

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

版本号排序 不知道什么傻逼原因,就是过不了 #pragma comment(linker, "/STACK:102400000,102400000") #include<stdio.h> #include<string.h> #include<stdlib.h> #include<vector> #include<algorithm> #include<iostream> #include<map> #

hihocoder [Offer收割]编程练习赛18 C 最美和弦(dp)

题目链接:http://hihocoder.com/problemset/problem/1532 题解:一道基础的dp,设dp[i][j][k][l]表示处理到第几个数,当前是哪个和弦错了几次初始x值是多少.这里还要再辅助一个val[k]表示处理到当前情况只错了k次的最小值是多少因为改变的不止是和弦还有初始值,可以看一下代码理解一下. #include <iostream> #include <cstring> #include <cstdio> #include &

hihocoder offer收割编程练习赛13 D 骑士游历

思路: 矩阵快速幂. 实现: 1 #include <iostream> 2 #include <cstdio> 3 #include <vector> 4 using namespace std; 5 6 typedef long long ll; 7 typedef vector<ll> vec; 8 typedef vector<vec> mat; 9 10 const ll mod = 1e9 + 7; 11 12 ll n, x, y;

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

思路: 模拟,深搜. 实现: 1 #include <iostream> 2 #include <cstdio> 3 #include <string> 4 using namespace std; 5 6 const int dx[4] = { 0, 1, 0, -1 }; 7 const int dy[4] = { -1, 0, 1, 0 }; 8 9 int n, m, cnt = 0; 10 int a[305][305]; 11 bool vis[305][3

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

思路: 可能数据太水了,随便乱搞就过了. 实现: 1 #include <iostream> 2 #include <cstdio> 3 #include <algorithm> 4 using namespace std; 5 typedef long long ll; 6 7 int a[100005], n; 8 9 int main() 10 { 11 int t; 12 cin >> t; 13 while (t--) 14 { 15 ll max