Codeforces Round #306 (Div. 2) (ABCE ),

Match link: http://codeforces.com/contest/550

A. Two Substringstime limit per test2 secondsmemory limit per test256 megabytes

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 ).

Input

The only line of input contains a string*S*Of length between 1 and 10^{5 consisting of uppercase Latin letters.}

Output

Print "YES" (without the quotes), if string*S*Contains 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 you can find two substrings that do not overlap with each other: "AB" and "BA"

Question Analysis: 4 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 example: ABACCCAB

#include <cstdio>#include <cstring>#include <algorithm>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\n");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\n");return 0;}printf("NO\n");}

B. Preparing Olympus iadtime limit per test2 secondsmemory limit per test256 megabytes

You have*N*Problems. You have estimated the difficulty of*I*-Th one as integer*C*_{I. 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.

Input

The first line contains four integers*N*,*L*,*R*,*X*(1 digit ≤ Digit*N*Limit ≤ limit 15, 1 limit ≤ limit*L*Limit ≤ limit*R*Limit ≤ limit 10^{9, 1 ≤ bytesXLimit ≤ 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 contains*N*Integers*C*_{1, 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 <cstdio> # include <cstring> # include <algorithm> 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) // specifies the number of questions. The maximum and minimum values are currently supported, sum of 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 \ n", ans );}

C. Divisibility by Eighttime limit per test2 secondsmemory limit per test256 megabytes

You are given a non-negative integer*N*, 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 integer*N*. The representation of number*N*Doesn't contain any leading zeroes and its length doesn't exceed100 digits.

Output

Print "NO" (without quotes), if there is no such way to remove some digits from number*N*.

Otherwise, print "YES" in the first line and the resulting number after removing digits from number*N*In the second line. The printed number must be divisible by8.

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 <cstdio>#include <cstring>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\n%c%c\n", 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\n%c%c%c\n", 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("YES\n0\n");return 0;}if(s[i] == '8'){printf("YES\n8\n");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\n");}

E. Brackets in Implicationstime limit per test: 2 secondsmemory 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 integer*N*(1 digit ≤ Digit*N*Limit ≤ limit 100 limit 000)-the number of arguments in a logical expression.

The second line contains*N*Numbers*A*_{1, 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 equal*N*. The order in which the digits follow in the expression from left to right must coincide*A*_{1, 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.}
- If
*V*Is a correct expression, then (*V*Is a correct expression.

The total number of characters in the resulting expression mustn't exceed10^{6.}

If there are multiple possible answers, you are allowed to print any of them.

Sample test (s) Input

40 1 1 0

Output

YES(((0)->1)->(1->0))

Input

21 1

Output

NO

Input

10

Output

YES0

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 <cstdio> # include <cstring> 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 \ n "); else printf ("YES \ n0 \ n"); return 0;} if (a [n] = 1) {printf ("NO \ n"); 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 \ n "); return 0;} printf ("YES \ n"); 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 \ n ");}

D is also a constructor.