1. 程式人生 > >[ACM]Codeforces Round #534 (Div. 2)

[ACM]Codeforces Round #534 (Div. 2)

每一個 truct delet ike any all 調整 p s out

一、前言 二、題面

A. Splitting into digits

Vasya has his favourite number n. He wants to split it to some non-zero digits. It means, that he wants to choose some digits d1,d2,…,dk, such that 1≤di≤9 for all i and d1+d2+…+dk=n.

Vasya likes beauty in everything, so he wants to find any solution with the minimal possible number of different digits among d1,d2,…,dk. Help him!

Input
The first line contains a single integer n — the number that Vasya wants to split (1≤n≤1000).

Output
In the first line print one integer k — the number of digits in the partition. Note that k must satisfy the inequality 1≤k≤n. In the next line print k digits d1,d2,…,dk separated by spaces. All digits must satisfy the inequalities 1≤di≤9.

You should find a partition of n in which the number of different digits among d1,d2,…,dk will be minimal possible among all partitions of n into non-zero digits. Among such partitions, it is allowed to find any. It is guaranteed that there exists at least one partition of the number n into digits.

Examples
input
1
output
1
1

input
4
output
2
2 2

input
27
output
3
9 9 9

Note
In the first test, the number 1 can be divided into 1 digit equal to 1.

In the second test, there are 3 partitions of the number 4 into digits in which the number of different digits is 1. This partitions are [1,1,1,1], [2,2] and [4]. Any of these partitions can be found. And, for example, dividing the number 4 to the digits [1,1,2] isn‘t an answer, because it has 2 different digits, that isn‘t the minimum possible number.

題意:嘗試用k個數字(0~9)加起來變成n,要求數字種類盡可能少,即能全部用相同的數字時就不用不同的數字。

正解:cf日常無聊題。第一行輸出n,第二行輸出n個1即可。

代碼:略

B. Game with string

Two people are playing a game with a string s, consisting of lowercase latin letters.

On a player‘s turn, he should choose two consecutive equal letters in the string and delete them.

For example, if the string is equal to "xaax" than there is only one possible turn: delete "aa", so the string will become "xx". A player not able to make a turn loses.

Your task is to determine which player will win if both play optimally.

Input
The only line contains the string s, consisting of lowercase latin letters (1≤|s|≤100000), where |s| means the length of a string s.

Output
If the first player wins, print "Yes". If the second player wins, print "No".

Examples
input
abacaba

output
No

input
iiq

output
Yes

input
abba

output
No

Note
In the first example the first player is unable to make a turn, so he loses.

In the second example first player turns the string into "q", then second player is unable to move, so he loses.

題意:每次從給出的字符串刪去兩個相連且相同的字符,刪去個數為奇數時輸出Yes,偶數輸出No。

思路:掃描字符串,找到相連的相同字符後,左端點向左,右端點向右進行搜索,直至左右端點不是相同字符,結束,繼續掃描,直至字符串末尾。註意,刪除子串後應將刪除部分的左端和右端相連,以保證後面的掃描的正確性。解決這個問題,我們可以先對字符串進行鏈標記,對字符進行串聯,即對每一個字符標記一個左字符和右字符,初始即其左右的臨近字符,在每次操作後可將該標記進行調整。

代碼:

 1 #include <bits/stdc++.h>
 2 #define MAXN 100005
 3 
 4 char ch[MAXN];
 5 int len, tot;
 6 
 7 struct node {
 8     char s;
 9     int l, r;
10 } a[MAXN];
11 .
12 int chk(int o) {
13     int l = o, r = o + 1; tot++;
14     while (a[a[l].l].s == a[a[r].r].s && a[a[l].l].s != \000) 
15         l = a[l].l, r = a[r].r, tot++;
16     a[a[r].r].l = a[l].l;
17     return a[r].r;
18 }
19 
20 int main() {
21     scanf("%s", ch + 1), len = strlen(ch + 1);
22     for (int i = 1; i <= len; i++) a[i].s = ch[i], a[i].l = i - 1, a[i].r = i + 1;
23     for (int i = 1; i <= len; i = (a[i].s == a[i + 1].s && a[i].s != \000) ? chk(i) : i + 1);
24     printf(tot % 2 ? "Yes" : "No");
25     return 0;
26 }

[ACM]Codeforces Round #534 (Div. 2)