LeetCode 5258. 得分最高的单词集合 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]。
本场游戏的「得分」是指:玩家所拼写出的单词集合里包含的所有字母的得分之和。

样例

示例 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] 只包含小写的英文字母。

算法1
(暴力枚举)
DFS 暴力枚举 是的 就是暴力
也许出题人是想做动态规划?? 然后降低了数据量么?

 1 class Solution {
 2 public:
 3
 4 int ret = 0;
 5 void transferToArr(const vector<string>& words, vector<vector<int>>& wordsArr,
 6     const  vector<char>& letters, vector<int>& lettersArr)
 7 {
 8     for (int i = 0; i < words.size(); i++) {
 9         vector<int> tmp(26);
10         for (int j = 0; j < words[i].size(); j++) {
11             int idx = words[i][j] - ‘a‘;
12             tmp[idx]++;
13         }
14         wordsArr.push_back(tmp);
15     }
16
17     for (int i = 0; i < letters.size(); i++) {
18         int idx = letters[i] - ‘a‘;
19         lettersArr[idx]++;
20     }
21 }
22
23 int SelectWord(vector<int> wordCount, vector<int>& lettersArr, vector<int>& score)
24 {
25     int retScore = 0;
26     for (int i = 0; i < wordCount.size(); i++) {
27         if (wordCount[i] != 0) {
28             if (wordCount[i] > lettersArr[i])
29                 return 0;
30             lettersArr[i] -= wordCount[i];
31             retScore += score[i] * wordCount[i];
32         }
33     }
34
35     return retScore;
36 }
37
38 void DFS(vector<vector<int>>& wordsArr, int idx, vector<int> lettersArr, int currScore, vector<int>& score)
39 {
40     if (idx >= wordsArr.size()) {
41         ret = max(ret, currScore);
42         return;
43     }
44
45     vector<int> copyLettersArr = lettersArr;
46     int addscore = SelectWord(wordsArr[idx], lettersArr, score);
47     if (addscore != 0)
48         DFS(wordsArr, idx+1, lettersArr, currScore + addscore, score);
49     //这里尝试的是不放入该单词的组合
50     DFS(wordsArr, idx + 1, copyLettersArr, currScore, score);
51 }
52
53 int maxScoreWords(vector<string>& words, vector<char>& letters, vector<int>& score) {
54
55     vector<vector<int>> wordsArr;
56     vector<int> lettersArr(26);
57     //将提出给出的变量 转化成字符索引 多少个字符为值得数组
58     transferToArr(words, wordsArr, letters, lettersArr);
59     DFS(wordsArr, 0, lettersArr, 0, score);
60     return ret;
61 }
62
63 };

原文地址:https://www.cnblogs.com/itdef/p/11832246.html

时间: 2024-08-01 21:38:08

LeetCode 5258. 得分最高的单词集合 Maximum Score Words Formed by Letters的相关文章

leetcode -day9 Candy &amp; Gas Station &amp; Binary Tree Maximum Path Sum

1.  Candy There are N children standing in a line. Each child is assigned a rating value. You are giving candies to these children subjected to the following requirements: Each child must have at least one candy. Children with a higher rating get m

[LeetCode] Valid Word Square 验证单词平方

Given a sequence of words, check whether it forms a valid word square. A sequence of words forms a valid word square if the kth row and column read the exact same string, where 0 ≤k < max(numRows, numColumns). Note: The number of words given is at le

[LeetCode] Valid Word Abbreviation 验证单词缩写

Given a non-empty string s and an abbreviation abbr, return whether the string matches with the given abbreviation. A string such as "word" contains only the following valid abbreviations: ["word", "1ord", "w1rd", &

[LeetCode]题解(python):104 Maximum Depth of Binary Tree

题目来源 https://leetcode.com/problems/maximum-depth-of-binary-tree/ Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. 题意分析 Input: tree Output:

LeetCode 290 Word Pattern(单词模式)(istringstream、vector、map)(*)

翻译 给定一个模式,和一个字符串str.返回str是否符合同样的模式. 这里的符合意味着全然的匹配,所以这是一个一对多的映射,在pattern中是一个字母.在str中是一个为空的单词. 比如: pattern = "abba". str = "dog cat cat dog" 应该返回真. pattern = "abba", str = "dog cat cat fish" 应该返回假. pattern = "aaa

leetcode 139 word break (单词拆分)

一开始的错误答案与错误思路,幻想直接遍历得出答案: 1 class Solution { 2 public: 3 bool wordBreak(string s, vector<string>& wordDict) { 4 for(int i;i<s.size();i++){ 5 int step=0; 6 for(int j;j<wordDict.size();j++){ 7 if(s.substr(i,wordDict[j].size())==wordDict[j]){

leetcode 211. 添加与搜索单词 - 数据结构设计 解题报告

设计一个支持以下两种操作的数据结构: void addWord(word) bool search(word) search(word) 可以搜索文字或正则表达式字符串,字符串只包含字母 . 或 a-z . . 可以表示任何一个字母. 示例: addWord("bad") addWord("dad") addWord("mad") search("pad") -> false search("bad"

力扣(LeetCode)字符串中的单词数 个人题解

统计字符串中的单词个数,这里的单词指的是连续的不是空格的字符. 请注意,你可以假定字符串里不包括任何不可打印的字符. 示例: 输入: "Hello, my name is John" 输出: 5 题目描述比较不清楚,这里只要是用空格隔开的一律当作字符,包括非字母.使用JAVA自带库函数解决问题.记得忽略空格情况 当然这里使用了较大的内存保存分割后的ss字符串数组,如果对内存比较敏感的可以对字符串手动以空格划分.(这里空格可能多个,所以可以使用正则表达式较为方便去匹配) 代码如下: cl

LeetCode 翻转字符串里的单词

给定一个字符串,逐个翻转字符串中的每个单词. 示例 1: 输入: "the sky is blue" 输出: "blue is sky the" 示例 2: 输入: "  hello world!  " 输出: "world! hello" 解释: 输入字符串可以在前面或者后面包含多余的空格,但是反转后的字符不能包括. 示例 3: 输入: "a good   example" 输出: "examp