2018-09-01 22:50:59

问题描述:

问题求解:

如果单纯的遍历判断,那么如何去重保证unique是一个很困难的事情,事实上最初我就困在了这个点上。

后来发现是一个动态规划的问题,可以将每个字符结尾的最长长度进行保存,这样就巧妙的解决的重复的问题。

  1. The max number of unique substring ends with a letter equals to the length of max contiguous substring ends with that letter. Example "abcd", the max number of unique substring ends with 'd' is 4, apparently they are "abcd", "bcd", "cd" and "d".
  2. If there are overlapping, we only need to consider the longest one because it covers all the possible substrings. Example: "abcdbcd", the max number of unique substring ends with 'd' is 4 and all substrings formed by the 2nd "bcd" part are covered in the 4 substrings already.
  3. No matter how long is a contiguous substring in p, it is in s since s has infinite length.
  4. Now we know the max number of unique substrings in p ends with 'a', 'b', ..., 'z' and those substrings are all in s. Summary is the answer, according to the question.
    public int findSubstringInWraproundString(String p) {
int res = 0;
int[] dp = new int[26];
int maxLen = 0;
for (int i = 0; i < p.length(); i++) {
if (i > 0 && (p.charAt(i) - p.charAt(i - 1) == 1 || p.charAt(i - 1) - p.charAt(i) == 25)) {
maxLen++;
}
else maxLen = 1;
dp[p.charAt(i) - 'a'] = Math.max(dp[p.charAt(i) - 'a'], maxLen);
}
for (int i = 0; i < 26; i++) res += dp[i];
return res;
}

动态规划-独特的子字符串存在于Wraparound String总个数 Unique Substrings in Wraparound String的更多相关文章

  1. [LeetCode] Unique Substrings in Wraparound String 封装字符串中的独特子字符串

    Consider the string s to be the infinite wraparound string of "abcdefghijklmnopqrstuvwxyz" ...

  2. [Swift]LeetCode467. 环绕字符串中唯一的子字符串 | Unique Substrings in Wraparound String

    Consider the string s to be the infinite wraparound string of "abcdefghijklmnopqrstuvwxyz" ...

  3. 【LeetCode】467. Unique Substrings in Wraparound String 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/unique-s ...

  4. Leetcode: Unique Substrings in Wraparound String

    Consider the string s to be the infinite wraparound string of "abcdefghijklmnopqrstuvwxyz" ...

  5. 467. Unique Substrings in Wraparound String

    Consider the string s to be the infinite wraparound string of "abcdefghijklmnopqrstuvwxyz" ...

  6. LeetCode 467. Unique Substrings in Wraparound String

    Consider the string s to be the infinite wraparound string of "abcdefghijklmnopqrstuvwxyz" ...

  7. 【LeetCode】467. Unique Substrings in Wraparound String

    Consider the string s to be the infinite wraparound string of "abcdefghijklmnopqrstuvwxyz" ...

  8. 467 Unique Substrings in Wraparound String 封装字符串中的独特子字符串

    详见:https://leetcode.com/problems/unique-substrings-in-wraparound-string/description/ C++: class Solu ...

  9. 467. [leetcode] Unique Substrings in Wraparound String

    467. Unique Substrings in Wraparound String Implement atoi to convert a string to an integer. Hint: ...

随机推荐

  1. 新手安装Ubuntu操作系统

    新手安装 Ubuntu 操作系统 版权声明:未经博主授权,内容严禁转载分享! 最近学习linux编程,需要安装一个 Ubuntu 操作系统,由于虚拟机的体验不是很好,所以便在电脑上试下装双系统.嘿嘿. ...

  2. 阶段性总结(2017 June 10 - 2017 July 10)

    综述 我在全面剖析了自身的特质以后,针对于工程技术领域的经验丰富优势.科学研究领域的理论薄弱劣势.我制定了全面夯实科学研究理论基础的学习计划.全面提高效率抓大放小的精力分配原则. 执行成果 线性代数( ...

  3. 02: git分支管理

    目录:GIT其他篇 01: git & github 02: git分支管理 目录: 1.1 Git常用命令 1.2 主要分支(保护分支) 1.3 特性分支:feature (开发分支合并到d ...

  4. ajax,分页器

    一.ajax请求数据 ''' $.ajax({ url: '/ajax/', # 请求路径 type: 'post', # 请求方式 data: { # get和post都以data字典方式携带数据 ...

  5. CentOS 安装 Gitlab

    源地址 https://mirror.tuna.tsinghua.edu.cn/gitlab-ce/ # 清华源 https://mirrors.tuna.tsinghua.edu.cn/help/g ...

  6. 【做题】sgu189 Perl-like Substr——dark模拟

    注:这篇博客纯属为凑篇数而生. 题面较长,幸运的是,网上给出了相当不错的翻译. 需要支持的操作很简单,即对子串提取.赋值和输出,且对时间复杂度没有要求.换言之此题有成为块链毒瘤题的潜质.难点在于输入的 ...

  7. 使用Scapy向Mininet交换机注入流量 实验记录

    使用Scapy向Mininet交换机注入流量 实验记录 用Python脚本及Scapy库写了一个简单的流量生成脚本,并打算使用该脚本往Mininet中的OpenvSwitch交换机注入流量.拓扑图如下 ...

  8. python2.7 qt4

    https://jaist.dl.sourceforge.net/project/pyqt/PyQt4/PyQt-4.11.4/PyQt4-4.11.4-gpl-Py2.7-Qt4.8.7-x64.e ...

  9. 【NOI 2016】优秀的拆分

    Problem Description 如果一个字符串可以被拆分为 \(AABB\) 的形式,其中 \(A\) 和 \(B\) 是任意非空字符串,则我们称该字符串的这种拆分是优秀的. 例如,对于字符串 ...

  10. Leetcode167-Two Sum II Input array is sorted-Easy

    Given an array of integers that is already sorted in ascending order, find two numbers such that the ...