8.31 360笔试真题
第一题 寻找子串
直接去找单个字符出现的次数即可
#include <iostream> #include <vector> using namespace std; int main() { string str; cin >> str; vector<int> arr(26, 0); for(auto &it:str) arr[it-'a']++; int res = 0; for(auto &it:arr) res = max(res, it); cout << res << endl; return 0; }
第二题 散步
吐槽一下,刚开始题目出错。。
#include <iostream> #include <vector> #include <unordered_set> using namespace std; int main() { int N, M; cin >> N >> M; vector<int> arr(M); for(int i=0; i<M; i++) cin >> arr[i]; unordered_set<int> hashSet; for(int i=1; i<=N; i++) hashSet.insert(i); for(int i=0; i<M; i++) { unordered_set<int> temp; for(auto &it:hashSet) { if(it + arr[i] >= 1 && N >= it + arr[i]) temp.insert(it + arr[i]); if(it - arr[i] >= 1 && N >= it - arr[i]) temp.insert(it - arr[i]); } swap(temp, hashSet); } cout << hashSet.size() << endl; return 0; }