第一题用一个哈希表存每个单词对应的次数,维护一个count变量记录当前所需次数,只要当前单词次数大于count,count自增,然后用set将该单词记录避免重复统计,最后输出count
#include<iostream> #include<string> #include<vector> #include<unordered_map> #include<unordered_set> using namespace std; int main(){ int n; cin>>n; string s; int count = 1; unordered_map<string, int> map; unordered_set<string> isRem; for(int i=0;i<n;i++){ cin>>s; map[s]++; if(map[s]>=count){ if(isRem.count(s)==0){ isRem.insert(s); count++; } } } cout<<count-1<<endl; }
这题有点脑筋急转弯,其实只要将m和w先拆开,然后左右指针判断即可
#include<iostream> #include<string> #include<vector> #include<unordered_map> #include<unordered_set> using namespace std; void check(string s){ int l = 0, r = s.size() - 1; while(l < r){ if(s[l]==s[r]){ l++;r--; } else if(s[l]=='b'||s[l]=='p'||s[l]=='q'||s[l]=='d'){ if(s[r]=='b'||s[r]=='p'||s[r]=='q'||s[r]=='d'){ l++;r--; } else{ cout<<"NO"<<endl; return; } } else if(s[l]=='n'||s[l]=='u'){ if(s[r]=='n'||s[r]=='u'){ l++;r--; } else{ cout<<"NO"<<endl; return; } } else{ cout<<"NO"<<endl; return; } } cout<<"YES"<<endl; } int main(){ int n; cin>>n; string s; for(int i=0;i<n;i++){ cin>>s; string str = ""; for(char ch:s){ if(ch=='w'){ str += "vv"; } else if(ch=='m'){ str += "nn"; } else str.push_back(ch); } check(str); } }
第三题旅游景点
只能经过三个城市,暴力dfs即可,有点坑的是不能用邻接矩阵表示城市间所需时间,这样好像会爆内存,但是赛码网不会提醒,得去提交记录里面看内存记录
#include<iostream> #include<string> #include<vector> #include<unordered_map> #include<unordered_set> using namespace std; int main(){ int n,m; long long k; int a,b,c; cin>>n>>m>>k; vector<int>sites(n+1); for(int i=1;i<=n;i++) cin>>sites[i]; vector<int>times(n+1); for(int i=1;i<=n;i++) cin>>times[i]; unordered_map<int,vector<pair<int,int>>> roads; for(int i=0;i<m;i++){ cin>>a>>b>>c; roads[a].push_back({b,c}); roads[b].push_back({a,c}); } long long maxvalue = 0; for(int i=1;i<=n;i++){ if(times[i]>k) continue; maxvalue = max(maxvalue, (long long)sites[i]); // 第一步 从i出发 vector<pair<int,int>> next = roads[i]; for(pair<int,int> p:next){ long long time = times[i]; long long value = sites[i]; int j = p.first; int road = p.second; if(i!=j){ time += (times[j] + road); if(time>k) continue; value += sites[j]; maxvalue = max(maxvalue, value); /// 第二步 从j出发 vector<pair<int,int>> next1 = roads[j]; for(pair<int,int> q:next1){ long long time2 = time; long long value2 = value; int x = q.first; int road1 = q.second; if(x!=j&&x!=i){ time2 += (times[x] + road1); if(time2>k) continue; value2 += sites[x]; maxvalue = max(maxvalue, value2); } } } } } cout<<maxvalue<<endl; }
感觉比小红书提前批的题简单多了
#小红书#