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.

  • 相关阅读:
    H5,JS国际化网站中英文切换
    DIV右上角标签的CSS3实现技巧
    .NET Core也是国产化信息系统开发的重要选项
    微软自家的.Net下的JavaScript引擎--- ClearScript
    Java 生态碎片化 和 .NET生态的一致性
    使用 .NET 进行游戏开发
    .NET 是信息技术应用创新产业重要参与者
    Chrome 80+ 跨域Samesite 导致的cookie not found 解决方法
    在腾讯云云函数计算上部署.NET Core 3.1
    腾讯云 云开发 部署 Blazor网站
  • 原文地址:https://www.cnblogs.com/luntai/p/6130767.html
Copyright © 2011-2022 走看看