zoukankan      html  css  js  c++  java
  • [Leetcode 83] 28 Implement strStr()

    Problem:

    Implement strStr().

    Returns a pointer to the first occurrence of needle in haystack, or null if needle is not part of haystack.

    Analysis:

    There are two ways: one is the naive comparing algorithm and the other is KMP algorithm.

    For KMP algorithm, please refer the wiki page

    For the naive algorithm, we start from the haystack's first position until the one plus needle's length is greater than the haystack's length.

    For each position, we compare it with the needle. If they are the same, we compare the next position and repeat this process. If they are not the same, we know this i-position won't give a match. So we just break out of the loop and go to the next i position. If we reach the end of the needle position, then there is a match. And return the i. This algoritm is of complexity O(m*n)

     A special case is that the given needle is of length 0. In this case, we directly return the haystack.

    Code:

    Naive algorithm:

     1 class Solution {
     2 public:
     3     char *strStr(char *haystack, char *needle) {
     4         // Start typing your C/C++ solution below
     5         // DO NOT write int main() function
     6         int hlen = strlen(haystack);
     7         int nlen = strlen(needle);
     8         
     9         for (int i=0; i+nlen <= hlen; i++) {
    10             int j = 0;
    11             
    12             for (j=0; j<nlen; j++) {
    13                 if (haystack[j+i] != needle[j])
    14                     break;
    15             }
    16             
    17             if (j == nlen) return haystack+i;
    18         }
    19         
    20         return NULL;
    21     }
    22 };
    View Code

    KMP algorithm:

     1 class Solution {
     2 public:
     3     char *strStr(char *haystack, char *needle) {
     4         // Start typing your C/C++ solution below
     5         // DO NOT write int main() function
     6         int n = strlen(haystack), m = strlen(needle);
     7         
     8         if (m == 0)
     9             return haystack;
    10         
    11         failure_function(needle);
    12         int i = 0, j = 0;
    13         while (true) {
    14             if (i == n)
    15                 break;
    16                 
    17             if (haystack[i] == needle[j]) {
    18                 i++;
    19                 j++;
    20                 if (j == m) 
    21                     return haystack + i - j;
    22             } else if (j > 0) {
    23                 j = ff[j];
    24             } else
    25                 i++;
    26         }
    27         
    28         return NULL;
    29     }
    30     
    31     
    32     void failure_function(char *s) {
    33         int n = strlen(s);
    34         
    35         ff = new int[n+1];
    36         
    37         ff[0] = ff[1] = 0;
    38         
    39         for (int i=2; i<=n; i++) {
    40             int j = ff[i-1];
    41             while (true) {
    42                 if (s[j] == s[i-1]) {
    43                     ff[i] = j + 1;
    44                     break;
    45                 }
    46                 
    47                 if (j == 0) {
    48                     ff[i] = 0;
    49                     break;
    50                 }
    51                      
    52                 j = ff[j];
    53             }
    54         }
    55     }
    56     
    57 private:
    58     int *ff;
    59 };
    View Code
  • 相关阅读:
    memcached命令
    模块管理常规功能自己定义系统的设计与实现(14--模块图表分析的设计)
    [易飞]凭证设计扩展字段之内容-文字显示格式
    将替代ListView的RecyclerView 的使用(一)
    POJ 2049— Finding Nemo(三维BFS)10/200
    最好用的jquery列表拖动排列(由项目提取)
    编程算法
    java几个easy出错的小程序
    GoldenGate配置(三)之DDL复制配置
    aused by: org.apache.xmlbeans.SchemaTypeLoaderException: XML-BEANS compiled schema: Incompatible min
  • 原文地址:https://www.cnblogs.com/freeneng/p/3209805.html
Copyright © 2011-2022 走看看