Codeforces Round #306 (Div. 2)
来源:互联网 发布:sql bigdecimal 编辑:程序博客网 时间:2024/05/01 10:04
You are given string s. Your task is to determine if the given string s contains two non-overlapping substrings "AB" and "BA" (the substrings can go in any order).
The only line of input contains a string s of length between 1 and 105 consisting of uppercase Latin letters.
Print "YES" (without the quotes), if string s contains two non-overlapping substrings "AB" and "BA", and "NO" otherwise.
ABA
NO
BACFAB
YES
AXBYBXA
NO
In the first sample test, despite the fact that there are substrings "AB" and "BA", their occurrences overlap, so the answer is "NO".
In the second sample test there are the following occurrences of the substrings: BACFAB.
In the third sample test there is no substring "AB" nor substring "BA".
------------
就是简单的字符串处理。
#include <cstdio>#include <cstring>char ____[100100];int main(){int _;int __ = 0;int ___ = 0;gets(____);_ = strlen(____);for(int i = 1; i < _; i++) {if(____[i] == 'B'&&____[i - 1] == 'A') {if(___&&___ < i - 1) {printf("YES\n");return 0;} if(!__)__ = i;}if(____[i] == 'A'&&____[i - 1] == 'B') {if(__&&__ < i - 1) {printf("YES\n");return 0;}if(!___)___ = i;}}printf("NO\n");return 0;}----------
You have n problems. You have estimated the difficulty of the i-th one as integer ci. Now you want to prepare a problemset for a contest, using some of the problems you've made.
A problemset for the contest must consist of at least two problems. You think that the total difficulty of the problems of the contest must be at least l and at most r. Also, you think that the difference between difficulties of the easiest and the hardest of the chosen problems must be at least x.
Find the number of ways to choose a problemset for the contest.
The first line contains four integers n, l, r, x (1 ≤ n ≤ 15, 1 ≤ l ≤ r ≤ 109, 1 ≤ x ≤ 106) — the number of problems you have, the minimum and maximum value of total difficulty of the problemset and the minimum difference in difficulty between the hardest problem in the pack and the easiest one, respectively.
The second line contains n integers c1, c2, ..., cn (1 ≤ ci ≤ 106) — the difficulty of each problem.
Print the number of ways to choose a suitable problemset for the contest.
3 5 6 11 2 3
2
4 40 50 1010 20 30 25
2
5 25 35 1010 10 20 10 20
6
In the first example two sets are suitable, one consisting of the second and third problem, another one consisting of all three problems.
In the second example, two sets of problems are suitable — the set of problems with difficulties 10 and 30 as well as the set of problems with difficulties 20 and 30.
In the third example any set consisting of one problem of difficulty 10 and one problem of difficulty 20 is suitable.
----------
n<=15(o^^o)♪
#include <cstdio>#include <cstring>#include <algorithm>using namespace std;int c[15];int n;int l, r, x;bool solve(int);int main(){int sum = 0;scanf("%d%d%d%d", &n, &l, &r, &x);for(int i = 0; i < n; i++)scanf("%d", c + i);sort(c, c + n);for(int i = 1; i <= (1 << n) - 1; i++)if(solve(i))sum++;printf("%d\n", sum);return 0;}bool solve(int a){int finger = 1;int min = 0, max = 0, sum = 0;for(int i = 0; i < n; i++) {if(finger&a) {if(min == 0)min = c[i];if(max < c[i])max = c[i];sum += c[i];}finger <<= 1;}if(sum >= l&&sum <= r&&max - min >= x)return true;elsereturn false;}----------
暴力求解并不会超时。
#include <cstdio>#include <cstring>#include <algorithm>using namespace std;char buf[111];int toInt(char, char, char);int main(){int length;gets(buf);length = strlen(buf);if(length == 1) {if(buf[0] == '0' || buf[0] == '8') {printf("YES\n%c", *buf);return 0;} else {printf("NO\n");return 0;}}for(int i = 0; i < length - 2; i++) {for(int j = i + 1; j < length - 1; j++) {for(int k = j + 1; k < length; k++) {if(toInt(buf[i], buf[j], buf[k]) % 8 == 0) {printf("YES\n%d\n", toInt(buf[i], buf[j], buf[k]));return 0;}if(toInt('0', buf[j], buf[k]) % 8 == 0) {printf("YES\n%d\n", toInt('0', buf[j], buf[k]));return 0;}if(toInt('0', buf[i], buf[k]) % 8 == 0) {printf("YES\n%d\n", toInt('0', buf[i], buf[k]));return 0;}if(toInt('0', buf[i], buf[j]) % 8 == 0) {printf("YES\n%d\n", toInt('0', buf[i], buf[j]));return 0;}}}}if(toInt('0', buf[0], buf[1]) % 8 == 0) {printf("YES\n%d\n", toInt('0', buf[0], buf[1]));return 0;}if(buf[0] == '0' || buf[0] == '8') {printf("YES\n%c", *buf);return 0;}if(buf[1] == '0' || buf[1] == '8') {printf("YES\n%c", buf[1]);return 0;}printf("NO\n");return 0;}int toInt(char a, char b, char c){return (a - '0') * 100 + (b - '0') * 10 + c - '0';}
----------
----------
好复杂啊,不说了。
import java.io.InputStream;import java.io.InputStreamReader;import java.io.BufferedReader;import java.io.OutputStream;import java.io.PrintWriter;import java.io.IOException;import java.util.StringTokenizer;/** * Built using CHelper plug-in * Actual solution is at the top */public class Main {public static void main(String[] args) {InputStream inputStream = System.in;OutputStream outputStream = System.out;InputReader in = new InputReader(inputStream);PrintWriter out = new PrintWriter(outputStream);TaskD solver = new TaskD();solver.solve(1, in, out);out.close();}}class TaskD { private int k; public void solve(int testNumber, InputReader in, PrintWriter out) { k = in.nextInt(); if (k % 2 == 0) { out.print("NO"); return ; } out.print("YES\n"); if (k == 1) out.println("2 1\n1 2"); else { int inc = 2; int n = 4 * (k - 1) + 2; int m = (n * k) / 2; out.printf("%d %d\n", n, m); out.printf("1 2\n"); for(int i = 1; i <= k - 1; ++i) out.printf("%d %d\n", 1, (i + inc)); for(int i = 1; i <= k - 1; ++i) for(int j = 1; j <= k - 1; ++j) out.printf("%d %d\n", i + inc, j + inc + k - 1); for(int i = 1; i <= k - 1; i += 2) out.printf("%d %d\n", i + inc + k - 1, i + inc + k); inc += 2 * (k - 1); for(int i = 1; i <= k - 1; ++i) out.printf("%d %d\n", 2, (i + inc)); for(int i = 1; i <= k - 1; ++i) for(int j = 1; j <= k - 1; ++j) out.printf("%d %d\n", i + inc, j + inc + k - 1); for(int i = 1; i <= k - 1; i += 2) out.printf("%d %d\n", i + inc + k - 1, i + inc + k); } }}class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream)); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); }}----------
从后往前,分析各种情况。
#include <cstdio>#include <cstring>#include <algorithm>using namespace std;int buf[100100];int bra[100100];int main(){int n;int finger;int flag = 0;int braCnt = 0;int lookFor = 1;scanf("%d", &n);finger = n - 1;for(int i = 0; i < n; i++) {scanf("%d", buf + i);}if(buf[finger]) {printf("NO\n");return 0;}while(--finger >= 0) {if(buf[finger] == lookFor) {if(lookFor == 0) {bra[finger] = 1;braCnt++;}printf("YES\n");flag = 1;break;} else if(lookFor == 1) {lookFor = 0;} else {bra[finger] = 1;braCnt++;}}if(!flag&&n>1) {printf("NO\n");} else {if(n == 1)printf("YES\n");for(int i = 0; i < n - 1; i++) {if(bra[i])putchar('(');putchar(buf[i] + '0');if(i == n - 2)while(braCnt--)putchar(')');printf("->");}printf("%d\n", buf[n - 1]);}return 0;}
- Codeforces Round #306 (Div. 2)
- Codeforces Round #306 (Div. 2)
- Codeforces Round #306 (Div. 2)
- Codeforces Round #306 (Div. 2)
- Codeforces Round #306 (Div. 2)
- Codeforces Round #306 (Div. 2)
- Codeforces Round #306 (Div. 2) A
- Codeforces Round #306 (Div. 2) B
- Codeforces Round #306 (Div. 2) C
- Codeforces Round #306 (Div. 2)_A
- Codeforces Round #306 div 2 solution
- Codeforces Round #306 (Div. 2) A
- Codeforces Round #306 (Div. 2) B(dfs)
- Codeforces Round #306 (Div. 2)_B
- Codeforces Round #306 (Div. 2)_C
- Codeforces Round #306 (Div. 2) (ABCE题解)
- Codeforces Round #306 (Div. 2) D-E
- Codeforces Round #306 (Div. 2) B DFS
- 第三章第44题
- phpstorm
- oracle的TM锁、TX锁
- Android视图背景及图片透明效果(用颜色表示)
- 进程控制 [fork() exec() wait() waitpid()]
- Codeforces Round #306 (Div. 2)
- POJ 3169-Layout(差分约束系统)
- iPad软件界面设计基本规范
- android app中调用其他应用程序
- solr学习笔记
- week14_1.cpp
- TypeError: Cannot read property 'style' of null 错误解决
- ASE isql 连接
- SAT填空练习题(三) 含答案