Codeforces Round #302 (Div. 2) (ABCD ),

Source: Internet
Author: User

Codeforces Round #302 (Div. 2) (ABCD ),

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


A. Set of Stringstime limit per test: 1 secondmemory limit per test: 256 megabytes

You are given a stringQ. A sequenceKStringsS1, bytes,S2, middle..., middle ,...,SKIs calledbeautiful, if the concatenation of these strings is stringQ(Formally,S1 worker + workerS2 cores + cores... cores + CoresSKSignature = SignatureQ) And the first characters of these strings are distinct.

Find any beautiful sequence of strings or determine that thebeautiful sequence doesn't exist.

Input

The first line contains a positive integerK(1 digit ≤ DigitKLimit ≤ limit 26)-the number of strings that shoshould be in abeautiful sequence.

The second line contains stringQ, Consisting of lowercase Latin letters. The length of the string is within range from1 to 100, inclusive.

Output

If such sequence doesn't exist, then print in a single line "NO" (without the quotes ). otherwise, print in the first line "YES" (without the quotes) and in the nextKLines print the beautiful sequence of stringsS1, bytes,S2, middle..., middle ,...,SK.

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

Sample test (s) Input
1abca
Output
YESabca
Input
2aaacas
Output
YESaaacas
Input
4abc
Output
NO
Note

In the second sample there are two possible answers :{"Aaaca", Role ","S"} And {"Aaa", Role ","Cas"}.

A given parent string is divided into k substrings. The sum of k substrings and the first character of k substrings cannot be the same.

Question Analysis: Mark the occurrence of the first character and count the number of parts.


#include <cstdio>#include <iostream>#include <string>#include <cstring>using namespace std;int main(){int k;cin >> k;string s;cin >> s;string ans[30];int cnt = 1;int len = s.length();bool hash[400];memset(hash, false, sizeof(hash));bool flag = false;for(int i = 0; i < len; i++){if(!hash[s[i]]){hash[s[i]] = true;ans[cnt ++] += s[i];}else if(hash[s[i]] || s[i] == s[i - 1])ans[cnt - 1] += s[i];if(cnt == k + 1){for(int j = i + 1; j < len; j++)ans[cnt - 1] += s[j];flag = true;break;}}if(!flag)printf("NO\n");else{printf("YES\n");for(int i = 1; i < cnt; i++)cout << ans[i] << endl;}}



B. Sea and Islandstime limit per test: 1 secondmemory limit per test: 256 megabytes

A map of some object is a rectangular field consistingNRows andNColumns. Each cell is initially occupied by the sea but you can cover some cells of the map with sand so that exactlyKIslands appear on the map. we will call a set of sand cells to beisland if it is possible to get from each of them to each of them by moving only through sand cells and by moving from a cell only to a side -adjacent cell. the cells are called to be side-adjacent if they share a vertical or horizontal side. it is easy to see that islands do not share cells (otherwise they together form a bigger island ).

Find a way to cover some cells with sand so that exactlyKIslands appear onNLimit × limitNMap, or determine that no such way exists.

Input

The single line contains two positive integersN,K(1 digit ≤ DigitNLimit ≤ limit 100,0 limit ≤ limitKLimit ≤ limitN2)-the size of the map and the number of islands you shoshould form.

Output

If the answer doesn't exist, print "NO" (without the quotes) in a single line.

Otherwise, print "YES" in the first line. In the nextNLines print the description of the map. each of the lines of the description must consist only of characters's 'and 'l ', where's 'is a cell that is occupied by the sea and 'L' is the cell covered with sand. the length of each line of the description must equalN.

If there are multiple answers, you may print any of them.

You shoshould not maximize the sizes of islands.

Sample test (s) Input
5 2
Output
YESSSSSSLLLLLSSSSSLLLLLSSSSS
Input
5 25
Output
NO

Divide a region into k island outputs. The island is a connected block in four directions.

Question Analysis: Fill in one island and fill in k islands. Note that the number of most likely islands is (n * n + 1)/2.


#include <cstdio>int main() {int n, k;scanf("%d %d", &n, &k);if((n * n + 1) / 2 < k)printf("NO\n");else{printf("YES\n");for(int i = 1; i <= n; i++){for(int j = 1; j <= n; j++){ if((i + j) % 2 == 0 && k > 0) {printf("L");k --;}else printf("S");}printf("\n");}}}



C. Writing Codetime limit per test: 3 secondsmemory limit per test: 256 megabytes

Programmers working on a large project have just completed ed a task to write exactly MLines of code. There are NProgrammers working on a project, I-Th of them makes exactly A IBugs in every line of code that he writes.

Let's call a sequence of non-negative integersV1, bytes,V2, middle..., middle ,...,VNAplan, ifV1 worker + workerV2 cores + cores... cores + CoresVNSignature = SignatureM. The programmers follow the plan like that: in the beginning the first programmer writes the firstV1 lines of the given task, then the second programmer writesV2 more lines of the given task, and so on. in the end, the last programmer writes the remaining lines of the code. let's call a plangood, if all the written lines of the task contain at mostBBugs in total.

Your task is to determine how many distinct good plans are there. As the number of plans can be large, print the remainder of this number modulo given positive integerMod.

Input

The first line contains four integersN,M,B,Mod(1 digit ≤ DigitN, Bytes,MLimit ≤ 500,0 limit ≤ limitBLimit ≤ limit 500; 1 limit ≤ limitModLimit ≤ limit 109 seconds + 7)-the number of programmers, the number of lines of code in the task, the maximum total number of bugs respectively and the modulo you should use when printing the answer.

The next line containsNSpace-separated integersA1, bytes,A2, middle..., middle ,...,AN(0 bytes ≤ bytesAILimit ≤ limit 500)-the number of bugs per line for each programmer.

Output

Print a single integer-the answer to the problem moduloMod.

Sample test (s) Input
3 3 3 1001 1 1
Output
10
Input
3 6 5 10000000071 2 3
Output
0
Input
3 5 6 111 2 1
Output
0


N people have to write a total of m-line programs. Each programmer has a number of bugs on each line, which requires the number of bugs in the whole program to be no more than the number of B's solutions.

Problem Analysis: It turns out that there are no more than multiple backpacks. dp [I] [j] indicates the number of solutions with j bugs in the first line of I, and then the number of backpacks.


#include <cstdio>#include <cstring>int const MAX = 505;int a[MAX], dp[MAX][MAX];int main(){    int n, m, b, MOD;    scanf("%d %d %d %d", &n, &m, &b, &MOD);    for(int i = 1; i <= n; i++)     scanf("%d", &a[i]);       dp[0][0] = 1;    for(int i = 1; i <= n; i++)        for(int j = 1; j <= m; j++)            for(int k = a[i]; k <= b; k++)             dp[j][k] = (dp[j][k] + dp[j - 1][k - a[i]]) % MOD;    int ans = 0;    for(int i = 0; i <= b; i++)     ans = (ans + dp[m][i]) % MOD;    printf("%d\n", ans);}



D. Destroying Roadstime limit per test: 2 secondsmemory limit per test: 256 megabytes

In some country there are exactlyNCities andMBidirectional roads connecting the cities. Cities are numbered with integers from1N. If citiesAAndBAre connected by a road, then in an hour you can go along this road either from cityATo cityB, Or from cityBTo cityA. The road network is such that from any city you can get to any other one by moving along the roads.

You want to destroy the largest possible number of roads in the country so that the remaining roads wocould allow you to get from cityS1 to cityT1 in at mostL1 hours and get from cityS2 to cityT2 in at mostL2 hours.

Determine what maximum number of roads you need to destroy in order to meet the condition of your plan. If it is impossible to reach the desired result, print-1.

Input

The first line contains two integersN,M(1 digit ≤ DigitNLimit ≤ limit 3000,)-the number of cities and roads in the country, respectively.

NextMLines contain the descriptions of the roads as pairs of integersAI,BI(1 digit ≤ DigitAI, Bytes,BILimit ≤ limitN,AI  =BI). It is guaranteed that the roads that are given in the description can transport you from any city to any other one. it is guaranteed that each pair of cities has at most one road between them.

The last two lines contains three integers each,S1,T1,L1 andS2,T2,L2, respectively (1 limit ≤ limitSI, Bytes,TILimit ≤ limitN, 0 bytes ≤ bytesLILimit ≤ limitN).

Output

Print a single number-the answer to the problem. If the it is impossible to meet the conditions, print-1.

Sample test (s) Input
5 41 22 33 44 51 3 23 5 2
Output
0
Input
5 41 22 33 44 51 3 22 4 2
Output
1
Input
5 41 22 33 44 51 3 23 5 1
Output
-1

N vertices and m edges in an undirected graph. Remove the most edges so that the time from s1 to t1 does not exceed l1 and the time from s2 to t2 does not exceed l2.

Question Analysis: Considering the range of n and the edge weight is fixed to 1, use bfs to find the shortest short circuit between any two points, then, enumerate the edges from s1 to t1 and s2 to t2 (other edges not related to them must be deleted directly) and obtain the minimum value.


#include <cstdio>#include <cstring>#include <vector>#include <queue>#include <algorithm>using namespace std;int const MAX = 3005;vector <int> vt[MAX];int dis[MAX][MAX];bool vis[MAX];int n, m;void BFS(){memset(dis, 0, sizeof(dis));for(int i = 1; i <= n; i++){memset(vis, false, sizeof(vis));queue <int> q;vis[i] = true;q.push(i);while(!q.empty()){int u = q.front();q.pop();int sz = vt[u].size();for(int j = 0; j < sz; j++){int v = vt[u][j];if(!vis[v]){vis[v] = true;dis[i][v] = dis[i][u] + 1;q.push(v);}}}}}int main(){scanf("%d %d", &n, &m);for(int i = 0; i < m; i++){int x, y;scanf("%d %d", &x, &y);vt[x].push_back(y);vt[y].push_back(x);}int s1, t1, l1, s2, t2, l2;scanf("%d %d %d %d %d %d", &s1, &t1, &l1, &s2, &t2, &l2);BFS();if(!(dis[s1][t1] <= l1 && dis[s2][t2] <= l2)){printf("-1\n");return 0;}int tmp = dis[s1][t1] + dis[s2][t2];for(int i = 1; i <= n; i++)for(int j = 1; j <= n; j++){if(dis[s1][i] + dis[i][j] + dis[j][t1] <= l1 && dis[s2][i] + dis[i][j] + dis[j][t2] <= l2)tmp = min(tmp, dis[s1][i] + dis[i][j] + dis[j][t1] + dis[s2][i] + dis[j][t2]);if(dis[s1][i] + dis[i][j] + dis[j][t1] <= l1 && dis[t2][i] + dis[i][j] + dis[i][s2] <= l2)tmp = min(tmp, dis[s1][i] + dis[i][j] + dis[j][t1] + dis[t2][i] + dis[i][s2]);if(dis[t1][i] + dis[i][j] + dis[j][s1] <= l1 && dis[s2][i] + dis[i][j] + dis[j][t2] <= l2)tmp = min(tmp, dis[t1][i] + dis[i][j] + dis[j][s1] + dis[s2][i] + dis[j][t2]);if(dis[t1][i] + dis[i][j] + dis[j][s1] <= l1 && dis[t2][i] + dis[i][j] + dis[j][s2] <= l2)tmp = min(tmp, dis[t1][i] + dis[i][j] + dis[j][s1] + dis[t2][i] + dis[j][s2]);}printf("%d\n", m - tmp);}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.