leetcode 691.Stickers to Spell Word
来源:互联网 发布:公安网络监控电路板 编辑:程序博客网 时间:2024/06/14 21:46
先附模仿代码 也是自己敲出来的
class Solution {
public:
int helper(vector<vector<int>> map,string target,unordered_map<string,int>&dp)
{
if (dp.count(target))return dp[target];
if(target.size()==0)
return0;
vector<int> tar(26,0);
for(char c:target)
tar[c-'a']++;
int re=0;
for(int j=0;j<=25;j++)
{
if(tar[j]==0)
continue;
int flag=0;
for(int i=0;i<=map.size()-1;i++)
{
if(map[i][j]>0)
{
flag=1;
break;
}
}
if(flag==0)
re=-1;
}
if(re==-1){
dp[target]=-1;
return -1;
}
int res=INT_MAX;
for(int i=0;i<=map.size()-1;i++)
{
if(map[i][target[0]-'a']==0)
continue;
//cout<<"第"<<i<<"次"<<endl;
string newone;
for(int j=0;j<=25;j++)
if(tar[j]>map[i][j])
newone+=string(tar[j]-map[i][j],'a'+j);
int k=helper(map, newone,dp)+1;
res= res<k? res:k;
}
dp[target]=res;
return res;
}
int minStickers(vector<string>& stickers,string target) {
int size=(int)stickers.size();
vector<vector<int>> map(size,vector<int>(26,0));
for(int i=0;i<=size-1;i++)
{
for(char c:stickers[i])
map[i][c-'a']++;
}
unordered_map<string,int>dp;
dp[""]=0;
returnhelper(map, target,dp);
}
};
代码很乱 还需要编辑 再写一下思路 不过现在脑子有点乱 还是稍后再编辑
- leetcode 691.Stickers to Spell Word
- 691. Stickers to Spell Word
- leetcode 691. Stickers to Spell Word(状压dp)
- leetcode -- 691. Stickers to Spell Word【动态规划 + 位图使用】
- LWC 53:691. Stickers to Spell Word
- leetcode691 Stickers to Spell Word 带记忆功能的回溯
- Stickers to Spell Word:多个单词拆拼接成完整以个目标单词所耗最小数目
- [LeetCode 273] Integer to English Word
- leetcode Add to List 425. Word Squares
- 读整数--leetcode Integer to English Word
- UVALive 6510 Stickers
- 10003---背诵为王-02-02-Too Difficult to Spell
- [Leetcode] Word Break、Word BreakII
- leetcode Word Break & Word Break ||
- word to pdf
- word to txt
- word to txt
- word to sql
- 每天回顾linux命令(unlink)
- 9.简单格式Excel的读取
- 解决VS中出现_ASSERTE(_BLOCK_TYPE_IS_VALID(pHead->nBlockUse))的问题
- APP中嵌入Android车牌识别算法的应用
- less学习(四)—关于Extend
- leetcode 691.Stickers to Spell Word
- 初学vertx
- 网易号指数介绍
- linux rinetd、socat端口转发部署(很实用的网络工具)
- 51Nod 1002 数塔取数问题
- Several ports (8005, 8080, 8009) required by demo are already in use. The server may already be runn
- 机器学习基石-08-4-Weighted Classification
- String的不可变
- Log4Net 在项目中的使用