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.

  • 相关阅读:
    Java8新特性之Collectors
    java日期的运用(DateUtils工具类)
    正则表达式30分钟入门教程
    一篇非常好的Spring教程
    结合实际需求,在webapi内利用WebSocket建立单向的消息推送平台,让A页面和服务端建立WebSocket连接,让其他页面可以及时给A页面推送消息
    关于企业微信对接内部应用开发,access_token的管理机制和业务接口调用项目实战的八个要点
    企业微信使用DevTools但显示为空白,解决方法
    16.刚体碰撞事件监测与处理
    15.碰撞体
    14.刚体组件Rigidbody
  • 原文地址:https://www.cnblogs.com/luntai/p/6130767.html
Copyright © 2011-2022 走看看