第一题在这里喔:https://www.cnblogs.com/zhmlzhml/p/13835555.html
dfs
1 class Solution { 2 public: 3 4 string findLexSmallestString(string s, int a, int b) { 5 map<string, int> mp; 6 queue<string> q; 7 string minx = s; 8 string ns, cur,curr; 9 int len=s.length(); 10 q.push(s); 11 while (!q.empty()) { 12 ns = q.front(); 13 q.pop(); 14 if (ns < minx) { 15 minx = ns; 16 } 17 cur = ns; 18 for (int i = 1; i < len; i+=2) { 19 cur[i]=(cur[i]-'0'+a)%10+'0'; 20 } 21 if(mp[cur]==0){ 22 q.push(cur); 23 mp[cur]++; 24 } 25 curr=ns.substr(len-b)+ns.substr(0,len-b); 26 if(mp[curr]==0){ 27 q.push(curr); 28 mp[curr]++; 29 } 30 31 32 } 33 return minx; 34 35 } 36 37 };
对我昨天就是没有想起substr这个函数,,,现在估计已经,,,,非常熟练了QWQ
第三题啊,这个题我很明确知道是sort+dp
就是那个sort那个地方,需要有两个排序(先是对年龄,再是对分数)
刚刚参考dl的代码,发现了一个功能qwq
如果是vector<vector<int> >这种类型,你对第一个vector 排序的时候sort,直接就可以实现上述那排序orz
然后就用dp啊
(dp似乎也不是很会的亚子emmmm)
直接贴dl代码了'
class Solution { public: int bestTeamScore(vector<int>& scores, vector<int>& ages) { vector<vector<int>> mp; //存储球员年龄以及分数 int n = ages.size(); for(int i = 0; i < n; i++){ mp.push_back({ages[i], scores[i]}); } sort(mp.begin(), mp.end()); //对球员排序 int maxs = 0; vector<int> dp(n, 0); for(int i = 0; i < n; i++){ for(int j = i - 1; j >= 0; j--){ if(mp[j][1] <= mp[i][1]) dp[i] = max(dp[i], dp[j]); //查找前一个球员 } dp[i] += mp[i][1]; //加上以i结尾球员的分值 maxs = max(dp[i], maxs); //更新最大值 } return maxs; } };
链接:https://leetcode-cn.com/problems/best-team-with-no-conflicts/solution/jing-dian-tan-xin-dong-tai-gui-hua-you-tu-you-zhu-/
啊第四题是关于一个图的,我想我可以直接选择放弃了orz
战略性放弃