A. Uniform String

题意:给出

Codeforces Round #527 (Div. 3)A B C D1 D2_#include

,输出长度为

Codeforces Round #527 (Div. 3)A B C D1 D2_#include_02

满足循环节的字符串。比如

Codeforces Round #527 (Div. 3)A B C D1 D2_codeforces_03

,就是

Codeforces Round #527 (Div. 3)A B C D1 D2_#ifndef_04

,不足循环节长度的也要输出。

代码

#include<bits/stdc++.h>

using namespace std;

int main()
{
#ifndef ONLINE_JUDGE
freopen("input.in","r",stdin);
#endif
int T, n, k;
cin >> T;
while(T--) {
cin >> n >> k;
int cnt = n / k, t = 0;
if(n % k != 0)
t = n % k;
for(int i = 0; i < k; ++i) {
if(i == k - 1)
cnt += t;
for(int j = 0; j < cnt; ++j) {
putchar(i + 'a');
}
}
puts("");
}
return 0;
}

B. Teams Forming

题意:给你一个数量为偶数的数列,问花费多少让他们两两成对差值最少。
题解:排序后,相邻两项做差明显是花费最少的。

代码

#include<bits/stdc++.h>

using namespace std;

int main()
{
#ifndef ONLINE_JUDGE
freopen("input.in","r",stdin);
#endif
int n, a[200] = {0}, book[200] = {0};
cin >> n;
for(int i = 0; i < n; ++i)
cin >> a[i];
sort(a, a + n);
int sum = 0;
for(int i = 1; i < n; i += 2) {
sum += a[i] - a[i - 1];
}
cout << sum << endl;
return 0;
}

C. Prefixes and Suffixes

题意:不按顺序给出

Codeforces Round #527 (Div. 3)A B C D1 D2_#include_05

个长度为

Codeforces Round #527 (Div. 3)A B C D1 D2_c++_06


Codeforces Round #527 (Div. 3)A B C D1 D2_c++_07

的前缀和后缀子串,最后问你哪些是前缀哪些是后缀。

题解:由题可知,每个长度都有2个串,所以我们先从长度为

Codeforces Round #527 (Div. 3)A B C D1 D2_c++_07

的子串假设其中一个为前缀,然后去根据长度枚举其它子串看是否满足你当前选择的这个最长前缀和最长后缀。如果当前情况不满足,就去交换一下再去枚举。

代码

#include<bits/stdc++.h>

using namespace std;
int n;
string s[202];
char ans[204];

bool solve(string pre,string suf)
{
memset(ans, 0, sizeof ans);
for(int i = 1; i <= n; ++i) {
bool f = 1;
for(int j = 0; j < 2 * n - 2; ++j) {
if(i == s[j].size()) {
if(pre.substr(0,i) == s[j] && f) {
ans[j] = 'P';
f = 0;
}else if(suf.substr(n - i - 1, i) == s[j]){
ans[j] = 'S';
}
if(!ans[j]) return 0;
}
}
}
return 1;
}

int main()
{
#ifndef ONLINE_JUDGE
freopen("input.in","r",stdin);
#endif
cin >> n;
string suf,pre;
for(int i = 0; i < 2 * n - 2; ++i) {
cin >> s[i];
if(s[i].size() == n - 1) {
if(!pre.size()) {
pre = s[i];
}else{
suf = s[i];
}
}
}
if(solve(suf,pre)) {
puts(ans);
}else if(solve(pre,suf)){
puts(ans);
}

return 0;
}

D1. Great Vova Wall (Version 1)

题意:给

Codeforces Round #527 (Div. 3)A B C D1 D2_#include_02

列瓦片,有

Codeforces Round #527 (Div. 3)A B C D1 D2_#include_10


Codeforces Round #527 (Div. 3)A B C D1 D2_#ifndef_11

的瓦片可以用,问最后能否拼成

Codeforces Round #527 (Div. 3)A B C D1 D2_#ifndef_12


题解:只要看相邻两列的数量差是否满足

Codeforces Round #527 (Div. 3)A B C D1 D2_codeforces_13

的倍数即可。满足就消去,最后看剩余的数量是否大于

Codeforces Round #527 (Div. 3)A B C D1 D2_c++_06

。栈模拟。

代码

#include<bits/stdc++.h>

using namespace std;

stack< int > st;

int main()
{
#ifndef ONLINE_JUDGE
freopen("input.in","r",stdin);
#endif
int n,x;
cin >> n >> x;
st.push(x);
for(int i = 1; i < n; ++i) {
cin >> x;
if(!st.empty() && (st.top() - x)% 2 == 0) {
st.pop();
}else{
st.push(x);
}
}
puts(st.size() > 1 ? "NO" : "YES");
return 0;
}

D2. Great Vova Wall (Version 2)

题意:同

Codeforces Round #527 (Div. 3)A B C D1 D2_#ifndef_15

,只不过可供选择的瓦片只剩下

Codeforces Round #527 (Div. 3)A B C D1 D2_codeforces_16


题解:因为只剩下

Codeforces Round #527 (Div. 3)A B C D1 D2_#ifndef_11

,所以必须相邻两列高度相等才可以消去,同样栈模拟。但是要注意

Codeforces Round #527 (Div. 3)A B C D1 D2_c++_18

这种情况。

代码

#include<bits/stdc++.h>

using namespace std;

stack<int> st;

int main() {
#ifndef ONLINE_JUDGE
freopen("input.in", "r", stdin);
#endif
int n, x, max1 = 0;
cin >> n;
for (int i = 0; i < n; ++i) {
cin >> x;
max1 = max(max1, x);
if (!st.empty()) {
if (st.top() < x) return puts("NO"), 0;
if (st.top() == x)
st.pop();
else
st.push(x);
} else {
st.push(x);
}
}
if (st.size() > 1 || (st.size() == 1 && st.top() < max1)) puts("NO");
else puts("YES");
return 0;
}