【2022-06-11-第80场双周赛】

总结
水题,Q1WA两次是逆天。
Q1.强密码检验器 II
模拟即可。
class Solution {
public:
bool strongPasswordCheckerII(string p) {
int h[4] = {0};
string t = "!@#$%^&*()-+";
if(p.size() < 8) return false;
for(int i = 0; i < p.size() - 1; ++i){
if(p[i] == p[i+1]) return false;
}
for(auto i : p){
if(i >= 'a' && i <= 'z') h[1] = 1;
if(i >= 'A' && i <= 'Z') h[0] = 1;
if(i >= '0' && i <= '9') h[3] = 1;
if(t.find(i) != -1) h[2] = 1;
}
for(int i = 0; i < 4; ++i) if(h[i] == 0) return false;
return true;
}
};
Q2. 咒语和药水的成功对数
class Solution {
public:
vector<int> successfulPairs(vector<int>& s, vector<int>& p, long long sc) {
sort(p.begin(), p.end());
int n = s.size(), m = p.size();
vector<int> ret;
for(auto i : s){
int l = -1, r = m;
while(l + 1 < r){
int mid = l + r >> 1;
if((long long)p[mid] * i < sc) l = mid;
else r = mid;
}
ret.push_back(m - r);
}
return ret;
}
};
Q3.替换字符后匹配
用哈希表记录转换路径,然后暴力匹配。
class Solution {
public:
bool matchReplacement(string s, string sub, vector<vector<char>>& mps) {
map<char, set<char>> mp;
int n = s.size(), m = sub.size();
int h[n - m + 1]; memset(h, 0, sizeof(h));
int p[128][128]; memset(p, 0, sizeof(p));
for(auto &i : mps) p[i[0]][i[1]] = 1;
for(int i = 0; i < m; ++i){
for(int j = 0; j <= n - m; ++j){
if(sub[i] == s[i + j] || p[sub[i]][s[i + j]]){
h[j]++;
}
}
}
for(int i = 0; i <= n - m; ++i){
if(h[i] == m) return true;
}
return false;
}
};
Q4.https://leetcode.cn/problems/count-subarrays-with-score-less-than-k/
可二分可滑窗。
class Solution {
public:
long long countSubarrays(vector<int>& a, long long k) {
int n = a.size();
long long ps[n + 1]; ps[0] = 0;
long long ret = 0;
for(int i = 0; i <n; ++i) ps[i + 1] = ps[i] + a[i];
for(int i = 1; i <= n; ++i){
int l = -1, r = i + 1;
while(l + 1 < r){
int mid = l + r >> 1;
if((ps[i] - ps[mid]) * (i - mid) >= k) l = mid;
else r = mid;
}
ret += i - r;
}
return ret;
}
};
浙公网安备 33010602011771号