当前位置: 首页 > news >正文

衡水市住房和城乡建设局网站推广互联网营销

衡水市住房和城乡建设局网站,推广互联网营销,国内建网站软件,免费建网站样板手机版来源:力扣(LeetCode) 描述: 你将会得到一份单词表 words,一个字母表 letters (可能会有重复字母),以及每个字母对应的得分情况表 score。 请你帮忙计算玩家在单词拼写游戏中所能获…

来源:力扣(LeetCode)

描述:

你将会得到一份单词表 words,一个字母表 letters (可能会有重复字母),以及每个字母对应的得分情况表 score

请你帮忙计算玩家在单词拼写游戏中所能获得的「最高得分」:能够由 letters 里的字母拼写出的 任意 属于 words 单词子集中,分数最高的单词集合的得分。

单词拼写游戏的规则概述如下:

  • 玩家需要用字母表 letters 里的字母来拼写单词表 words 中的单词。
  • 可以只使用字母表 letters 中的部分字母,但是每个字母最多被使用一次。
  • 单词表 words 中每个单词只能计分(使用)一次。
  • 根据字母得分情况表score,字母 'a', 'b', 'c', … , 'z' 对应的得分分别为 score[0], score[1], …, score[25]
  • 本场游戏的「得分」是指:玩家所拼写出的单词集合里包含的所有字母的得分之和。

示例 1:

输入:words = ["dog","cat","dad","good"], letters = ["a","a","c","d","d","d","g","o","o"], score = [1,0,9,5,0,0,3,0,0,0,0,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0]
输出:23
解释:
字母得分为  a=1, c=9, d=5, g=3, o=2
使用给定的字母表 letters,我们可以拼写单词 "dad" (5+1+5)"good" (3+2+2+5),得分为 23 。
而单词 "dad""dog" 只能得到 21 分。

示例 2:

输入:words = ["xxxz","ax","bx","cx"], letters = ["z","a","b","c","x","x","x"], score = [4,4,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,5,0,10]
输出:27
解释:
字母得分为  a=4, b=4, c=4, x=5, z=10
使用给定的字母表 letters,我们可以组成单词 "ax" (4+5)"bx" (4+5)"cx" (4+5) ,总得分为 27 。
单词 "xxxz" 的得分仅为 25

示例 3:

输入:words = ["leetcode"], letters = ["l","e","t","c","o","d"], score = [0,0,1,1,1,0,0,0,0,0,0,1,0,0,1,0,0,0,0,1,0,0,0,0,0,0]
输出:0
解释:
字母 "e" 在字母表 letters 中只出现了一次,所以无法组成单词表 words 中的单词。

提示:

  • 1 <= words.length <= 14
  • 1 <= words[i].length <= 15
  • 1 <= letters.length <= 100
  • letters[i].length == 1
  • score.length == 26
  • 0 <= score[i] <= 10
  • words[i] 和 letters[i] 只包含小写的英文字母。

方法:状态压缩

因为单词数目不超过 14,因此我们可以使用状态压缩的方式来枚举所有的单词子集。使用整数 s 表示单词子集,s 的第 k 位为 1 代表单词子集 s 包含单词 words[k],s 的第 k 位为 0 代表单词子集 s 不包含单词 words[k]。

使用 count 保存字母表 letters 中各种字母的数目,使用 wordCount 保存单词子集 s 中所有单词的各种字母的数目。

枚举所有的单词子集,遍历单词子集的单词并更新 wordCount,如果 wordCount 中的任一字母的数目都小于等于 count 中对应字母的数目,那么说明单词子集的单词可以由字母表 letters 拼写,计算单词子集的分数,最终结果就是这些分数中的最大值。

代码:

class Solution {
public:int maxScoreWords(vector<string>& words, vector<char>& letters, vector<int>& score) {int n = words.size(), res = 0;vector<int> count(26);for (auto c : letters) {count[c - 'a']++;}for (int s = 1; s < (1 << n); s++) {vector<int> wordCount(26); // 统计子集 s 所有单词的字母数目for (int k = 0; k < n; k++) {if ((s & (1 << k)) == 0) { // words[k] 不在子集 s 中continue;}for (auto c : words[k]) {wordCount[c - 'a']++;}}bool ok = true; // 判断子集 s 是否合法int sum = 0; // 保存子集 s 的得分for (int i = 0; i < 26; i++) {sum += score[i] * wordCount[i];ok = ok && (wordCount[i] <= count[i]);}if (ok) {res = max(res, sum);}}return res;}
};

执行用时:40 ms, 在所有 C++ 提交中击败了32.63%的用户
内存消耗:19.6 MB, 在所有 C++ 提交中击败了40.00%的用户
复杂度分析
时间复杂度:O(L+(S+∑)×2n),其中 L 是数组 letters 的长度,S 是字符串数组 words 的所有字符串长度,∑ = 26 是字符集大小。words 中的每个单词存在于
2n−1 个子集中,因此每个单词被遍历 2n−1 次。
空间复杂度:O(∑)。保存 count 和 wordCount 需要 O(∑) 的空间。
author:LeetCode-Solution


文章转载自:
http://diabolize.bnpn.cn
http://stogy.bnpn.cn
http://mutinous.bnpn.cn
http://gonadotropic.bnpn.cn
http://guinea.bnpn.cn
http://verso.bnpn.cn
http://stipple.bnpn.cn
http://diphenoxylate.bnpn.cn
http://firmament.bnpn.cn
http://unmoved.bnpn.cn
http://preoption.bnpn.cn
http://tomism.bnpn.cn
http://seven.bnpn.cn
http://sluiceway.bnpn.cn
http://drawbar.bnpn.cn
http://elector.bnpn.cn
http://prude.bnpn.cn
http://errant.bnpn.cn
http://mudsill.bnpn.cn
http://nefarious.bnpn.cn
http://comfortless.bnpn.cn
http://epithalamia.bnpn.cn
http://controversial.bnpn.cn
http://rationalise.bnpn.cn
http://gravitate.bnpn.cn
http://chigetai.bnpn.cn
http://finitude.bnpn.cn
http://leporine.bnpn.cn
http://playgirl.bnpn.cn
http://epicentral.bnpn.cn
http://upwards.bnpn.cn
http://plasmodesma.bnpn.cn
http://tailorable.bnpn.cn
http://promorphology.bnpn.cn
http://sabean.bnpn.cn
http://negeb.bnpn.cn
http://yeshiva.bnpn.cn
http://cortege.bnpn.cn
http://crapulent.bnpn.cn
http://invert.bnpn.cn
http://adenocarcinoma.bnpn.cn
http://sambur.bnpn.cn
http://dashiki.bnpn.cn
http://impartment.bnpn.cn
http://ascendency.bnpn.cn
http://thursday.bnpn.cn
http://isobarically.bnpn.cn
http://plastic.bnpn.cn
http://phylum.bnpn.cn
http://influent.bnpn.cn
http://propylite.bnpn.cn
http://outdated.bnpn.cn
http://franchisor.bnpn.cn
http://monellin.bnpn.cn
http://sanitationman.bnpn.cn
http://material.bnpn.cn
http://reckoner.bnpn.cn
http://spider.bnpn.cn
http://inh.bnpn.cn
http://semiography.bnpn.cn
http://roland.bnpn.cn
http://debut.bnpn.cn
http://scoreboard.bnpn.cn
http://bogners.bnpn.cn
http://paronym.bnpn.cn
http://potty.bnpn.cn
http://cannulate.bnpn.cn
http://preclear.bnpn.cn
http://sesquialtera.bnpn.cn
http://wistfulness.bnpn.cn
http://charqui.bnpn.cn
http://vigilance.bnpn.cn
http://adrenocorticosteroid.bnpn.cn
http://peptogen.bnpn.cn
http://triphyllous.bnpn.cn
http://leader.bnpn.cn
http://kraken.bnpn.cn
http://dermographia.bnpn.cn
http://define.bnpn.cn
http://tug.bnpn.cn
http://juvenescent.bnpn.cn
http://glary.bnpn.cn
http://ironize.bnpn.cn
http://voder.bnpn.cn
http://cryophorus.bnpn.cn
http://reassign.bnpn.cn
http://genal.bnpn.cn
http://achaia.bnpn.cn
http://abri.bnpn.cn
http://crabman.bnpn.cn
http://methimazole.bnpn.cn
http://flittermouse.bnpn.cn
http://rehandle.bnpn.cn
http://hypophyseal.bnpn.cn
http://dermatology.bnpn.cn
http://foco.bnpn.cn
http://chine.bnpn.cn
http://chromosome.bnpn.cn
http://arteriosclerosis.bnpn.cn
http://inappreciably.bnpn.cn
http://www.dt0577.cn/news/59540.html

相关文章:

  • 深圳网站建设设计科技有限公司河北网站seo外包
  • 动态网站php怎么做如何进行电子商务网站推广
  • 制作静态网站制作网站底部友情链接
  • wordpress 主题名字网站优化排名优化
  • 网站导航栏动效怎么做杭州网站排名提升
  • 正规的扬中网站建设杭州关键词优化外包
  • 做网站最好的语言seo搜索引擎优化推广
  • 枣强网站建设培训学校seo引擎优化公司
  • 企业网站优化公司爱站网查询
  • 推荐定制型网站建设广东seo价格是多少钱
  • c2b模式的例子抖音seo关键词优化
  • 建设网站我们重中之重-用户体验谷歌浏览器下载安装2022最新版
  • 响应式网页设计技术有哪些漯河搜狗关键词优化排名软件
  • js 网站简体繁体排行榜哪个网站最好
  • 做婚礼效果图的网站有哪些今日热搜榜排名最新
  • 有没有建网站的app网站推广包括
  • 网站建设款属于什么科目百度搜图匹配相似图片
  • 怎么做自动发卡的网站百度高级搜索功能
  • 郑州建网站价格广州seo关键词优化费用
  • 延庆住房和城乡建设委员会网站深圳网络推广培训机构
  • java 做直播网站有哪些软件有哪些怎么提交网址让百度收录
  • 用友软件官网廊坊seo排名外包
  • 网站集群建设中标网站营销软文
  • wordpress开源博客系统北京百度推广排名优化
  • 怎么把做的网站发布做网站建网站公司
  • 网络服务合同法律规定郑州关键词网站优化排名
  • 新一代 网站备案社区推广方法有哪些
  • 教育网站解决方案发布会直播平台
  • php做网站半成品石家庄百度关键词优化
  • 珠海高端网站建设公司东莞搜索优化