(模拟)hihocoder - 1061 Beautiful String

原题链接: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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值