题目来源:https://leetcode.com/problems/find-the-shortest-superstring/description/

标记难度:Hard

提交次数:3/4

代码效率:2.93% -> 79.31%

题意

有N个字符串,找到最小的字符串S,使得这N个字符串都是S的子串。其中N<=12,字符串的长度<=20。

分析

这道题比赛的时候我没有做出来,但我自认为自己已经找到了正确的解法(确实差不多是正确的),只要再调一小会就能调出来了!结果事实是又花了两天才弄出来。我犯了这些错误:

  • 在搞错了状态变量的范围的同时没有设置好变量的初值
  • 计算两个字符串的overlap的函数少考虑了一种情况

所以就这样了……


我觉得比较简单的方法还是状态压缩DP。[1]dp[mask][i]表示总共包含mask这些字符串,且以A[i]作为结尾的字符串的最小长度(或者最大overlap长度;当字符串都是那么多时,这两者是一样的。然后就可以递推了:dp[mask ^ 1<<j][j] = max(dp[mask][i] + overlap(i, j))。显然,我们事实上可以不用保存具体的字符串(因为有最后一个字符串就够用了),而且可以事先计算出每两个字符串之间的overlap(这样就不需要重复计算)。不过这样就需要最后重建DP过程了……不过字符串处理过程太耗时了,也可以理解……

不过这样做了之后时间效率大大提高了(从1324ms提高到了28ms)

代码

特别慢的那个就不贴了……

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
class  {
private:
inline int calcOverlap(const string& s1, const string& s2) {
int start = s1.length() - s2.length();
start = max(0, start);
for (int i = start; i < s1.length(); i++) {
int len = s1.length() - i;
if (s1.substr(i, len) == s2.substr(0, len))
return len;
}
return 0;
} inline bool contains(int cnt, int i) {
return (cnt & (1 << i)) != 0;
}大专栏  Leetcode 943. Find the Shortest Superstring(DP)> public:
string shortestSuperstring(vector<string>& A) {
int n = A.size();
if (n == 1) return A[0]; int overlap[n][n];
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
overlap[i][j] = calcOverlap(A[i], A[j]); const int MAX_CNT = (1 << n);
int f[MAX_CNT][n], parent[MAX_CNT][n];
for (int i = 0; i < n; i++) {
f[1 << i][i] = 0;
parent[1 << i][i] = -1;
} // start DP
int ans = -1;
int p = -1;
for (int cnt = 2; cnt <= n; cnt++) {
for (int i = 0; i < MAX_CNT; i++) {
if (__builtin_popcount(i) != cnt) continue;
// ends with j
for (int j = 0; j < n; j++) {
if (!contains(i, j)) continue;
f[i][j] = -1; int nmask = i ^ (1 << j);
// last one ends with k
for (int k = 0; k < n; k++) {
if (!contains(nmask, k)) continue;
if (f[nmask][k] + overlap[k][j] > f[i][j]) {
f[i][j] = f[nmask][k] + overlap[k][j];
parent[i][j] = k;
}
} if (cnt == n && f[i][j] > ans) {
ans = f[i][j];
p = j;
}
}
}
} // rebuild the path
string str;
int nmask = MAX_CNT - 1;
while (true) {
int par = parent[nmask][p];
if (par == -1) {
str = A[p] + str;
break;
}
str = A[p].substr(overlap[par][p], A[p].length() - overlap[par][p]) + str;
nmask ^= (1 << p);
p = par;
} return str;
}
};

  1. Leetcode Official Solution for 943. Find the Shortest Superstring

Leetcode 943. Find the Shortest Superstring(DP)的更多相关文章

  1. 由Leetcode详解算法 之 动态规划(DP)

    因为最近一段时间接触了一些Leetcode上的题目,发现许多题目的解题思路相似,从中其实可以了解某类算法的一些应用场景. 这个随笔系列就是我尝试的分析总结,希望也能给大家一些启发. 动态规划的基本概念 ...

  2. 【LeetCode】10.Regular Expression Matching(dp)

    [题意] 给两个字符串s和p,判断s是否能用p进行匹配. [题解] dp[i][j]表示s的前i个是否能被p的前j个匹配. 首先可以分成3大类情况,我们先从简单的看起: (1)s[i - 1] = p ...

  3. LeetCode House Robber 家庭劫犯(dp)

    题意:有一个整数序列,从中挑出一些数字,使得总和是最大,前提是,相邻的两个数字中只能挑其一.比如1 2 3 就只能挑2或者1和3. 思路:很直观的题,dp思想.降低规模,从小规模开始考虑.如果只有两个 ...

  4. Java实现 LeetCode 823 带因子的二叉树(DP)

    823. 带因子的二叉树 给出一个含有不重复整数元素的数组,每个整数均大于 1. 我们用这些整数来构建二叉树,每个整数可以使用任意次数. 其中:每个非叶结点的值应等于它的两个子结点的值的乘积. 满足条 ...

  5. Leetcode之动态规划(DP)专题-详解983. 最低票价(Minimum Cost For Tickets)

    Leetcode之动态规划(DP)专题-983. 最低票价(Minimum Cost For Tickets) 在一个火车旅行很受欢迎的国度,你提前一年计划了一些火车旅行.在接下来的一年里,你要旅行的 ...

  6. Leetcode之动态规划(DP)专题-647. 回文子串(Palindromic Substrings)

    Leetcode之动态规划(DP)专题-647. 回文子串(Palindromic Substrings) 给定一个字符串,你的任务是计算这个字符串中有多少个回文子串. 具有不同开始位置或结束位置的子 ...

  7. Leetcode之动态规划(DP)专题-474. 一和零(Ones and Zeroes)

    Leetcode之动态规划(DP)专题-474. 一和零(Ones and Zeroes) 在计算机界中,我们总是追求用有限的资源获取最大的收益. 现在,假设你分别支配着 m 个 0 和 n 个 1. ...

  8. Leetcode之动态规划(DP)专题-486. 预测赢家(Predict the Winner)

    Leetcode之动态规划(DP)专题-486. 预测赢家(Predict the Winner) 给定一个表示分数的非负整数数组. 玩家1从数组任意一端拿取一个分数,随后玩家2继续从剩余数组任意一端 ...

  9. Leetcode之动态规划(DP)专题-264. 丑数 II(Ugly Number II)

    Leetcode之动态规划(DP)专题-264. 丑数 II(Ugly Number II) 编写一个程序,找出第 n 个丑数. 丑数就是只包含质因数 2, 3, 5 的正整数. 示例: 输入: n ...

随机推荐

  1. 自定义EL函数(转)

    有看到一个有趣的应用了,转下来,呵呵!! 1.定义类MyFunction(注意:方法必须为 public static) package com.tgb.jstl;         /**     * ...

  2. 爬虫笔记(九)——安装Fiddler

    在ubuntu下不能直接安装Fiddler,我们要先安装mono环境,具体可分为三个步骤: 1.   在终端下输入指令安装mono环境 :sudo apt-get install mono-compl ...

  3. 如何让网站HTTPS评级为A或者A+

    环境说明:CentOS Linux release 7.5.1804 (Core).nginx/1.10.0 需求:公司网站在myssl的评级只得到了B的评分,需要提升至A+ 具体操作如下: 一.ng ...

  4. A component required a bean named xxx that could not be found. Action: Consider defining

    0 环境 系统:win10 1 正文 https://stackoverflow.com/questions/44474367/field-in-com-xxx-required-a-bean-of- ...

  5. 树分治(挑战p360)

    poj1741 题:http://poj.org/problem?id=1741 #include<iostream> #include<algorithm> #include ...

  6. Office 365管理员添加自定义域名

    添加自定义域,以便Office 365允许更短.更熟悉的的电子邮件或用户ID用于服务 一.Office 365小型企业版添加自定义域名 1.使用Office 365管理员账户登陆到由世纪互联运营的Of ...

  7. [LC] 46. Permutations

    Given a collection of distinct integers, return all possible permutations. Example: Input: [1,2,3] O ...

  8. SpringMVC静态资源拦截的问题

    通常在web.xml中的核心控制器的DispatcherServlet中的url-pattern属性配置成类似“/”的拦截路径,但是会出现静态资源找不到的问题,比如js脚本.图片.css等无法加载,那 ...

  9. MOOC(7)- case依赖、读取json配置文件进行多个接口请求-封装mock(9)

    封装mock # -*- coding: utf-8 -*- # @Time : 2020/2/12 8:51 # @File : mock_demo_9.py # @Author: Hero Liu ...

  10. hadoop配置文件详解、安装及相关操作补充版

    一.      Hadoop伪分布配置           首先应该现在profile文件添加hadoop_home操作如下: export JAVA_HOME=/usr/java/jdk expor ...