首页 > 其他 > 详细

Codeforces Round #652 (Div. 2) B. AccurateLee(思维)

时间:2020-07-04 21:35:19      阅读:40      评论:0      收藏:0      [点我收藏+]

题意:

给你一个01字符串,现在你可以删除其中的一些子序列,要求如下:
当遇到1 0的俩个连续子字符串后,可以删除其中的一个字符,现在要求把他删到尽量最短并且字典序最小,输出最后的字符串

 

题解:

刚开始想着就是模拟,谁知道越模拟越复杂,,,最后换思路一看,这不就是输出所有前缀0,输出所有后缀1.中间(中间就是去掉前缀0和后缀1之后的)如果有0的话就输出0就行了。如果前缀0加上后缀1刚好等于原字符串长度,就不用判断中间那一段了。

 

代码:

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include<iostream>
 4 #include<algorithm>
 5 using namespace std;
 6 #define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
 7 int main()
 8 {
 9     int t;
10     char s[100005];
11     scanf("%d",&t);
12     while(t--)
13     {
14         int n,ans0=0,ans1;
15         scanf("%d",&n);
16         ans1=n;
17         scanf("%s",s);
18         for(int i=0;i<n;++i)
19         {
20             if(s[i]==0)
21                 ans0=max(ans0,i);
22             else ans1=min(ans1,i);
23         }
24         if(ans0<=ans1)
25         printf("%s\n",s);
26         else 
27         {
28             for(int i=0;i<ans1;++i)
29             printf("%c",s[i]);
30             for(int i=ans0;i<n;++i)
31             printf("%c",s[i]);
32             printf("\n");
33         }
34     }
35     return 0;
36 }

 

Codeforces Round #652 (Div. 2) B. AccurateLee(思维)

原文:https://www.cnblogs.com/kongbursi-2292702937/p/13236544.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!