LeetCode 1255 得分最高的单词集合 Maximum Score Words Formed by Letters
地址 https://leetcode-cn.com/problems/maximum-score-words-formed-by-letters/
题目描述
你将会得到一份单词表 words,一个字母表 letters (可能会有重复字母),以及每个字母对应的得分情况表 score。
请你帮忙计算玩家在单词拼写游戏中所能获得的「最高得分」:能够由 letters 里的字母拼写出的 任意 属于 words 单词子集中,分数最高的单词集合的得分。
单词拼写游戏的规则概述如下:
玩家需要用字母表 letters 里的字母来拼写单词表 words 中的单词。
可以只使用字母表 letters 中的部分字母,但是每个字母最多被使用一次。
单词表 words 中每个单词只能计分(使用)一次。
根据字母得分情况表score,字母 ‘a’, ‘b’, ‘c’, … , ‘z’ 对应的得分分别为 score[0], score[1], …, score[25]。
本场游戏的「得分」是指:玩家所拼写出的单词集合里包含的所有字母的得分之和。
样例
示例 : 输入:words = ["dog","cat","dad","good"], letters = ["a","a","c","d","d","d","g","o","o"],
score = [,,,,,,,,,,,,,,,,,,,,,,,,,]
输出:
解释:
字母得分为 a=, c=, d=, g=, o=
使用给定的字母表 letters,我们可以拼写单词 "dad" (++)和 "good" (+++),得分为 。
而单词 "dad" 和 "dog" 只能得到 分。
示例 : 输入:words = ["xxxz","ax","bx","cx"], letters = ["z","a","b","c","x","x","x"],
score = [,,,,,,,,,,,,,,,,,,,,,,,,,]
输出:
解释:
字母得分为 a=, b=, c=, x=, z=
使用给定的字母表 letters,我们可以组成单词 "ax" (+), "bx" (+) 和 "cx" (+) ,总得分为 。
单词 "xxxz" 的得分仅为 。
示例 : 输入:words = ["leetcode"], letters = ["l","e","t","c","o","d"],
score = [,,,,,,,,,,,,,,,,,,,,,,,,,]
输出:
解释:
字母 "e" 在字母表 letters 中只出现了一次,所以无法组成单词表 words 中的单词。
提示: <= words.length <=
<= words[i].length <=
<= letters.length <=
letters[i].length ==
score.length ==
<= score[i] <=
words[i] 和 letters[i] 只包含小写的英文字母。
算法1
(暴力枚举)
DFS 暴力枚举 是的 就是暴力
也许出题人是想做动态规划?? 然后降低了数据量么?
class Solution {
public: int ret = ;
void transferToArr(const vector<string>& words, vector<vector<int>>& wordsArr,
const vector<char>& letters, vector<int>& lettersArr)
{
for (int i = ; i < words.size(); i++) {
vector<int> tmp();
for (int j = ; j < words[i].size(); j++) {
int idx = words[i][j] - 'a';
tmp[idx]++;
}
wordsArr.push_back(tmp);
} for (int i = ; i < letters.size(); i++) {
int idx = letters[i] - 'a';
lettersArr[idx]++;
}
} int SelectWord(vector<int> wordCount, vector<int>& lettersArr, vector<int>& score)
{
int retScore = ;
for (int i = ; i < wordCount.size(); i++) {
if (wordCount[i] != ) {
if (wordCount[i] > lettersArr[i])
return ;
lettersArr[i] -= wordCount[i];
retScore += score[i] * wordCount[i];
}
} return retScore;
} void DFS(vector<vector<int>>& wordsArr, int idx, vector<int> lettersArr, int currScore, vector<int>& score)
{
if (idx >= wordsArr.size()) {
ret = max(ret, currScore);
return;
} vector<int> copyLettersArr = lettersArr;
int addscore = SelectWord(wordsArr[idx], lettersArr, score);
if (addscore != )
DFS(wordsArr, idx+, lettersArr, currScore + addscore, score);
//这里尝试的是不放入该单词的组合
DFS(wordsArr, idx + , copyLettersArr, currScore, score);
} int maxScoreWords(vector<string>& words, vector<char>& letters, vector<int>& score) { vector<vector<int>> wordsArr;
vector<int> lettersArr();
//将提出给出的变量 转化成字符索引 多少个字符为值得数组
transferToArr(words, wordsArr, letters, lettersArr);
DFS(wordsArr, , lettersArr, , score);
return ret;
} };
LeetCode 1255 得分最高的单词集合 Maximum Score Words Formed by Letters的更多相关文章
- 【leetcode】1255. Maximum Score Words Formed by Letters
题目如下: Given a list of words, list of single letters (might be repeating) and score of every charact ...
- UVA 12906 Maximum Score 排列组合
Maximum Score Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/vie ...
- [LeetCode] Concatenated Words 连接的单词
Given a list of words (without duplicates), please write a program that returns all concatenated wor ...
- java基础(10)---leetcode的String、数组以及集合的一些使用
整数 一.整数反转_7 /* 12345 变成 54321 */ public class 整数反转_7 { public static void main(String[] args){ int x ...
- [LeetCode] Valid Word Square 验证单词平方
Given a sequence of words, check whether it forms a valid word square. A sequence of words forms a v ...
- [LeetCode] Valid Word Abbreviation 验证单词缩写
Given a non-empty string s and an abbreviation abbr, return whether the string matches with the give ...
- [LeetCode] Shortest Word Distance 最短单词距离
Given a list of words and two words word1 and word2, return the shortest distance between these two ...
- [LeetCode]题解(python):104 Maximum Depth of Binary Tree
题目来源 https://leetcode.com/problems/maximum-depth-of-binary-tree/ Given a binary tree, find its maxim ...
- [LeetCode] Short Encoding of Words 单词集的短编码
Given a list of words, we may encode it by writing a reference string S and a list of indexes A. For ...
随机推荐
- Skulpt在线模拟运行Python工具
1. Skulpt是一个完全依靠浏览器端模拟实现Python运行的工具 2. 不需要预处理.插件或服务器端支持,只需编写python并重新载入即可. 3. 由于代码完全是在浏览器中运行的,所以不必担心 ...
- LATEX Mathematical Symbols
原文地址:https://www.caam.rice.edu/~heinken/latex/symbols.pdf
- CSS学习笔记-过渡模块
过渡模块: 1.过渡三要素 1.1必须要有属性发生变化 1.2必须告诉系统哪个属性需要执行过渡效果 1.3必须告诉系统过渡效果持续时长 2.格式: ...
- 设计冲刺Design Sprint - 阅读记录
改进团队流程: 审查了头脑风暴 - brain storming的成果,真正付诸实践并且获得成功的想法并不是来自大喊大叫的头脑风暴.而是来自静下心来的一次思考. 1. 搭建舞台 在开始设计冲刺之前,你 ...
- RTP Payload Format for VP8 Video
整体结构 0 1 2 3 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 +-+-+-+-+-+-+-+-+-+-+-+ ...
- 【CentOS 7】CentOS 7各个版本镜像下载地址(转)
参考链接:https://www.centos.org/download/mirrors/ https://www.cnblogs.com/defineconst/p/11176593.html
- Python Poetry 学习和使用
Poetry是啥? 是一个Python虚拟环境和依赖管理工具,另外它还提供了包管理功能,比如打包和发布.可以用来管理python库和python程序. 安装Poetry curl -sSL https ...
- 安卓投屏助手(ARDC)最新版
安卓投屏助手(B1493) 1.兼容Android 10: 2.增加灭屏投屏功能: 3.增加显示鼠标位置功能; 4.增加了虚拟NaviBar功能: 5.捐赠界面增加QQ支付,移除Paypal,感谢大家 ...
- 【Java基础】JDBC简明教程
目录 1. 常用类 2. JDBC编程步骤 3. 事务处理 4. 数据库连接池 5. JDBC列子代码 6. 使用Apache的JDBC工具类 虽然在平时的开发过程中我们不会直接使JDBC的API来操 ...
- Java题库——Chapter10 面向对象思考
1)You can declare two variables with the same name in ________. 1) _______ A)a method one as a forma ...