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 ...
随机推荐
- mysql-5.7.27安装
1,下载5.7.27安装包 百度网盘 (注:5.7.27要安装net faframework4.5.2) 2,创建my.ini及data文件 下载5.7.27后解压,在创建my.ini文件及d ...
- springcloud~配置中心~对敏感信息加密
简介 RSA非对称加密有着非常强大的安全性,HTTPS的SSL加密就是使用这种方法进行HTTPS请求加密传输的.因为RSA算法会涉及Private Key和Public Key分别用来加密和解密,所以 ...
- C lang:Array_Multidimensional arrays
#include<stdio.h> #include<windows.h> #define YEARS 5 #define MONTHS 12 void color(short ...
- [Go] 解决golang.org模块无法下载的问题
使用GOPROXY环境变量解决proxy.golang.org无法访问问题 在/etc/profile中增加 export GOPROXY=https://goproxy.cn windows下使用 ...
- BITCTF-MISC
MISC 以此笔记来记录本菜鸡做misc的历程 签到85 首先看题 提示base85 打开kali,使用python的base64库来解码(内有base85解码) (其实只要输python3即可 我还 ...
- 菜鸟刷面试题(一、Java基础篇)
目录: JDK 和 JRE 有什么区别? == 和 equals 的区别是什么? 两个对象的 hashCode()相同,则 equals()也一定为 true,对吗? final 在 java 中有什 ...
- C++ std::array 基本用法
#include <iostream> #include <string> #include <array> using namespace std; // htt ...
- Python文件操作:文件的打开关闭读取写入
Python文件操作:文件的打开关闭读取写入 一.文件的打开关闭 Python能以文本和二进制两种方式处理文件,本文主要讨论在Python3中文本文件的操作. 文件操作都分为以下几个步骤: 1.打开文 ...
- JQ的offset().top与JS的getBoundingClientRect区别详解,JS获取元素距离视窗顶部可变距离
壹 ❀ 引 我在 JQ的offset().top与js的offsetTop区别详解 这篇博客中详细分析了JQ方法offset().top与JS属性offsetTop的区别,并得出了一条offset( ...
- 关于ASP.NET配置
字符串加密打开Vs的开发人员命令提示符 //加密web.config文件的连接字符串aspnet_regiis.exe -pef "connectionStrings" " ...