Codeforces Round #306 (Div. 2) (ABCE)
A. Two Substrings time limit per test 2 seconds memory limit per test 256 megabytes
You are given stringS. Your task is to determine if the given stringSContains two non-overlapping substrings AB and BA (the substrings can go in any order ).
Input
The only line of input contains a stringSOf length between 1 and 105 consisting of uppercase Latin letters.
Output
Print YES (without the quotes), if stringSContains two non-overlapping substrings AB and BA, and NO otherwise.
Sample test (s) Input
ABA
Output
NO
Input
BACFAB
Output
YES
Input
AXBYBXA
Output
NO
Note
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.
Give a string and ask if we can find two substrings, AB and BA, that do not overlap with each other.
Question Analysis: Four brute-force scans, the first and second scans AB and then BA. The third and fourth scans BA and then AB. Pay attention to this group of samples ABACCCAB.
#include
#include
#include using namespace std;int const MAX = 1e5 + 5;char s1[MAX], s2[MAX];int main(){scanf(%s, s1);memcpy(s2, s1, sizeof(s1));int len = strlen(s1);bool f1 = false;bool f2 = false;for(int i = 0; i < len; i++){if(s1[i] == 'A' && s1[i + 1] == 'B'){f1 = true;s1[i] = s1[i + 1] = '*';break;}}for(int i = 0; i < len; i++)if(s1[i] == 'B' && s1[i + 1] == 'A')f2 = true;if(f1 && f2){printf(YES);return 0;}f1 = f2 = false;for(int i = 0; i < len; i++){if(s2[i] == 'B' && s2[i + 1] == 'A'){f1 = true;s2[i] = s2[i + 1] = '*';break;}}for(int i = 0; i < len; i++)if(s2[i] == 'A' && s2[i + 1] == 'B')f2 = true;if(f1 && f2){printf(YES);return 0;}printf(NO);}
B. Preparing Olympus IAD time limit per test 2 seconds memory limit per test 256 megabytes
You haveNProblems. You have estimated the difficulty ofI-Th one as integerCI. 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 leastLAnd at mostR. Also, you think that the difference between difficulties of the easiest and the hardest of the chosen problems must be at leastX.
Find the number of ways to choose a problemset for the contest.
Input
The first line contains four integersN,L,R,X(1 digit ≤ DigitNLimit ≤ limit 15, 1 limit ≤ limitLLimit ≤ limitRLimit ≤ limit 109, 1 hour ≤ hourXLimit ≤ limit 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 containsNIntegersC1, bytes,C2, middle..., middle ,...,CN(1 digit ≤ DigitCILimit ≤ limit 106)-the difficulty of each problem.
Output
Print the number of ways to choose a suitable problemset for the contest.
Sample test (s) Input
3 5 6 11 2 3
Output
2
Input
4 40 50 1010 20 30 25
Output
2
Input
5 25 35 1010 10 20 10 20
Output
6
Note
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 questions: Each difficulty is ci. You need to select some questions. The total difficulty of these questions must not exceed r and not less than l, the difference between the most difficult and least difficult cannot be less than x.
Question Analysis: n is equal to 15. If you want to solve this problem, I directly search for it, which is concise and clear.
# Include
# Include
# Include using namespace std; int const INF = 0x3fffffff; int n, l, r, x; int c [20]; int ans; void DFS (int idx, int ma, int mi, int sum) // number of questions, current maximum, current minimum, and total difficulty {if (idx = n + 1) return; if (sum <= r & sum> = l & x <= ma-mi & idx = n) ans ++; DFS (idx + 1, max (ma, c [idx]), min (mi, c [idx]), sum + c [idx]); // select the idx DFS (idx + 1, ma, mi, sum); // do not select idx return;} int main () {scanf (% d, & n, & l, & r, & x); for (int I = 0; I <n; I ++) scanf (% d, & c [I]); ans = 0; DFS (0, 0, INF, 0); printf (% d, ans );}
C. Divisibility by Eight time limit per test 2 seconds memory limit per test 256 megabytes
You are given a non-negative integerN, Its decimal representation consists of at most 100 digits and doesn' t contain leading zeroes.
Your task is to determine if it is possible in this case to remove some of the digits (possibly not remove any digit at all) so that the result contains at least one digit, forms a non-negative integer, doesn' t have leading zeroes and is divisible by 8. after the removing, it is forbidden to rearrange the digits.
If a solution exists, you should print it.
Input
The single line of the input contains a non-negative integerN. The representation of numberNDoesn't contain any leading zeroes and its length doesn't exceed 100 digits.
Output
Print NO (without quotes), if there is no such way to remove some digits from numberN.
Otherwise, print YES in the first line and the resulting number after removing digits from numberNIn the second line. The printed number must be divisible by 8.
If there are multiple possible answers, you may print any of them.
Sample test (s) Input
3454
Output
YES344
Input
10
Output
YES0
Input
111111
Output
NO
Question: Give a number and ask if some of the numbers can be divisible by 8.
Question Analysis: I typed a small table and found the rule. First, there were 8 or 0 direct queries, followed by 16, 24, 32, 56, 64, 72, and 96. Second, first, find an odd number, which can be followed by one of 12, 36,. The length of the number is very small.
#include
#include
int const MAX = 105;char s[MAX];int len;// 0// 8// 16// 24// 32// 56// 64// 72// 96// 112// 136// 144// 152// 176// 192// 312// 336bool judge1(int i, char ch1, char ch2){if(s[i] == ch1){for(int j = i + 1; j < len; j++){if(s[j] == ch2){printf(YES%c%c, ch1, ch2);return true;}}}return false;}bool judge2(char ch, int i, char ch1, char ch2){for(int j = i + 1; j < len; j++){if(s[j] == ch1){for(int k = j + 1; k < len; k ++){if(s[k] == ch2){printf(YES%c%c%c, ch, ch1, ch2);return true;}}}}return false;}int main(){scanf(%s, s);len = strlen(s);for(int i = 0; i < len; i++){if(s[i] == '0'){printf(YES0);return 0;}if(s[i] == '8'){printf(YES8);return 0;}if(judge1(i, '1', '6'))return 0;if(judge1(i, '2', '4'))return 0;if(judge1(i, '3', '2'))return 0;if(judge1(i, '5', '6'))return 0;if(judge1(i, '6', '4'))return 0;if(judge1(i, '7', '2'))return 0;if(judge1(i, '9', '6'))return 0;if((s[i] - '0') % 2 == 1 && judge2(s[i], i, '1', '2'))return 0;if((s[i] - '0') % 2 == 1 && judge2(s[i], i, '3', '6'))return 0;if((s[i] - '0') % 2 == 1 && judge2(s[i], i, '4', '4'))return 0;if((s[i] - '0') % 2 == 1 && judge2(s[i], i, '5', '2'))return 0;if((s[i] - '0') % 2 == 1 && judge2(s[i], i, '7', '6'))return 0;if((s[i] - '0') % 2 == 1 && judge2(s[i], i, '9', '2'))return 0;}printf(NO);}
E. Brackets in Implications time limit per test: 2 seconds memory limit per test: 256 megabytes
Implication is a function of two logical arguments, its value is false if and only if the value of the first argument is true and the value of the second argument is false.
Implication is written by using character '', and the arguments and the result of the implication are written as '0 '(False) And '1 '(True). According to the definition of the implication:
When a logical expression contains multiple implications, then when there are no brackets, it will be calculated from left to fight. For example,
.
When there are brackets, we first calculate the expression in brackets. For example,
.
For the given logical expression determine if it is possible to place there brackets so that the value of a logical expression is false. if it is possible, your task is to find such an arrangement of brackets.
Input
The first line contains integerN(1 digit ≤ DigitNLimit ≤ limit 100 limit 000)-the number of arguments in a logical expression.
The second line containsNNumbersA1, bytes,A2, middle..., middle ,...,AN(), Which means the values of arguments in the expression in the order they occur.
Output
Print NO (without the quotes), if it is impossible to place brackets in the expression so that its value was equal to 0.
Otherwise, print YES in the first line and the logical expression with the required arrangement of brackets in the second line.
The expression shoshould only contain characters '0', '1', '-' (character with ASCII code 45), '>' (character with ASCII code 62 ), '('and ')'. characters '-' and '> 'can occur in an expression only supported red like that: (->) and represent implication. the total number of logical arguments (I. e. digits '0' and '1') in the expression must be equalN. The order in which the digits follow in the expression from left to right must coincideA1, bytes,A2, middle..., middle ,...,AN.
The expression shoshould be correct. More formally, a correct expression is determined as follows:
Expressions 0, 1 (without the quotes) are correct. If
V
1,V2 are correct, thenV1->V2 is a correct expression. IfVIs a correct expression, then (VIs a correct expression.The total number of characters in the resulting expression mustn't exceed 106.
If there are multiple possible answers, you are allowed to print any of them.
Sample test (s) Input40 1 1 0
OutputYES(((0)->1)->(1->0))
Input21 1
OutputNO
Input10
OutputYES0
Question: give four transfer formulas, and then give a 0/1 string to ask if there is a certain calculation method to make the final answer 0. If so, output any legal method.
Question Analysis: from the four formulas given, we can find that if the result is 0, the last digit must be 0. Now we need to construct 1 before the last 0, we can find that if the first of the last 0 is 1, no matter what the last answer is 1, because 0-> 1 =-> 1 = 1, that is, it has nothing to do with the previous value, so we will consider the case that it is impossible to consider that the first of the last 0 is 0, because 1 is required, and 0-> 0 =-> 0 = 0 that is to say, the front of the 0 cannot be 1, and so on. If the front of the last 0 is 1, there is no solution, otherwise there will be a solution
# Include
# Include
Int const MAX = 1e6 + 5; int n, a [MAX]; int main () {scanf (% d, & n); for (int I = 1; I <= n; I ++) scanf (% d, & a [I]); if (n = 1) // specify a number {if (a [1] = 1) printf (NO); else printf (YES0); return 0 ;} if (a [n] = 1) {printf (NO); return 0;} bool flag = false; for (int I = 1; I <= n-2; I ++) {if (a [I]! = 1) {flag = true; break;} if (! Flag & a [n-1] = 0 & a [n] = 0) // 1111111 1 0Y 0 0N {printf (NO); return 0 ;} printf (YES); for (int I = 1; I <= n-2; I ++) printf (% d->, a [I]); printf (% d, a [n-1]); for (int I = 1; I <= n-2; I ++) printf ()); printf (-> 0 );}
D is also a constructor.