[BestCoder] Round #11
Only one output case is true.
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Define rd (x) scanf (% d, & x) # define rd2 (x, y) scanf (% d, & x, & y) # define rd3 (x, y, z) scanf (% d, & x, & y, & z) using namespace std; typedef long ll; const int inf = 0x3f3f3f; int dx [8] = {,-,-1,-1}; int dy [8] = {1, 0,-,-}; // The key point is the vertex int n, m, x, y; int main () {while (cin> n> m> x> y) {if (2 * x = n & 2 * y = m) cout <
1002
Http://acm.hdu.edu.cn/showproblem.php? Pid = 1, 5055
N numbers (0 ~ 9). The n numbers must be used to form a number. A leading 0 is required and the last digit must be an odd number. Output-1 if it cannot be composed.
We can construct a number: there is only one number, and the number is an odd number; there are at least two numbers of n numbers greater than 0, and there are odd numbers in n.
If yes, then the smallest odd number is the last digit of the number, and the remaining number comes first in the descending order.
When writing, the logic is a little messy... when writing such questions, you must consider them comprehensively.
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Define rd (x) scanf (% d, & x) # define rd2 (x, y) scanf (% d, & x, & y) # define rd3 (x, y, z) scanf (% d, & x, & y, & z) using namespace std; typedef long ll; const int inf = 0x3f3f3f; int dx [8] = {,-,-1,-1}; int dy [8] = {1, 0,-102,-}; const int maxn =; int num [maxn]; bool hasodd; int minodd; int n; bool cmp (int a, int B) {return a> B;} int main () {while (rd (n )! = EOF) {hasodd = 0; minodd = 100; // the smallest odd int p =-1; // the smallest odd position int cnt = 0; // The number greater than 0 for (int I = 1; I <= n; I ++) {rd (num [I]); if (num [I]> 0) cnt ++; if (num [I] & 1) {hasodd = 1; if (minodd> num [I]) {minodd = num [I]; p = I ;}} if (n = 1) {if (hasodd) cout <
= 2 & hasodd) {swap (num [p], num [n]); sort (num + 1, num + n, cmp); for (int I = 1; I <= n; I ++) cout <
1003
A string consisting of lower-case letters is given, and then a number k is given to ask how many substrings meet the requirements of the same letters in the substring to appear no more than k. For example, if there is a substring abac k = 2, the number of times of a = 2 cannot exceed k, and the number of times of B and c is 1 or greater than k, therefore, this substring meets the requirements.
A string with a length of n. a substring contains n (n + 1)/2. n is 100000 in the question, and direct enumeration is definitely not acceptable.
In the beginning, we also want to enumerate each position and determine the number of substrings starting with this position. There are two repeated loops, and the second loop is the length, at first, I thought that the maximum length was 26 (26 letters). The complexity was okay, but I forgot about k... k is not necessarily equal to 1. It may be very large .... so tragic, decisive timeout ..
We can see that the problem-solving report also enumerates each position. statistics are based on the maximum length of the substring ending with this position, and the number of all substrings ending with this position. This is easy to understand, for example, k = 2, bcca. When the position is a, the maximum substring meeting the condition is bcca and the length is 4. Then the number of all substrings is 4, bcca ca. Open up two pointers. One is the enumeration position, and the other is the starting position of the largest string meeting the position. The processing is the first position of the string. Map
Mp: record the number of times each letter appears. The following is an example to illustrate how it works. The explanation is unclear .....
For example, string abbcbcc k = 2
The starting position is 1, 'A', the starting pointer start is 1, and mp ['a'] ++. The value must be <= k, ans + = I-start + 1; that is, ans + = 1, and the corresponding string is
Then the position is 2, 'B', and the starting pointer start is 1. mp ['B'] ++ meets the conditions. ans + = 2-start + 1; that is, ans + = 2. The corresponding string is B AB.
The location is 3 and the condition is also met. ans + = 3, and the corresponding string is B bb abb.
Later, the location is 4 and the condition is also met. ans + = 4; the corresponding string is c bc bbc abbc.
Then the location is 5, mp ['B'] ++. At this time, mp [B] = 3, 3> k does not meet the conditions, because the letter "B" is added, to end with this position, you must remove B from the front and require the maximum string. Therefore, remove B from the front and update the start position. At this time, the start position is still 1, to meet the condition, move the pointer, mp ['a'] --, which is the position corresponding to the start pointer, start ++. At this time, start = 2, the corresponding letter is B. Find the first B and remove it. So run mp ['B'] --, start ++ again. At this time, start = 3, the answer is correct. The maximum string is bcb, ans + = 5-start + 1, that is, ans + = 3, and the corresponding substring is B cb bcb.
Later, similarly ....
From the above process, we can see that the start pointer is always forward, which is more efficient than the one mentioned above. The number of substrings that meet some positions can be calculated as 0 (1 ).
#include
#include
#include #include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define rd(x) scanf(%d,&x)#define rd2(x,y) scanf(%d%d,&x,&y)#define rd3(x,y,z) scanf(%d%d%d,&x,&y,&z)using namespace std;typedef long long ll;const int inf=0x3f3f3f3f;int dx[8]={0,0,-1,1,1,1,-1,-1};int dy[8]={1,-1,0,0,-1,1,-1,1};char s[100004];map
mp;int len;int k;ll ans;int main(){ int t;rd(t); while(t--) { mp.clear(); scanf(%s,s+1); rd(k); len=strlen(s+1); int start=1; ans=0; for(int i=1;i<=len;i++) { mp[s[i]]++; if(mp[s[i]]>k) { while(s[start]!=s[i]) { mp[s[start]]--; start++; } mp[s[start]]--; start++; } ans+=i-start+1; } printf(%I64d,ans); } return 0;}