Codeforces Round #442 (Div. 2)(思路题)
来源:互联网 发布:阿里云域名控制台 编辑:程序博客网 时间:2024/06/14 14:09
A题,给你一个字符串,判断是否只出现以上人名仅有1次,即一个人名出现两次也是错误,用这个点hack了好几个人。
求一个字符串在另一个字符串中出线次数的方法如下:
int Count = 0; string op = "aa"; string str = "aaaaaaaa"; int len = op.length(); for (int i = 0; (i = str.find(op, i)) != string::npos; i += len) Count++; cout << Count << endl;
或者是用.rfind,即最后一次出现的位置和第一次出现的位置相同。
B题,就是给你一个字符串,通过删除一些字符串,最后使字符串只有三部分(每一个部分也可为空),第一部分全是a,第二部分全是b,第三部分全是a,然后求这个字符串的最大长度。
这个题目的思路也很简单,因为是分成三部分,所以只要枚举两个指针的位置就能把字符串分成三部分,第一部分和第三部分只统计a的个数,第二部分只统计b的个数。所以这里再求一下字符串中a,b个数的前缀和就行。
代码如下:
#include<iostream>#include<cstring>#include<string>#include<set>#include<vector>#include<map>#include<queue>#include<algorithm>using namespace std;const int MAX = 5010;string str;int sumA[MAX],sumB[MAX];int solve(){ int len = (int)str.length(); for(int i=1;i<=len;++i){ if(str[i-1] == 'a') sumA[i] = 1; else sumB[i] = 1; sumA[i] += sumA[i-1]; sumB[i] += sumB[i-1]; } int ans = 0; for(int p1=1;p1<=len;++p1){ for(int p2=p1;p2<=len;++p2){ int res = sumA[p1] + (sumB[p2] - sumB[p1-1]) + (sumA[len]-sumA[p2]); ans = max(res,ans); } } return ans;}int main(void){ cin >> str; cout << solve() << endl; return 0;}
C题:
这个题目的意思就是给你一个n代表n个点,有的点可能有坦克,也可能没有,每个坦克被炸后会向两边移动,比如2会移动到1或者3,每个坦克要炸两次才能摧毁。问你对于n个点,全部摧毁至少要炸几次,炸的顺序是什么。
这个题比较怪,不过同样例能找到规律,就是先炸偶数节点,然后坦克无论怎样都会跑到奇数节点,再把奇数节点炸了。最后再炸一遍偶数节点就行。。
这个就是最优的结果。代码如下:
#include<iostream>#include<cstdio>#include<cstring>#include<string>#include<set>#include<cmath>#include<queue>using namespace std;int n;void solve() { int res = n + n / 2; printf("%d\n", res); printf("%d", 2); for (int i = 4; i <= n; i += 2) printf(" %d", i); for (int i = 1; i <= n; i += 2) printf(" %d", i); for (int i = 2; i <= n; i += 2) printf(" %d", i); printf("\n");}int main(void) { scanf("%d", &n); solve(); return 0;}
阅读全文
0 0
- Codeforces Round #442 (Div. 2)(思路题)
- Codeforces Round #435 (Div. 2) (思路题)
- Codeforces Round #298 (Div. 2) C. Polycarpus' Dice(思路)
- Codeforces Round #209 (Div. 2)思路
- codeforces round# 302(div 2 D)(思路)
- Codeforces Round #358 (Div. 2) -- B. Alyona and Mex (思路水题)
- Codeforces Round #365 (Div. 2) -- C. Chris and Road (思路水题)
- Codeforces Round #199 (Div. 2) -- A. Xenia and Divisors (思路)
- Codeforces Round #423 (Div. 2) B. Black Square(思路)
- Codeforces Round #202 (Div. 1) A. Mafia(二分,思路)
- Codeforces Round #442 (Div. 2)
- Codeforces Round #442 (Div. 2)
- Codeforces Round #442 (Div. 2)
- Codeforces Round #361 (Div. 2) -- A. Mike and Cellphone (思路题目)
- Codeforces Round #293 (Div. 2) -- E. Arthur and Questions (思路 + 贪心)
- Codeforces Round #293 (Div. 2) A. Vitaly and Strings(思路)
- Codeforces Round #395 (Div. 2) D. Timofey and rectangles(思路)
- 【codeforces】Codeforces Round #441(div.2)
- 平均最短路径长度——网络建模之基本指标
- 筛法加强版
- Linux Shell脚本攻略 学习笔记 --- 第一天
- 科普一下: 电子的速度很慢,比乌龟还慢, 比蚂蚁还慢!
- linkedlist源码解读
- Codeforces Round #442 (Div. 2)(思路题)
- 聊聊SIGCHLD信号的作用
- 51nod 1407 与与与与 dp+容斥
- 菜单
- 状态模式学习和思考
- ubuntu常用软件安装
- Django 和mysql通信得插件windows下安装mysql-python报错的解决办法
- RGB颜色模式
- String to Integer 算法学习