牛客周赛 Round 34
A. 小红的字符串生成
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> PII;
const int INF = 1e9;
int main(){
char a, b;
cin >> a >> b;
if(a == b){
cout << 2 << endl;
cout << a << endl;
cout << a << b << endl;
}else{
cout << 4 << endl;
cout << a << endl;
cout << b << endl;
cout << a << b << endl;
cout << b << a << endl;
}
return 0;
}
B. 小红的非排列构造
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> PII;
const int INF = 1e9;
const int N = 1e5 +10;
int n;
int a[N], b[N];
bool flag = 0;
int main(){
cin >> n;
for(int i = 1; i <= n; i++) cin >> a[i], b[i] = a[i];
sort(b + 1, b + n + 1);
for(int i = 1; i <= n; i++){
if(b[i] != i){
flag = 1;
break;
}
}
if(flag) cout << 0;
else cout << 1 << endl << 1 << " " << a[1] + 1;
return 0;
}
C. 小红的数字拆解
s[i]判断这一段子串是否是偶数,如果是就存入vector,不是就继续搜索下一个字符。存入v后左值l变为上一个右值的下一个 l = i + 1
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> PII;
const int INF = 1e9;
string s;
vector<string> v;
bool cmp(string a, string b){
if(a.size() != b.size()) return a.size() < b.size();
return a < b;
}
int main(){
cin >> s;
int l = 0;
for(int i = 0; i < s.size(); i++){
if((s[i] - '0') % 2 == 0){
v.push_back(s.substr(l, i - l + 1));
l = i + 1;
}
}
sort(v.begin(), v.end(), cmp);
for(int i = 0; i < v.size(); i++) cout << v[i] << endl;
return 0;
}
D. 小红的陡峭值
分情况讨论,如果q全是0、q的陡峭值大于1、q的陡峭值 = 0(首尾是否有0)
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> PII;
const int INF = 1e9;
const int N = 1e5 + 10;
int a[N];
int n;
queue<int> q;
bool vis[N];
int main(){
memset(vis, 0, sizeof vis);
cin >> n;
for(int i = 1; i <= n; i++){
cin >> a[i];
if(a[i]) q.push(i), vis[i] = 1;
}
if(q.size() == 0){
for(int i = 1; i < n; i++){
cout << 1 << " ";
}
cout << 2;
return 0;
}
while(q.size()){
int t = q.front();
q.pop();
if(t > 1 && a[t - 1] == 0) a[t - 1] = a[t], q.push(t - 1);
if(t < n && a[t + 1] == 0) a[t + 1] = a[t], q.push(t + 1);
}
int ans = 0;
for(int i = 2; i <= n; i++) ans += abs(a[i] - a[i - 1]);
if(ans > 1){
cout << -1;
return 0;
}else{
if(ans == 0){
if(vis[1] && vis[n]){
cout << -1;
return 0;
}
if(!vis[1]) a[1] ++;
else a[n] ++;
}
}
for(int i = 1; i <= n; i++) cout << a[i] << " ";
return 0;
}