leetcode_1048. Longest String Chain_[DP,动态规划,记忆化搜索]
1048. Longest String Chain
https://leetcode.com/problems/longest-string-chain/
Let's say word1
is a predecessor of word2
if and only if we can add exactly one letter anywhere in word1
to make it equal to word2
. For example, "abc"
is a predecessor of "abac"
.
A word chain is a sequence of words [word_1, word_2, ..., word_k]
with k >= 1
, where word_1
is a predecessor of word_2
, word_2
is a predecessor of word_3
, and so on.
Return the longest possible length of a word chain with words chosen from the given list of words
.
解法:动态规划
对于任意word,任意删去其中一个字母后为word',若word'在words中,则dp[word] = max(dp[word], dp[word'] + 1)。
先将所有words按长度存储。在计算dp[word]时,先将words中长度为word.size()-1的单词放入一个set,方便word'是否在words中。
class Solution
{
public:
int longestStrChain(vector<string>& words)
{
vector<vector<string>> len_word(, vector<string>());
for(auto word:words)
len_word[word.size()].push_back(word);
map<string,int> dp;
int res=;
for(int i=;i>=;i--)
{
if(i<res)
break;
for(auto word:len_word[i])
res = max(res,dfs(len_word,word,dp));
}
return res;
}
int dfs(vector<vector<string>>& len_word,string& nowword, map<string,int>& dp)
{
//cout<<nowword<<endl;
if(dp.count(nowword))
return dp[nowword];
set<string> Set;
for(auto word:len_word[nowword.size()-])
Set.insert(word);
int nowres=;
for(int i=; i<nowword.size(); i++)
{
string temp=nowword;
temp.erase(i,);
if(Set.count(temp))
nowres = max(nowres,dfs(len_word,temp,dp)+);
}
return dp[nowword]=nowres;
}
};
leetcode_1048. Longest String Chain_[DP,动态规划,记忆化搜索]的更多相关文章
- 蓝桥杯历届试题 地宫取宝 dp or 记忆化搜索
问题描述 X 国王有一个地宫宝库.是 n x m 个格子的矩阵.每个格子放一件宝贝.每个宝贝贴着价值标签. 地宫的入口在左上角,出口在右下角. 小明被带到地宫的入口,国王要求他只能向右或向下行走. 走 ...
- sicily 1176. Two Ends (Top-down 动态规划+记忆化搜索 v.s. Bottom-up 动态规划)
Description In the two-player game "Two Ends", an even number of cards is laid out in a ro ...
- 二进制数(dp,记忆化搜索)
二进制数(dp,记忆化搜索) 给定k个<=1e6的正整数x(k不大于10),问最小的,能被x整除且只由01组成的数. 首先,dp很好写.用\(f[i][j]\)表示i位01串,模ki的值是j的数 ...
- poj1179 区间dp(记忆化搜索写法)有巨坑!
http://poj.org/problem?id=1179 Description Polygon is a game for one player that starts on a polygon ...
- 【bzoj1415】【聪聪和可可】期望dp(记忆化搜索)+最短路
[pixiv] https://www.pixiv.net/member_illust.php?mode=medium&illust_id=57148470 Descrition 首先很明显是 ...
- 非常完整的线性DP及记忆化搜索讲义
基础概念 我们之前的课程当中接触了最基础的动态规划. 动态规划最重要的就是找到一个状态和状态转移方程. 除此之外,动态规划问题分析中还有一些重要性质,如:重叠子问题.最优子结构.无后效性等. 最优子结 ...
- Codevs_1017_乘积最大_(划分型动态规划/记忆化搜索)
描述 http://codevs.cn/problem/1017/ 给出一个n位数,在数字中间添加k个乘号,使得最终的乘积最大. 1017 乘积最大 2000年NOIP全国联赛普及组NOIP全国联赛提 ...
- UVA 10285 Longest Run on a Snowboard(记忆化搜索)
Problem C Longest Run on a Snowboard Input: standard input Output: standard output Time Limit: 5 sec ...
- UVA1351-----String Compression-----区间DP(记忆化搜索实现)
本文出自:http://blog.csdn.net/dr5459 题目地址: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&a ...
随机推荐
- c/c++面试30-38之指针
30 看代码写结果-----指针加减 #include <stdio.h> int main(void) { ] = { , , , , }; );//这里要特别注意,&a+1的值 ...
- springboot+mongodb报错Caused by: java.net.ConnectException: Connection refused (Connection refused)
com.mongodb.MongoSocketOpenException: Exception opening socket at com.mongodb.connection.SocketStrea ...
- .NET 5 - 下一代.NET
不知不觉中微软已经计划推出了下一代的.NET了,我们先来看一下新的.NET有包含什么 What's new in .NET 5? .NET 5将会引入新的APIs,运行时功能和新的语言特色. 在运行时 ...
- apringboot aop权限控制
+ 定义切面: ···@Aspect @Component public class LoginInterceptor { @Around("@annotation(lock)") ...
- (转)关于MongoDB你需要知道的几件事
本文列举了颇让作者困惑的一些MongoDB限制,如果你也打算使用MongoDB,那么至少要提前了解这些限制,以免遇到的时候措手不及. 消耗磁盘空间 这是我的第一个困惑:MongoDB会消耗太多的磁盘空 ...
- POI刷题记录
POI2007 HNOI2018滚粗后,默默来刷POI 先从2007刷起 bzoj1103[POI2007]大都市meg bzoj1098[POI2007]办公楼biu bzoj1102[POI200 ...
- DBUtils学习一 增删该查
package com.mozq.jdbc.test; import java.sql.SQLException; import java.util.List; import java.util.Ma ...
- PostgreSQL - 用psql 运行SQL文件
对于预先写好的SQL文件,比如/home/user1/updateMyData.sql, 可以有两种方式来运行这个SQL文件. 方式一:连接db后执行SQL文件 首先通过psql连接到对应的db: p ...
- python快排
代码: def partition(data,left,right): tmp = data[left] while left<right: while left < right and ...
- a标签中href=""的几种用法
http://blog.csdn.net/u010297791/article/details/52784879 这是分页上的 <?php function pages($page,$e_pag ...