20220904 字节开发笔试
0.四道都A了,可以先看看代码,哪里不明白可以在评论里问一下
1.第一题用逗号分隔,用python写的,代码没保存;(大家应该都过了吧)
2. 第二题弹弹球,就是简单动态规划的变形
#include <iostream> #include <algorithm> #include <cmath> using namespace std; int a[1005][1005]; int b[1005][1005]; bool flag[1005][1005]; int N,M; int main(){ cin >> N >> M; for(int i = 1;i<=N;i++){ for(int j = 1;j<=M;j++){ cin >> a[i][j]; } } for(int i = 1;i<=N;i++) { for(int j = 1; j<=M;j++) { if(i == 1){ flag[i][j] = true; } if(flag[i][j]) { if(a[i][j] == -1) { flag[i+1][j+1] = true; flag[i+1][j-1] = true; } else flag[i+1][j] = true; } } } int ans = 0; for(int i = 1;i<=N;i++){ for(int j = 1; j<=M;j++){ if(!flag[i][j]) continue; if(i == 1){ b[i][j] = max(a[i][j], 0); continue; } if(flag[i-1][j-1] && a[i-1][j-1] == -1){ b[i][j] = max(b[i][j], b[i-1][j-1]); } if(flag[i-1][j+1] && a[i-1][j+1] == -1){ b[i][j] = max(b[i][j], b[i-1][j+1]); } if(flag[i-1][j] && a[i-1][j] != -1){ b[i][j] = max(b[i][j], b[i-1][j]); } b[i][j] += max(0,a[i][j]); ans = max(ans,b[i][j]); } } cout << ans; }3. 判断什么赛高数
#include <iostream> #include <algorithm> #include <cmath> #include <unordered_set> using namespace std; int a[200005]; int main(){ int N,n; cin >> N; while(N--){ cin >> n; for(int i = 1;i<=n;i++){ cin >> a[i]; } sort(a+1,a+n+1); if(a[1]<0 && a[2]<0 && a[3] < 0){ cout << "NO"<<endl; continue; } else if(a[n-1]>0 && a[n-2]>0 && a[n-3] > 0){ cout << "NO" <<endl; continue; } else { unordered_set<int> S; for(int i = 1;i<=n;i++){ S.insert(a[i]); } bool flag = true; for(int i = 1;flag && i<=n;i++){ for(int j = i+1; flag && j<=n;j++){ for(int k = j+1;flag && k<=n;k++){ if(S.count(a[i]+a[j]+a[k]) == 0){ flag = false; break; } } } } if(flag) { cout << "YES"<<endl; } else { cout << "NO"<<endl; } } } }4.子字符串,暴力即可
#include <iostream> #include <algorithm> #include <cmath> #include <unordered_set> #include <string> using namespace std; int main(){ int t; cin >> t; while(t--) { int n,m; string s, ch; cin >> n >> m; cin >> s >> ch; int ans = 0; for(int i = 0;i<n;i++) { int p = i-1, q = i+1; int w = 0; int res = 1; while(p >= 0) { if(s[p] == s[i]){ p--; res+=1; } else if(ch[p] == '1'){ p--; res+=1; w+=1; } else{ break; } if(w > m) { res --; break; } } while(q < n) { if(s[i] == s[q]) { q++; res+=1; } else if(ch[q] == '1'){ q++; res+=1; w+=1; } else{ break; } if(w > m) { res --; break; } } ans = max(ans,res); } cout << ans << endl; } }