Codeforces Round #760
A. Polycarp and Sums of Subsequences
题目意思:
这道题的意思是一个长度为3数组,由其内部元素两两或全部相加得到的数,在组成一个长度为7的数组,例如{1, 3, 4}=>{1, 3, 4, 4, 5, 7, 8}。
其中:
4 = 1 + 3;
5 = 1 + 4;
7 = 3 + 4;
8 = 1 + 3 + 4;
但是题目要求的是让我们逆向思维,也就是由{1, 3, 4, 4, 5, 7, 8}得到{1, 3, 4}。
思路分析:
其实我们很容易找到其中的规律,最小的两个数一定是我们要求的三个数里面的,因为我们只能由小的数组成大的数,而不能由大的数组成小的数,所以确认两个数后我们只需将最大的数减去那两个数,就是第三个数了。
代码如下:
#include <iostream>
using namespace std;
int main(){
int n;
cin >> n;
while(n--){
int b[7];
int a[3];
for(int i = 0; i < 7; i++){
cin >> b[i];
}
a[0] = b[0];
a[1] = b[1];
a[2] = b[6] - a[0] - a[1];
cout << a[0] << " " << a[1] << " " << a[2] << endl;
}
return 0;
}
B. Missing Bigram
题目要求:
题目是给定n-2个长度为二的字符串,并只由a、b组成,要求我们找出一个字符串,使得这n-2个长度为2的字符串都是该串的子串,并且长度为n,且顺序与输入的一致。
思路分析:
我们只需要判断需要链接的两个串之间,前一个串的第二个字符与后一个串的第一字符是否相等即可。同时我们还需判断长度是否足够。
代码如下:
#include <bits/stdc++.h>
using namespace std;
int main(){
int t;
cin >> t;
while(t--){
int n;
cin >> n;
string str[n];
for(int i = 0; i < n - 2; i++){
cin >> str[i];
}
string s = str[0];
bool judge = true;
for(int i = 1; i < n - 2; i++){
if(str[i][0] != str[i - 1][1]){
judge = false;
s += str[i][0];
}
s += str[i][1];
}
if(judge){
s += 'a';
}
cout << s << endl;
}
}