(模拟)hihocoder

原题链接:http://hihocoder.com/problemset/problem/1061


题意

给定一个不超过10MB的字符串,问是否存在一个子串,是相同数量的不同的连续升序小写字母组成的。


分析

这题坑的我想死,,一开始写了一个for循环,怎么也查不出错,就是WA,差点吐血。。

最后AC的思路是,按顺序把字母的数量整理出来,然后遍历一遍整理出的来字母,看看是否存在符合条件的。

通过枚举,我们可以很快发现,只需要中间的数量不大于两边的数量就行了,因为是检验存在性,只需要判断3连串就行了。


代码

 1 #include <set>
 2 #include <map>
 3 #include <list>
 4 #include <cmath>
 5 #include <queue>
 6 #include <vector>
 7 #include <bitset>
 8 #include <string>
 9 #include <cctype>
10 #include <cstdio>
11 #include <cstring>
12 #include <cstdlib>
13 #include <iostream>
14 #include <algorithm>
15 
16 using namespace std;
17 
18 typedef long long ll;
19 typedef unsigned long long ull;
20 #define inf (0x3f3f3f3f)
21 #define lnf (0x3f3f3f3f3f3f3f3f)
22 #define eps (1e-8)
23 int sgn(double a) {return a < -eps ? -1 : a < eps ? 0 : 1;}
24 
25 //--------------------------
26 
27 
28 int t;
29 int n;
30 string str;
31 
32 string s;
33 vector<int> v;
34 
35 
36 void solve() {
37     cin >> t;
38     while (t--) {
39         s.clear();
40         v.clear();
41         cin >> n >> str;
42         int cnt = 1;
43         for (int i = 0; i < n; i++) {
44             if (str[i] == str[i + 1]) {
45                 cnt++;
46             } else {
47                 s.push_back(str[i]);
48                 v.push_back(cnt);
49                 cnt = 1;
50             }
51         }
52         bool flag = false;
53         for (int i = 1; i < s.size(); i++) {
54             if (s[i] - s[i - 1] == 1 && s[i + 1] - s[i] == 1 && v[i] <= v[i - 1] && v[i + 1] >= v[i]) {
55                 flag = true;
56                 break;
57             }
58         }
59 
60         if (flag) {
61             puts("YES");
62         } else {
63             puts("NO");
64         }
65     }
66 
67 }
68 
69 int main() {
70 
71 #ifndef ONLINE_JUDGE
72     freopen("1.in", "r", stdin);
73     //freopen("1.out", "w", stdout);
74 #endif
75     //iostream::sync_with_stdio(false);
76     solve();
77     return 0;
78 }
原文地址:https://www.cnblogs.com/tak-fate/p/6142643.html