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.

  • 相关阅读:
    docker笔记(1)
    解决MySQL8 #1227 – Access denied; you need (at least one of) the SYSTEM_USER privilege(s) for this ...
    nginx if判断&&和||写法
    泡泡后台Couchbase缓存使用经验分享
    ARTS-WEEK-007
    ARTS-WEEK-006
    ARTS-WEEK-005
    ARTS-WEEK-004
    ARTS-WEEK-003
    总结httpclient资源释放和连接复用
  • 原文地址:https://www.cnblogs.com/luntai/p/6130767.html
Copyright © 2011-2022 走看看