动态规划-独特的子字符串存在于Wraparound String总个数 Unique Substrings in Wraparound String

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;
    }

原文地址:https://www.cnblogs.com/TIMHY/p/9572041.html

时间: 2024-10-20 12:06:59

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

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

Consider the string s to be the infinite wraparound string of "abcdefghijklmnopqrstuvwxyz", so s will look like this: "...zabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcd....". Now we have another string p. Your job is to find

[Leetcode] DP-- 467. Unique Substrings in Wraparound String

Consider the string s to be the infinite wraparound string of "abcdefghijklmnopqrstuvwxyz", so s will look like this: "...zabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcd....". Now we have another string p. Your job is to find

467. Unique Substrings in Wraparound String

https://leetcode.com/problems/unique-substrings-in-wraparound-string/#/description Consider the string s to be the infinite wraparound string of "abcdefghijklmnopqrstuvwxyz", so s will look like this: "...zabcdefghijklmnopqrstuvwxyzabcdefgh

LeetCode Unique Substrings in Wraparound String

原题链接在这里:https://leetcode.com/problems/unique-substrings-in-wraparound-string/description/ 题目: Consider the string s to be the infinite wraparound string of "abcdefghijklmnopqrstuvwxyz", so s will look like this: "...zabcdefghijklmnopqrstuvw

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

详见:https://leetcode.com/problems/unique-substrings-in-wraparound-string/description/ C++: class Solution { public: int findSubstringInWraproundString(string p) { vector<int> cnt(26, 0); int len = 0; for (int i = 0; i < p.size(); ++i) { if (i >

LeetCode &quot;467. Unique Substrings in Wraparound String&quot; !!

Check out this brilliant solution:https://discuss.leetcode.com/topic/70658/concise-java-solution-using-dp My first solution was a O(n^2) typical DP boiler-plate code. The key is, that solution is too generalized. With given char as the ending char in

动态规划--之--最长公共子字符串

package 动态规划;import java.util.Scanner;public class LogestCommonZiXuLie { public static void main(String[] args)     {      Scanner scan = new Scanner(System.in);      while(scan.hasNextLine())        {          String str = scan.nextLine();         

[经典] 最X(长 | 大和 | 大积)Y(子序列 | 子字符串)

Note: 子序列,可以不连续:子字符串,必须连续. 以下题目按在我看看来的难度从易到难排列: 最大和子序列(Maximum sum subsequence) 这道题纯属娱乐...应该不会有人出这种题吧.方案是遇到正数就放入序列. vector<int> msseq(vector<int> &num) { vector<int> result; for(int i : num) if(i > 0) result.push_back(i); return r

字符串匹配算法一:查找子字符串

[题目] 就是给一个很长的字符串str 还有一个字符集比如{a,b,c} 找出str里包含{a,b,c}的最短子串.要求O(n). [例子] 字符集是a,b,c,字符串是abdcaabcx,则最短子串为abc. [分析] 有题意可知,满足要求的字符串只需要包括字符集中的所有字符,并没有顺序要求 当然最容易想到的是做一个字符匹配的过程,但题目要求查找次数为O(n),在思考了几种解决方法后,觉得下面的方案能够达到要求,虽然需要一些额外的空间. 下面我给出自己的解决方案,难免有遗漏的地方,如果路过的朋