The Alphabet Sticker
When we were kids, we used to play with some stickers where these stickers contain some (but not necessarily all) lower case English alphabet letters.
Each sticker contains some letters arranged in a single row, where all occurrences of the same letter are adjacent to each other. A sticker can be represented as a string of characters, for example the following are valid stickers’ representations: “aabcc”, “ccccab” and “mmaw”. And the following are not valid (because not all occurrences of the same letter are adjacent to each other): “abacc”, “cccabc” and “mawm”.
Now we found some stickers with some missing letters, but we are sure that all missing letters belong to the visible letters set (that is, for every missing letter, there is at least one visible letter that matches the missing one). In this problem a question mark letter represents a missing letter. Given some stickers’ representations with zero or more missing letters, your task is to count the number of possible original configurations for each sticker.
For example, this sticker “aa??bb” with missing letters could have been one of the following original stickers“aaaabb”, “aaabbb” or “aabbbb”. But it could not have been any of the following original stickers “aababb”(it is invalid sticker) and “aaccbb” (because the letter ‘c’ did not appear in the given configuration).
Input
Your program will be tested on one or more test cases. The first line of the input will be a single integer T, the number of test cases (1 ≤ T ≤ 100). Followed by the test cases, each test case is described in one line which contains a non-empty string which consists of up to 10,000 letters, each letter is either a lowercase English letter (from ‘a’ to ‘z’) or a question mark (‘?’). This string represents a sticker configuration which contains zero or more question marks, it will also contain at least one letter which is not a question mark and there will be at least one valid original configuration for it.
Output
For each test case, print a single line which contains a single integer representing the number of possible original configurations for the sticker, since the result may be very large, print it modulo 1,000,000,007(10^9 + 7).
输出时每行末尾的多余空格,不影响答案正确性
样例输入复制
4 aa??bb aaccbb ?a? a??a
样例输出复制
3 1 1 1
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
E. Balloons Colors
Assigning a balloon color to each problem is one of the tasks we need to do every year, and sometimes itis tricky.
We noticed that some contestants assume some colors for some problems according to the difficulty. Forexample, the easiest problem is the red one and the hardest problem is the black one.
We do not want these assumptions to be true, so we decided to add constraints for the easiest and thehardest problems.
There are N problems, numbered from 1 to N , the easiest problem is problem number 1, and the hardestproblem is problem number N . Also there are N unique colors, for simplicity we will give each color aunique number from 1 to N .
We want to assign each color to exactly 1 problem, such that the easiest problem does not get the colorX and the hardest problem does not get the color Y .
Given N , X , Y and an assignment of the colors, your task is to find if this assignment satisfies the aboveconditions or not.
Input
Your program will be tested on one or more test cases. The first line of the input will be a single integer T,the number of test cases (1 ≤ T ≤ 100). Followed by the test cases, the first line of each test case contains3 integers separated by a single space N X Y (3 ≤ N ≤ 100) and (1 ≤ X , Y ≤ N ) representing thenumber of problems, the color which the easiest problem should not get and the color which the hardestproblem should not get, respectively. Followed by a line which contains N integers separated by a singlespace (each integer from 1 to N should appear exactly once), the first integer is the color for the firstproblem (the easiest), the second integer is the color for the second problem and so on (the last integer isthe color for the hardest problem).
Output
For each test case, print a single line which contains a single word, this word should be (without thequotes):
- “BOTH”: If both the easiest and hardest problems got colors which they should not get.
- “EASY”: If only the easiest problem got a color which it should not get.
- “HARD”: If only the hardest problem got a color which it should not get.
- “OKAY”: If both the easiest and hardest problems got colors which they can get.
输出时每行末尾的多余空格,不影响答案正确性
样例输入复制
4 3 1 2 1 3 2 5 3 4 3 1 2 4 5 6 1 6 2 1 3 4 5 6 7 7 7 1 7 2 3 4 5 6
样例输出复制
BOTH EASY HARD OKAY
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
F. NASSA's Robot
NASSA’s robot landed on Mars. The place where it landed can be modeled as an infinite 2-dimensionalplane with perpendicular X-axis and Y-axis coordinates.
The robot continuously reports its location back to Earth, but due to a serious design flaw, it only reportsthe moves it makes instead of the coordinates of its exact location. Some signals went missing and neverreached our reception.
In one of the space exploration missions, the robot sent a sequence of signals, which can be representedby a string composed of the following characters: ‘U’, ‘R’, ‘D’, ‘L’ or ‘?’. ‘U’ represents up (Y-coordinateincreases by 1), ‘R’ represents right (X-coordinate increases by 1), ‘D’ represents down (Y-coordinatedecreases by 1), ‘L’ represents left (X-coordinate decreases by 1) and ‘?’ represents a missed signal. Everycharacter in the sequence is a single step in the corresponding direction. A missed signal is a single stepin one of the four directions. The robot is initially at X-coordinate 0 and Y-coordinate 0 before startingto send the given signals.
After sending some signals while the robot is moving, its software crashed and the robot could not do anyfurther moves. The researchers on the base want to limit the space where they can look for the robot. Inother words, they want to find the minimum possible X-coordinate, the minimum possible Y-coordinate,the maximum possible X-coordinate and the maximum possible Y-coordinate of the current location ofthe robot.
Input
Your program will be tested on one or more test cases. The first line of the input will be a single integerT, the number of test cases (1 ≤ T ≤ 100). Followed by the test cases, each test case is described in oneline which contains a non-empty string which consists of up to 100,000 letters, each letter is ‘U’, ‘R’, ‘D’,‘L’ or ‘?’. This string represents the sequence of signals as described above.
Output
For each test case, print a single line which contains 4 integers separated by a single space, which are theminimum possible X-coordinate, the minimum possible Y-coordinate, the maximum possible X-coordinateand the maximum possible Y-coordinate for the location of the robot after it stopped moving.
输出时每行末尾的多余空格,不影响答案正确性
样例输入复制
3 RUL?R?D ???????? RRRUU
样例输出复制
-1 -2 3 2 -8 -8 8 8 3 2 3 2
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
G. The Stones Game
The stones game is a simple game, it is also a very old game which is unknown to almost everyone.
The game starts with N stones and M players, the players are numbered from 1 to M. The players playin turns, player number 1 plays first, then player number 2 and so on until player number M plays, afterthis player number 1 plays again and they keep playing until the end of the game.
For each turn, the players do the following 2 steps:
1. The player gets a chance to remove a stone, and he/she should remove a stone in this step if he/shedecided to do so.
2. Regardless of the decision of the current player (whether or not he/she removed a stone in the firststep), if this is not the first turn and in the previous turn the player decided not to remove a stonein his/her first step, then the current player must remove a stone in this step (if in the previous turnthe player decided to remove a stone in his/her first step, then the current player must not removea stone in this step).
This means in some turns a player might remove 0, 1 or 2 stones according to the above rules. In thisgame, the player who removes the last stone wins the game.
Now you are given the total number of stones, the total number of players and a player number and youare asked to answer the following question:
Is there a strategy for this player to win the game regardless of the actions taken by the other players intheir turns?
Input
Your program will be tested on one or more test cases. The first line of the input will be a single integerT, the number of test cases (1 ≤ T ≤ 100). Followed by the test cases, each test case is described in oneline which contains 3 integers separated by a single space N M X (1 ≤ N , M ≤ 10^9) and (1 ≤ X ≤M) representing the number of stones, the number of players and the player number, respectively.
Output
For each test case, print a single line which contains a single word, this word is either “YES” or “NO”(without the quotes) representing the answer for the above question for the given player number.
输出时每行末尾的多余空格,不影响答案正确性
样例输入复制
2 2 2 2 2 2 1
样例输出复制
YES NO
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
I. Omar Loves Candies
Omar loves to eat a lot of candies, but unfortunately most of the candies are not healthy. So his parentsfound a way to give each candy a score, a higher score means a healthier candy (the score is an integerthat can be positive, zero or negative).
One day he went with his parents to buy some candies, and they found a strange store where all the candiesare stored in a 2-dimensional grid of N rows with M candies in each row. The rows are numbered from1 to N from top to bottom, and the columns are numbered from 1 to M from left to right and every cellcontains one candy.
They noticed something else, any candy (except for those in the first row) is healthier than the candywhich is exactly above it, and any candy (except for those in the first column) is healthier than the candywhich is exactly to its left (healthier means having higher score as defined above).
There is one more strange thing about this store, to buy some candies you have to select a sub-rectangleof the candies’ grid and buy all the candies within this sub-rectangle.
Omar’s parents want to select a non-empty sub-rectangle that has the maximum sum of candies’ scoresamong all possible sub-rectangles.
For example, consider the grid in the example input. Some of the possible sub-rectangles of candies theycan select are [-2, -1, 2, 3], [-4, -2, -1] or [2, 3, 4, 5]. The last sub-rectangle has the maximum sum ofscores, which is 14. They can not select the following lists of candies [1, 2, 3, 4, 5] or [-2, -1, 2] (becausethese lists do not form a sub-rectangle of the given grid).
Can you help them by writing a program which finds the non-empty sub-rectangle with the maximumpossible sum of scores in the given grid?
Input
Your program will be tested on one or more test cases. The first line of the input will be a single integerT, the number of test cases (1 ≤ T ≤ 100). Followed by the test cases, each test case starts with aline containing two integers separated by a single space N M (1 ≤ N , M ≤ 1,000) representing thedimensions of the candies’ grid, followed by N lines, each one contains M integers separated by a singlespace, representing the candies’ scores in this row. The given grid representation will satisfy the conditionsmentioned above, and each integer in the grid will not be less than -2,000 and will not be greater than2,000.
Output
For each test case, print a single line which contains a single integer representing the maximum sum ofscores they can get from a non-empty sub-rectangle.
输出时每行末尾的多余空格,不影响答案正确性
样例输入复制
1 3 3 -4 -2 -1 -3 2 3 1 4 5
样例输出复制
14
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include