<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
	<channel>
		<title>C&#43;&#43; on 一共十七划</title>
		<link>https://noirzh.com/categories/c&#43;&#43;/</link>
		<description>Recent content in C&#43;&#43; on 一共十七划</description>
		<generator>Hugo</generator>
		<language>zh-CN</language>
		
		
		
		
			<lastBuildDate>Sat, 17 Feb 2024 16:11:25 +0800</lastBuildDate>
		
			<atom:link href="https://noirzh.com/categories/c++/index.xml" rel="self" type="application/rss+xml" />
			<item>
				<title>题解-哈希表,用输入流实现C&#43;&#43;的split函数</title>
				<link>https://noirzh.com/reward-top-k-students-solution/</link>
				<pubDate>Wed, 11 Oct 2023 06:07:00 +0800</pubDate>
				<guid>https://noirzh.com/reward-top-k-students-solution/</guid>
				<description>&lt;blockquote&gt;&#xD;&#xA;&lt;p&gt;题目:&lt;a href=&#34;https://leetcode.cn/problems/reward-top-k-students/description/&#34; rel=&#34;noopener noreferrer&#34; target=&#34;_blank&#34;&gt;奖励最顶尖的 K 名学生&lt;/a&gt;&lt;/p&gt;&#xD;&#xA;&lt;/blockquote&gt;&#xD;&#xA;&lt;h2&gt;题目描述&lt;/h2&gt;&#xD;&#xA;&lt;p&gt;给你两个字符串数组 positive_feedback 和 negative_feedback ，分别包含表示正面的和负面的词汇。不会 有单词同时是正面的和负面的。&lt;/p&gt;&#xD;&#xA;&lt;p&gt;一开始，每位学生分数为 0 。每个正面的单词会给学生的分数 加 3 分，每个负面的词会给学生的分数 减  1 分。&lt;/p&gt;&#xD;&#xA;&lt;p&gt;给你 n 个学生的评语，用一个下标从 0 开始的字符串数组 report 和一个下标从 0 开始的整数数组 student_id 表示，其中 student_id[i] 表示这名学生的 ID ，这名学生的评语是 report[i] 。每名学生的 ID 互不相同。&lt;/p&gt;&#xD;&#xA;&lt;p&gt;给你一个整数 k ，请你返回按照得分 从高到低 最顶尖的 k 名学生。如果有多名学生分数相同，ID 越小排名越前。&lt;/p&gt;&#xD;&#xA;&lt;h2&gt;思路&lt;/h2&gt;&#xD;&#xA;&lt;ul&gt;&#xD;&#xA;&lt;li&gt;题目不难,只需要对比各个词出现次数就可以&lt;/li&gt;&#xD;&#xA;&lt;li&gt;对于不同语言来说上面的可能有一定难度&lt;/li&gt;&#xD;&#xA;&lt;li&gt;所以写了一版C++的split(是用输入流实现的)&lt;/li&gt;&#xD;&#xA;&lt;/ul&gt;&#xD;&#xA;&lt;h2&gt;代码&lt;/h2&gt;&#xD;&#xA;&lt;pre tabindex=&#34;0&#34;&gt;&lt;code class=&#34;language-cpp&#34;&gt;class Solution {&#xD;&#xA;public:&#xD;&#xA;  vector&amp;lt;int&amp;gt; topStudents(vector&amp;lt;string&amp;gt;&amp;amp; positive_feedback, vector&amp;lt;string&amp;gt;&amp;amp; negative_feedback, vector&amp;lt;string&amp;gt;&amp;amp; report, vector&amp;lt;int&amp;gt;&amp;amp; student_id, int k) {&#xD;&#xA;      unordered_map&amp;lt;string,int&amp;gt;score;&#xD;&#xA;      vector&amp;lt;pair&amp;lt;int,int&amp;gt;&amp;gt;list;//直接嵌套vector的时间效率不如pair&#xD;&#xA;      for (auto word:positive_feedback) score[word]=3;&#xD;&#xA;      for (auto word:negative_feedback) score[word]=-1;&#xD;&#xA;      for (int i=0;i&amp;lt;student_id.size();i++){&#xD;&#xA;          string words=report[i];&#xD;&#xA;          int id=student_id[i];&#xD;&#xA;          int s=0;&#xD;&#xA;          string key;&#xD;&#xA;          istringstream is(words);//将字符转成输入流然后操作&#xD;&#xA;          while(getline(is,key,&#39; &#39;)){&#xD;&#xA;              if (score.count(key))&#xD;&#xA;                  //key不存在时int默认为0,但是那样效率不&#xD;&#xA;                  s+=score[key];如判断高&#xD;&#xA;          }&#xD;&#xA;          list.emplace_back(make_pair(id,s));&#xD;&#xA;      }&#xD;&#xA;      sort(list.begin(),list.end(),[&amp;amp;](const pair&amp;lt;int,int&amp;gt;a,const pair&amp;lt;int,int&amp;gt;b){&#xD;&#xA;          return a.second&amp;gt;b.second ||(a.second==b.second&amp;amp;&amp;amp;a.first&amp;lt;b.first);&#xD;&#xA;      });&#xD;&#xA;      vector&amp;lt;int&amp;gt;res;&#xD;&#xA;      for (int i=0;i&amp;lt;k;i++){&#xD;&#xA;          res.emplace_back(list[i].first);&#xD;&#xA;      }&#xD;&#xA;      return res;&#xD;&#xA;&lt;p&gt;}&#xA;};&#xA;&lt;/code&gt;&lt;/pre&gt;&lt;/p&gt;</description>
			</item>
			<item>
				<title>题解-英雄的力量</title>
				<link>https://noirzh.com/solution-power-of-heros/</link>
				<pubDate>Tue, 01 Aug 2023 06:31:49 +0800</pubDate>
				<guid>https://noirzh.com/solution-power-of-heros/</guid>
				<description>&lt;blockquote&gt;&#xD;&#xA;&lt;p&gt;Problem: &lt;a href=&#34;https://leetcode.cn/problems/power-of-heroes/description/&#34;&gt;2681. 英雄的力量&lt;/a&gt;&lt;/p&gt;&#xD;&#xA;&lt;/blockquote&gt;&#xD;&#xA;&lt;h2&gt;题目描述&lt;/h2&gt;&#xD;&#xA;&lt;p&gt;给你一个下标从&lt;strong&gt;0&lt;/strong&gt;开始的整数数组&lt;code&gt;nums&lt;/code&gt;，它表示英雄的能力值。如果我们选出一部分英雄，这组英雄的&lt;strong&gt;力量&lt;/strong&gt;定义为：&lt;/p&gt;&#xD;&#xA;&lt;ul&gt;&#xD;&#xA;&lt;li&gt;&lt;strong&gt;&lt;code&gt;i&lt;sub&gt;0&lt;/sub&gt;&lt;/code&gt; ，&lt;code&gt;i&lt;sub&gt;1&lt;/sub&gt;&lt;/code&gt; ，… &lt;code&gt;i&lt;sub&gt;k&lt;/sub&gt;&lt;/code&gt;&lt;/strong&gt; 表示这组英雄在数组中的下标。那么这组英雄的力量为 &lt;code&gt;&lt;strong&gt;max(nums[i&lt;sub&gt;0&lt;/sub&gt;],nums[i&lt;sub&gt;1&lt;/sub&gt;] ... nums[i&lt;sub&gt;k&lt;/sub&gt;])&lt;sup&gt;2&lt;/sup&gt; * min(nums[i&lt;sub&gt;0&lt;/sub&gt;],nums[i&lt;sub&gt;1&lt;/sub&gt;] ... nums[i&lt;sub&gt;k&lt;/sub&gt;])&lt;/strong&gt;&lt;/code&gt; 。&lt;/li&gt;&#xD;&#xA;&lt;/ul&gt;&#xD;&#xA;&lt;p&gt;请你返回所有可能的&lt;strong&gt;非空&lt;/strong&gt;英雄组的&lt;strong&gt;力量&lt;/strong&gt;之和。由于答案可能非常大，请你将结果对&lt;code&gt;&lt;strong&gt;10&lt;sup&gt;9 &lt;/sup&gt;+ 7&lt;/strong&gt;&lt;/code&gt;&lt;strong&gt;取余。&lt;/strong&gt;&lt;/p&gt;&#xD;&#xA;&lt;h2&gt;思路&lt;/h2&gt;&#xD;&#xA;&lt;ul&gt;&#xD;&#xA;&lt;li&gt;选取与顺序无关先排序&lt;/li&gt;&#xD;&#xA;&lt;li&gt;接下来只要枚举所有子序列的最大值的平方和最小值的乘积之和&lt;/li&gt;&#xD;&#xA;&lt;li&gt;注意到只和最大值和最小值有关&lt;/li&gt;&#xD;&#xA;&lt;/ul&gt;&#xD;&#xA;&lt;h2&gt;解题方法&lt;/h2&gt;&#xD;&#xA;&lt;ol&gt;&#xD;&#xA;&lt;li&gt;确定了子序列的最大值之后,我们只需要确定子序列里面各个值最小值出现了几次&lt;/li&gt;&#xD;&#xA;&lt;li&gt;也就是确定排序后的$nums[i]$必选且为最大值时,我们假设此时在答案中与除自身之外他的平方相乘的所有值之和为$pre_i$,此时有答案$res_i=nums[i]^2*(pre_i+nums[i])$&lt;/li&gt;&#xD;&#xA;&lt;li&gt;不去管$pre_i$现在为多少,我们假设现在要选取的是$nums[i+1]$为最大值,则注意到对于子序列&#xD;&#xA;&lt;ol&gt;&#xD;&#xA;&lt;li&gt;如果不选$ nums[i] $,$ pre_i$也会和$nums[i+1]$相乘&lt;/li&gt;&#xD;&#xA;&lt;li&gt;如果选取$nums[i]$,对于所有$nums[i]$不是最小值的情况,$pre_i$也会和$nums[i+1]$相乘,而$nums[i]$是最小值的情况只有一个(也就是选取$i$和$i+1$两个)&lt;/li&gt;&#xD;&#xA;&lt;/ol&gt;&#xD;&#xA;&lt;/li&gt;&#xD;&#xA;&lt;li&gt;根据以上的两个推理,我们能够得出 $$ \displaystyle res_{i+1}=nums[i+1]^2 \cdot (pre_i \cdot 2+nums[i]+nums[i+1]) \\ pre_{i+1}=pre_i\cdot 2+nums[i] $$&lt;/li&gt;&#xD;&#xA;&lt;li&gt;$res=\displaystyle\sum^{n}_{i=0} res_i $&lt;/li&gt;&#xD;&#xA;&lt;/ol&gt;&#xD;&#xA;&lt;ul&gt;&#xD;&#xA;&lt;li&gt;时间复杂度$O(nlogn)$&lt;/li&gt;&#xD;&#xA;&lt;li&gt;空间复杂度$O(1)$&lt;/li&gt;&#xD;&#xA;&lt;/ul&gt;&#xD;&#xA;&lt;h2&gt;Code&lt;/h2&gt;&#xD;&#xA;&lt;div&gt;&#xD;&#xA;&lt;div&gt;&#xD;&#xA;&lt;div&gt;&lt;div&gt;&#xD;&#xA;&lt;pre tabindex=&#34;0&#34;&gt;&lt;code class=&#34;language-python&#34;&gt;class Solution:&#xD;&#xA;    def sumOfPower(self, nums: List[int]) -&amp;gt; int:&#xD;&#xA;        mod=1000000007&#xD;&#xA;        nums.sort()&#xD;&#xA;        pre=0&#xD;&#xA;        res=0&#xD;&#xA;        for num in nums:&#xD;&#xA;            res=(res+(pre+num)*(num**2)&#xD;&#xA;            pre=(pre*2+num&#xD;&#xA;        return res&lt;/code&gt;&lt;/pre&gt;&#xD;&#xA;&lt;/div&gt;&lt;/div&gt;&#xD;&#xA;&lt;/div&gt;&#xD;&#xA;&lt;/div&gt;&#xD;&#xA;&lt;pre tabindex=&#34;0&#34;&gt;&lt;code class=&#34;language-python&#34;&gt;class Solution {&#xD;&#xA;public:&#xD;&#xA;    int sumOfPower(vector&amp;lt;int&amp;gt;&amp;amp; nums) {&#xD;&#xA;        const int mod=1e9+7;&#xD;&#xA;        sort(nums.begin(),nums.end());&#xD;&#xA;        int res=0,pre=0;&#xD;&#xA;        for (long num:nums){&#xD;&#xA;            res=(res+(num*nu&#xD;&#xA;            pre=(pre*2+num&#xD;&#xA;        }&#xD;&#xA;        return res;&#xD;&#xA;    }&#xD;&#xA;};&#xD;&#xA;&lt;/code&gt;&lt;/pre&gt;</description>
			</item>
	</channel>
</rss>
