[Offer收割]编程练习赛63

Posted aguin

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[Offer收割]编程练习赛63相关的知识,希望对你有一定的参考价值。

命名

暴力

技术分享图片
 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 char S[1111], T[1111], a[1111], b[1111];
 4 
 5 int main() {
 6     int N;
 7     scanf("%d", &N);
 8     while(N--) {
 9         scanf("%s %s", S + 1, T + 1);
10         int ls = strlen(S + 1), lt = strlen(T + 1), la = 0, lb = 0;
11         for(int i = 1; i <= ls; ++i) {
12             if(i % 2) a[++la] = S[i];
13             else b[++lb] = S[i];
14         }
15         int pa = 1, pb = 1;
16         for(int i = 1; i <= lt; ++i) {
17             if(pa <= la && T[i] == a[pa]) pa++;
18             if(pb <= lb && T[i] == b[pb]) pb++;
19         }
20         puts(pa > la || pb > lb ? "Yes" : "No");
21     }
22     return 0;
23 }
Aguin

 

洗牌

暴力

技术分享图片
 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 int a[1111];
 4 
 5 void solve(int l, int r) {
 6     int m = (l + r) / 2;
 7     for (int i = l; i <= m; ++i) swap(a[i], a[m+i-l+1]);
 8     if (r - l > 1) solve(l, m), solve(m + 1, r);
 9 }
10 
11 int main() {
12     int n, t;
13     scanf("%d %d", &n, &t);
14     for (int i = 1; i <= (1 << n); ++i) scanf("%d", a + i);
15     if (t % 2 == 0) for (int i = 1; i <= (1 << n); ++i) printf("%d
", a[i]);
16     else {
17         solve(1, 1 << n);
18         for (int i = 1; i <= (1 << n); ++i) printf("%d
", a[i]);
19     }
20     return 0;
21 }
Aguin

 

密码更改

暴力

技术分享图片
 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 typedef long long LL;
 4 LL o, t, ans = -1, b;
 5 char s[22];
 6 int l;
 7 
 8 LL dis(LL x, LL y) {
 9     return min(abs(x - y), b - abs(x - y));
10 }
11 
12 int vis[10];
13 void dfs(int x) {
14     if(x == l + 1) {
15         if(ans == -1) ans = t;
16         else if(dis(o, ans) < dis(o, t)) ans = t;
17         else if(dis(o, ans) == dis(o, t) && t < ans) ans = t;
18         return;
19     }
20     for(int i = 0; i <= 9; ++i) {
21         if(vis[i]) continue;
22         vis[i] = 1, t = t * 10 + i, dfs(x + 1);
23         vis[i] = 0, t /= 10;
24     }
25 }
26 
27 void print(int x, LL y) {
28     if(x != 1) print(x - 1, y / 10);
29     printf("%lld", y % 10);
30 }
31 
32 int main() {
33     scanf("%s", s + 1);
34     l = strlen(s + 1);
35     b = 1;
36     for(int i = 1; i <= l; ++i) b *= 10;
37     sscanf(s + 1, "%lld", &o);
38     dfs(1);
39     print(l, ans);
40     return 0;
41 }
Aguin

 

道路摧毁

树dp

技术分享图片
 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 typedef long long LL;
 4 const LL INF = 1e18;
 5 const int maxn = 2e5 + 10;
 6 vector<int> G[maxn], W[maxn];
 7 int lab[maxn];
 8 
 9 LL dp[maxn][3];
10 void dfs(int x, int f) {
11     if(lab[x] == 1) dp[x][2] = INF;
12     if(lab[x] == 2) dp[x][1] = INF;
13     for(int i = 0; i < G[x].size(); ++i) {
14         int to = G[x][i], w = W[x][i];
15         if(to == f) continue;
16         dfs(to, x);
17         if(lab[x] != 1) dp[x][2] += min(dp[to][2], dp[to][1] + w);
18         if(lab[x] != 2) dp[x][1] += min(dp[to][1], dp[to][2] + w);
19     }
20 }
21 
22 int main() {
23     int n, o, mX, mY;
24     scanf("%d", &n);
25     scanf("%d", &mX);
26     for(int i = 1; i <= mX; ++i) scanf("%d", &o), lab[o] = 1;
27     scanf("%d", &mY);
28     for(int i = 1; i <= mY; ++i) scanf("%d", &o), lab[o] = 2;
29     for(int i = 1; i < n; ++i) {
30         int u, v, w;
31         scanf("%d %d %d", &u, &v, &w);
32         G[u].push_back(v), W[u].push_back(w);
33         G[v].push_back(u), W[v].push_back(w);
34     }
35     dfs(1, 0);
36     printf("%lld
", min(dp[1][1], dp[1][2]));
37     return 0;
38 }
Aguin

 

以上是关于[Offer收割]编程练习赛63的主要内容,如果未能解决你的问题,请参考以下文章

[Offer收割]编程练习赛42

hihocoder [Offer收割]编程练习赛18 C 最美和弦(dp)

[Offer收割]编程练习赛39

[Offer收割]编程练习赛32

[Offer收割]编程练习赛40

[Offer收割]编程练习赛57