Testing Round 19 (Div. 3)

1|0A. 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; }

2|0B. 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; }

3|0C1. 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; }

4|0C2. 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; }

__EOF__

本文作者PHarr
本文链接https://www.cnblogs.com/PHarr/p/18386641.html
关于博主:前OIer,SMUer
版权声明CC BY-NC 4.0
声援博主:如果这篇文章对您有帮助,不妨给我点个赞
posted @   PHarr  阅读(312)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
点击右上角即可分享
微信分享提示