A
模拟即可。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 #include <bits/stdc++.h> using namespace std;#define int long long using ll = long long ;const int M = 1234567890 ;const int mod = 998244353 ;void Totoro () {int n;cin>>n; vector<int >a (n+1 ); ll sum=0 ; for (int i=1 ;i<=n;i++){ cin>>a[i]; if (i&1 ){ sum+=a[i]; continue ; } sum-=a[i]; } cout<<sum<<'\n' ; } signed main () { int t = 1 ; cin>>t; while (t--) { Totoro (); } }
jiangly-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 #include <bits/stdc++.h> using i64 = long long ;void solve () { int n; std::cin >> n; std::vector<int > a (n) ; for (int i = 0 ; i < n; i++) { std::cin >> a[i]; } int ans = 0 ; for (int i = 0 ; i < n; i++) { if (i % 2 == 0 ) { ans += a[i]; } else { ans -= a[i]; } } std::cout << ans << "\n" ; } int main () { std::ios::sync_with_stdio (false ); std::cin.tie (nullptr ); int t; std::cin >> t; while (t--) { solve (); } return 0 ; }
B
模拟即可。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 #include <bits/stdc++.h> using namespace std;#define int long long using ll = long long ;const int M = 1234567890 ;const int mod = 998244353 ;int mp[4 ];void Totoro () { int a,b; cin>>a>>b; mp[a]++; mp[b]++; for (int i=1 ;i<=3 ;i++) { if (!mp[i]) { cout<<i<<'\n' ; return ; } } } signed main () { int t = 1 ; while (t--) { Totoro (); } }
jiangly
这个太优雅了,由于 1,2,3 异或起来是
0,一个没来相当于就是他的异或,巧妙。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include <bits/stdc++.h> using i64 = long long ;int main () { std::ios::sync_with_stdio (false ); std::cin.tie (nullptr ); int a, b; std::cin >> a >> b; std::cout << (a ^ b) << "\n" ; return 0 ; }
C1
暴力即可。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 #include <bits/stdc++.h> using namespace std;#define int long long using ll = long long ;void Totoro () { 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 ; } } cout << "NO\n" ; } signed main () { int t = 1 ; while (t--) { Totoro (); } }
jiangly
直接用 kmp 了,甚至没写暴力。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 #include <bits/stdc++.h> using i64 = long long ;int main () { std::ios::sync_with_stdio (false ); std::cin.tie (nullptr ); std::string s; std::cin >> s; int n = s.size (); std::vector<int > f (n + 1 ) ; for (int i = 1 , j = 0 ; i < n; i++) { while (j > 0 && s[i] != s[j]) { j = f[j]; } j += (s[i] == s[j]); f[i + 1 ] = j; } if (f[n] > n / 2 ) { std::cout << "YES\n" ; std::cout << s.substr (0 , f[n]) << "\n" ; } else { std::cout << "NO\n" ; } return 0 ; }
C2
KMP。思考 next 数组的含义。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 #include <bits/stdc++.h> using namespace std;#define int long long using ll = long long ;vector<int > prefix_function (const string &s) { int n = s.size (); vector<int > 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; } void Totoro () { string s; cin >> s; int n = s.size (); vector<int > pi = prefix_function (s); if (pi[n - 1 ] * 2 > n) { cout << "YES\n" << s.substr (0 , pi[n - 1 ]) << "\n" ; } else { cout << "NO\n" ; } } signed main () { int t = 1 ; while (t--) { Totoro (); } }