经典操作系统和数据库
第一题,二维前缀和
#include<bits/stdc++.h>
using namespace std;
int mp[1010][1010];
int pre[1010][1010];
// !注意可能一个坐标上有多个敌人
int main() {
int n,a,b;
cin >> n >> a >> b;
int mx = -1, my = -1;
for(int i = 1; i <= n; i++) {
int x, y;
cin >> x >> y;
mx = max(mx, x + 1);
my = max(my, y + 1);
mp[x][y] ++;
}
for(int i = 1; i <= mx; i++) {
for(int j = 1; j <= my; j++) {
pre[i][j] = mp[i][j] + pre[i - 1][j] + pre[i][j - 1] - pre[i - 1][j - 1];
}
}
int res = -1;
for(int i = 1; i <= mx - a; i ++) {
for(int j = 1; j <= my - b; j++) {
res = max(res, pre[a + i][b + j] - pre[a + i][j - 1] - pre[i - 1][b + j] + pre[i - 1][j - 1]);
}
}
cout << res;
}
第二题,模拟,考虑奇数长度字符串的情况
#我的实习求职记录##美团##美团笔试#
#include<bits/stdc++.h>
using namespace std;
int main() {
string s, str;
cin >> s;
str = s;
reverse(str.begin(), str.end());
int l = 0, r = s.length() - 1;
if(s.length() == 1) {
cout << 'a' << endl;
} else if(str == s) {
while(s[l] == 'a' && l < r) {
l ++;
r--;
}
s[l] = s[r] = 'a';
} else {
int cnt = 0, x = -1, y = -1,flag = 0;
while(l < r) {
if(s[l] != s[r]) {
cnt ++;
x = l, y = r;
if(min(s[l], s[r]) == 'a') {
flag = 1;
}
s[l] = s[r] = min(s[l], s[r]);
}
l ++;
r --;
}
if(flag && cnt == 1){
if(s.length() & 1) {
s[s.length() / 2] = 'a';
}
} else if(cnt == 1) {
s[x] = s[y] = 'a';
}
}
cout << s << endl;
}