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.

  • 相关阅读:
    Maven项目中的配置文件找不到以及打包问题
    企业信息化快速开发平台 JeeSite
    http缓存浅谈
    <mvc:annotation-driven />注解意义
    Spring中报"Could not resolve placeholder"的解决方案
    eclipse怎么停止building workspace
    利用mybatis-generator自动生成代码
    Java 8 中的 Streams API 详解
    tomca配置文件自动还原问题的解决 server.xml content.xml 等
    tomcat 修改默认字符集
  • 原文地址:https://www.cnblogs.com/luntai/p/6130767.html
Copyright © 2011-2022 走看看