原题链接:https://leetcode-cn.com/problems/longest-string-chain/

动态规划

dp[i]表示在i处最长字符串链长度

状态转移

dp[i]=max(dp[i],dp[j]+1);  当words[j]属于字符串链

代码:

static bool lessLen(const string &a,const string b){return a.size()<b.size();
}int longestStrChain(vector<string>& words) {sort(words.begin(),words.end(),lessLen);//先对数组按长度排序int len=words.size();vector<int> dp(len,1);int ans=1;for(int i=1;i<len;i++){for(int j=0;j<i;j++){int len_i=words[i].size();int len_j=words[j].size();if(len_i-len_j!=1){continue;}else if(isStrChain(words[i],words[j])){         dp[i]=max(dp[i],dp[j]+1);//状态转移ans=max(ans,dp[i]);//记录最大值            }}}return ans;
}bool isStrChain(string a,string b){//判断是否属于字符串链int len_a=a.size();int len_b=b.size();for(int i=0;i<len_a;i++){string c=a;c.erase(i,1);if(c==b) return true;}return false;
}

判断是否属于字符串链还可以使用双指针法(更推荐)

bool isStrChain(string a,string b){int len_a=a.size();int len_b=b.size();int i=0,j=0;while(i<len_a&&j<len_b){if(a[i]==b[j]) j++;i++;}return j==len_b;
}

leetcode算法题--最长字符串链相关推荐

  1. leetcode算法题--最长数对链

    原题链接:https://leetcode-cn.com/problems/maximum-length-of-pair-chain/ 1.动态规划 dp[i]表示第i个位置最长的数对链的长度 状态转 ...

  2. leetcode算法题--最长快乐字符串★

    原题链接:https://leetcode-cn.com/problems/longest-happy-string/ 贪心算法 class Solution {public:static bool ...

  3. leetcode算法题--最长回文子序列★★

    原题链接:https://leetcode-cn.com/problems/longest-palindromic-subsequence/ 注意不是回文子串,子串必须是连续! dp[i][j]表示字 ...

  4. leetcode算法题--最长定差子序列

    原题链接:https://leetcode-cn.com/problems/longest-arithmetic-subsequence-of-given-difference/ 相关题目:最长等差数 ...

  5. leetcode算法题--最长等差数列★

    原题链接:https://leetcode-cn.com/problems/longest-arithmetic-sequence/ 动态规划 dp[i][dif]表示i位置上差为dif的等差数列的最 ...

  6. leetcode算法题--最长公共子数组

    原题链接:https://leetcode-cn.com/problems/maximum-length-of-repeated-subarray/ 相似题目:最长公共子序列 只不过这里必须是连续的, ...

  7. leetcode算法题--两个字符串的最小ASCII删除和★

    原题链接:https://leetcode-cn.com/problems/minimum-ascii-delete-sum-for-two-strings/ 相关题目:最长公共子序列 1.递归(超时 ...

  8. leetcode算法题--最长公共子序列★

    原题链接:https://leetcode-cn.com/problems/longest-common-subsequence/ 动态规划 dp[i][j]表示text1[:i]和text2[:j] ...

  9. leetcode算法题-- 最长递增子序列的个数★

    原题链接:https://leetcode-cn.com/problems/number-of-longest-increasing-subsequence/ 相关题目:最长上升子序列 lengths ...

最新文章

  1. ubuntu 16.04 分辨率只有800×600问题解决
  2. String中的“equal方法”和“==”
  3. 算成绩啦(洛谷P5740、P5741题题解,Java语言描述)
  4. HDU 4857 逃生 【拓扑排序+反向建图+优先队列】
  5. java_数组插入001
  6. java 时间回退_java.time DateTimeFormatter使用灵活的回退值进行解析
  7. 知乎推荐算法工程师面经
  8. 2020中关村论坛百度平行论坛:探索中国AI开源与产业智能化模式
  9. android 崩溃捕获_Android从相机和图库捕获图像
  10. 中国各地的特色茶,你知道几个?
  11. PyQt4对话框(dialog类型介绍)
  12. Android开源项目大合集(转载的基础上添加了项目地址)
  13. 解密weblogic控制台账号密码
  14. VS2017中处理命令行参数的方法----C++语言 Windows系统
  15. 计算机科学着重于理论和算法,大学计算机-中国大学mooc-题库零氪
  16. c语言程序设计教程+西安交通大学,大学C程序设计教程-西安交通大学.ppt
  17. 走近手球运动·体育项目
  18. 地图可视化数据处理技术在银行领域的应用
  19. 协程爬取整站豆瓣网络
  20. 基于springboot开发的停车场管理系统

热门文章

  1. python菜鸟基础教程-终于懂得python入门菜鸟教程
  2. 在电脑上安装python的步骤-python中pygame安装过程(超级详细)
  3. python好找工作吗2017-2017学什么编程语言好找工作?
  4. pythonurllib模块-Python3中核心模块urllib的用法大全
  5. python转行it好学吗-我研一想转行it,建议Java还是Python呢?
  6. python学习软件-python软件学习从入门到精通
  7. python pandas读取excel-python pandas 读取文件 写入文件excel
  8. Ubuntu用apt-get安装报错:E: Could not get lock /var/lib/dpkg/lock-frontend - open (11:资源暂时不可用)
  9. Windows和Ubuntu系统之间文件相互复制粘贴
  10. LeetCode Sort List(单链表归并排序)