ACM刷题之codeforce————Nikita and string
2018-02-18 23:31
337 查看
Nikita and stringtime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputOne 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?InputThe first line contains a non-empty string of length not greater than 5 000 containing only lowercase English letters "a" and "b".OutputPrint a single integer — the maximum possible size of beautiful string Nikita can get.ExamplesinputCopy
做法:建立一个数组pre来记录当前下标前面的a的个数,再建立一个数组nex来记录当前下标后面的a的个数。然后再两重循环遍历一段字符串内b的个数,计为sum。当pre + sum + nex 最大时,就是剩下字符的最大值。
下面是ac代码:#include<bits/stdc++.h>
using namespace std;
#define MID(x,y) ((x+y)>>1)
#define CLR(arr,val) memset(arr,val,sizeof(arr))
#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);
const double PI = acos(-1.0);
const int INF = 0x3f3f3f3f;
const int N=5555;
char c
;
int pre
,nex
;
int main()
{
//freopen("f:/input.txt", "r", stdin);
int i, j ,k, le, sum;
scanf("%s", &c);
le = strlen(c);
CLR(pre, 0);
CLR(nex, 0);
sum = 0;
for (i = 0 ; i < le; i++) {
if (c[i] == 'a') ++sum;
else pre[i] = sum;
}
sum = 0;
for (i = le -1 ; i >= 0; i--) {
if (c[i] == 'a') ++sum;
else nex[i] = sum;
}
int maxn = -INF;
for (i = 0; i < le; i++) {
sum = 0;
for (j = i; j < le; j++) {
if (c[j] == 'b') {
sum++;
if (pre[i] + sum + nex[j] > maxn) maxn = pre[i] + sum + nex[j];
}
}
}
if (maxn == -INF) maxn = le;
printf("%d\n", maxn);
}
abbaoutput
4inputCopy
baboutput
2NoteIt 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 组成 字符串,让你删掉几个字符,然后能拆分成三个字符串形式为 ,第一个字符串只能有a,第二个字符串只能有b,第三个字符串只能有a,每个字符串都可以为空。问最多能剩下几个字符。
做法:建立一个数组pre来记录当前下标前面的a的个数,再建立一个数组nex来记录当前下标后面的a的个数。然后再两重循环遍历一段字符串内b的个数,计为sum。当pre + sum + nex 最大时,就是剩下字符的最大值。
下面是ac代码:#include<bits/stdc++.h>
using namespace std;
#define MID(x,y) ((x+y)>>1)
#define CLR(arr,val) memset(arr,val,sizeof(arr))
#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);
const double PI = acos(-1.0);
const int INF = 0x3f3f3f3f;
const int N=5555;
char c
;
int pre
,nex
;
int main()
{
//freopen("f:/input.txt", "r", stdin);
int i, j ,k, le, sum;
scanf("%s", &c);
le = strlen(c);
CLR(pre, 0);
CLR(nex, 0);
sum = 0;
for (i = 0 ; i < le; i++) {
if (c[i] == 'a') ++sum;
else pre[i] = sum;
}
sum = 0;
for (i = le -1 ; i >= 0; i--) {
if (c[i] == 'a') ++sum;
else nex[i] = sum;
}
int maxn = -INF;
for (i = 0; i < le; i++) {
sum = 0;
for (j = i; j < le; j++) {
if (c[j] == 'b') {
sum++;
if (pre[i] + sum + nex[j] > maxn) maxn = pre[i] + sum + nex[j];
}
}
}
if (maxn == -INF) maxn = le;
printf("%d\n", maxn);
}
相关文章推荐
- Codeforces Round #442 (Div. 2) B. Nikita and string
- ACM刷题之codeforce————Arpa and a research in Mexican wave
- codeforce 766D Mahmoud and a Dictionary 带权并查集
- Vasya and String CodeForces - 676C [二分查找 + 尺取法]
- Codeforces Round #442 (Div. 2) B. Nikita and string (记忆化搜索)
- B. Nikita and string
- codeforce 777 C. Alyona and Spreadsheet (打表预处理)
- CodeForce 508C Anya and Ghosts (贪心+模拟)
- codeforce.Vladik and flights
- CodeForce 526B Om Nom and Dark Park(dp + 递归)
- codeforce 766D Mahmoud and a Dictionary 带权并查集
- code force. Anton and fairy tale.
- 【CodeFores 798B】 Mike and strings(模拟+string)
- Codeforce Gym 100819P : 2015-2016 ACM-ICPC Pacific Northwest Regional Contest - P 仔细看题啊!
- 【动态规划】 ◆CodeForce 461B◆ Appleman and Tree
- Educational Codeforces Round 8 C. Bear and String Distance 贪心
- codeforce之Bear and Two Paths
- codeforce 766D Mahmoud and a Dictionary 带权并查集
- Codeforces Round #442 (Div. 2) B. Nikita and string
- Codeforces Round #430 (Div. 2) E. Nikita and game