<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
	<channel>
		<title>技巧 on 一共十七划</title>
		<link>https://noirzh.com/tags/%E6%8A%80%E5%B7%A7/</link>
		<description>Recent content in 技巧 on 一共十七划</description>
		<generator>Hugo</generator>
		<language>zh-CN</language>
		
		
		
		
			<lastBuildDate>Sun, 05 Nov 2023 20:09:19 +0800</lastBuildDate>
		
			<atom:link href="https://noirzh.com/tags/%E6%8A%80%E5%B7%A7/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>
	</channel>
</rss>
