A. Alternating Sum of Numbers
#include<bits/stdc++.h>
using namespace std;
using i32 = int32_t;
using i64 = long long;
using i128 = __int128;
using vi = vector<int>;
using pii = pair<int, int>;
const i32 inf = INT_MAX / 2;
const i64 INF = LLONG_MAX / 2;
void solve() {
int n, res = 0;
cin >> n;
for (int i = 1, x; i <= n; i++) {
cin >> x;
if (i & 1) res += x;
else res -= x;
}
cout << res << "\n";
return;
}
i32 main() {
ios::sync_with_stdio(false), cin.tie(nullptr);
int T;
cin >> T;
while (T--)
solve();
return 0;
}
B. Three Brothers
#include<bits/stdc++.h>
using namespace std;
using i32 = int32_t;
using i64 = long long;
using i128 = __int128;
//#define int i64
using vi = vector<int>;
using pii = pair<int, int>;
const i32 inf = INT_MAX / 2;
const i64 INF = LLONG_MAX / 2;
i32 main() {
ios::sync_with_stdio(false), cin.tie(nullptr);
int x , y;
cin >> x >> y;
cout << 6 - x - y;
return 0;
}
C1. Message Transmission Error (easy version)
#include<bits/stdc++.h>
using namespace std;
using i32 = int32_t;
using i64 = long long;
using i128 = __int128;
//#define int i64
using vi = vector<int>;
using pii = pair<int, int>;
const i32 inf = INT_MAX / 2;
const i64 INF = LLONG_MAX / 2;
i32 main() {
ios::sync_with_stdio(false), cin.tie(nullptr);
string s;
cin >> s;
int n = s.size();
for (int i = 1; i < n ; i++) {
if( i * 2 <=n) continue;
string t = s.substr(0, i);
if (t == s.substr(n - i, i)) {
cout << "YES\n" << t << "\n";
return 0;
}
}
cout << "NO\n";
return 0;
}
C2. Message Transmission Error (hard version)
直接暴力做肯定不行了,這裡可以用到kmp中的字首和函式來計算
#include<bits/stdc++.h>
using namespace std;
using i32 = int32_t;
using i64 = long long;
using i128 = __int128;
//#define int i64
using vi = vector<int>;
using pii = pair<int, int>;
const i32 inf = INT_MAX / 2;
const i64 INF = LLONG_MAX / 2;
vi prefix_function(const string &s) {
int n = s.size();
vi pi(n);
for (int i = 1, j; i < n; i++) {
j = pi[i - 1];
while (j > 0 and s[i] != s[j])
j = pi[j - 1];
if (s[i] == s[j]) j++;
pi[i] = j;
}
return pi;
}
i32 main() {
ios::sync_with_stdio(false), cin.tie(nullptr);
string s;
cin >> s;
int n = s.size();
vi pi = prefix_function(s);
if(pi[n - 1] * 2 > n) {
cout << "YES\n" << s.substr(0 , pi[n - 1]) << "\n";
}else{
cout << "NO\n";
}
return 0;
}