Nikita and string
来源:互联网 发布:分数统计软件 编辑:程序博客网 时间:2024/05/21 06:01
One day Nikita found the string containing letters "a" and "b" only.
Nikita thinks that string is beautiful if it can be cut into 3 strings (possibly empty) without changing the order of the letters, where the 1-st and the 3-rd one contain only letters "a" and the 2-nd contains only letters "b".
Nikita wants to make the string beautiful by removing some (possibly none) of its characters, but without changing their order. What is the maximum length of the string he can get?
The first line contains a non-empty string of length not greater than 5 000containing only lowercase English letters "a" and "b".
Print a single integer — the maximum possible size of beautiful string Nikita can get.
abba
4
bab
2
It the first sample the string is already beautiful.
In the second sample he needs to delete one of "b" to make it beautiful.
题意:给出一个只包含 ' a ' , ' b ' 的串,求一个最长的子串,这个字串划分为3部分,第一和第三部分只有 a 第二部分只有 b (每一部分都可为空串)
这道题当时没有想出来,后来看了一眼别人写的思路,感觉很简单
就是说这道题既然让你去求aba串的最大长度,所以这个时候就去寻找每个b两边的a的个数,用一个数组去记录在b之前的,用量另一个数组去记录在b之后的这个a的数量. 在最后的时候再通过双重for循环从i到j之间b的个数,每找到一个就比较一次,看看这两个哪个更大一些,这种方法有个特例就是全是a的时候,这个时候你用来存储最大量的变量的值是他的初始值,所以要在最后进行一次特判,假如maxx的值是其初始值,就直接把串的长度输出就行.
#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;char mp[6000]; //存储字符串int main(){ while(~scanf("%s",mp)) { int l=strlen(mp); int str[6009],str1[6009]; memset(str,0,sizeof(str)); //统计b之前有多少个 memset(str1,0,sizeof(str1)); //统计b之后有多少个 int sum=0; //统计在这个区间里面a的个数 for(int i=0;i<l;i++) { if(mp[i]=='b')str[i]=sum; else sum++; } sum=0; for(int i=l-1;i>=0;i--) { if(mp[i]=='b')str1[i]=sum; else sum++; } int maxx=-0x3f3f3f3f; //maxx 来保存最大长度 for(int i=0;i<l;i++) { sum=0; for(int j=i;j<l;j++) { if(mp[j]=='a')continue; sum++; if((str[i]+str1[j]+sum)>maxx) maxx=str[i]+str1[j]+sum; } } if(maxx==-0x3f3f3f3f) maxx=l; printf("%d\n",maxx); } return 0;}
- Nikita and string
- Nikita and string
- B. Nikita and string
- Codeforces 877 B Nikita and string
- codeforces877B(div2)Nikita and string 暴力枚举+前缀和
- Codeforces 877(442 Div.2) B. Nikita and string
- Codeforces Round #442 (Div. 2) B. Nikita and string 题解
- CF#877 B. Nikita and string(思维,暴力)
- Codeforces 877 B. Nikita and string (技巧)
- Codeforces Round #442 (Div. 2) B. Nikita and string
- Codeforces Round #442 (Div. 2) B. Nikita and string
- 【Codeforces Round #442 (Div. 2)】 B. Nikita and string (线性状态dp)
- Codeforces Round #442 (Div. 2) B. Nikita and string (记忆化搜索)
- Codeforces Round #442(Div.2) B.Nikita and string(线性DP)
- codeforces 877 problem B Nikita and string 【思维 + 前后缀思想】
- 南华新生训练2017- (思维题)codeforces Round 442 B Nikita and string
- 【CodeForces】759C Nikita and stack
- Codeforces Round #430 (Div. 2) E. Nikita and game
- usermod,用户密码管理,mkpasswd
- 多线程(未完结,待续)
- spring基础思维导图
- Oc 拨打电话和发短信功能的实现
- 在docker上部署https
- Nikita and string
- javascript中的类型判断
- svn服务器搭建
- Reverse Words in a String--LeetCode
- mysql创建表,同时加上约束,SQL查看所有表和查看某个表结构;sql删除表
- 经典背包问题----(01背包、完全背包、多重背包)
- 排序算法@c++描述-shell排序
- C语言回溯法装载问题
- easydss与linux内核接收网络数据流程(四)