zoukankan      html  css  js  c++  java
  • 【LeetCode】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 out how many unique non-empty substrings of p are present in s. In particular, your input is the string p and you need to output the number of different non-empty substrings of p in the string s.

    Note: p consists of only lowercase English letters.

    Example 1:

    Input: "a"
    Output: 1
    
    Explanation: Only the substring "a" of string "a" is in the string s.
    

    Example 2:

    Input: "cac"
    Output: 2
    Explanation: There are two substrings "a", "c" of string "cac" in the string s.
    

    Example 3:

    Input: "zab"
    Output: 6
    Explanation: There are six substrings "z", "a", "b", "za", "ab", "zab" of string "zab" in the string s.
    

      

      This is the third problem in weekly contest 11. It's true that there are many brilliant pipo. Hope next time is me.

      Causing there are too many common substrs at the end of any characters among 'a' - 'z'. How can I count them without repeating.

      A simply solution is calculating every sub-string in p and finding out if it is a common sub-string with s. But its too expensive in time which is O(n*n*n).

      The brilliant ideal is using one loop to travel p and using a  variable "pos" to record the mis-match position, thus we can get the maximum length of common string ending at every character('a' - 'z')。Finally,we count the number of common sub-string by the length (num = length). For example, "abc"'s max common substring len is 3. Common substrings are "abc" "bc" "c" .

      Here comes the code:

    class Solution {
    public:
        int findSubstringInWraproundString(string p) {
            int lenp = p.length(), cnt = 0, pos = 0;
            if(lenp <= 0) return 0;
            vector<int>length(256, 0);  /*length['a']:用来记录以'a'作为结尾的公共子串的最大长度 (因为结尾固定,按照相同的规律发展,长度越长那么细分出来的公共子串数目 = length 越多) */
            p += '#';
            lenp = p.length();
            for(int i = 1; i < lenp; i ++){
                if((p[i] - 'a') % 26 != (p[i - 1] - 'a' + 1) % 26){
                    for(int j = pos; j < i; j ++)
                        length[p[j]] = max(length[p[j]], j - pos + 1);
                    pos = i;
                }
            }
            for(int c = 'a'; c <= 'z'; c ++){
                cnt += length[c];
            }
            return cnt;
        }
    };

      Do not waste any second in your life. Be strong , be confident.

  • 相关阅读:
    你必须知道的关于大数据的七个概念
    SAS中的聚类分析方法总结
    SAS中的聚类分析方法总结
    数据分析的关键是制定聪明的决策
    数据分析的关键是制定聪明的决策
    想使用 MongoDB ,你应该了解这8个方面!
    利用crtmpserver搭建rtmp服务器
    【leetcode】Jump Game I, II 跳跃游戏一和二
    iOS中从零開始使用protobuf
    session.use_cookies有什么作用,
  • 原文地址:https://www.cnblogs.com/luntai/p/6130767.html
Copyright © 2011-2022 走看看