下面是KMP算法的实现伪代码:

KMP_MATCHER ( T, P )
. n = T.length
. m = P.length
. next = COMPUTE_PREFIX_FUNCTION ( P )
. q = //number of characters matched
. for i = to n //scan the text from left to right
. while q > and P [q+] <> T[i]
//next character does not match
. q = next[q]
. if P[q+ ] == T [i] // next character matches
. q = q+
. if q == m // is all the characters in P matched
. finish
. q = next[q] // look for the next matcher COMPUTE_PREFIX_FUNCTION ( P )
m = P.length
let next be a new array with length of m
next[] =
k =
for q = to m
while k> and P[k+] <> P[q]
k = next [k]
if P[k+] == P[q]
k = k+
next[q] = k
return next

在不同的情况下,对应的next下标是不相同的,当然和个人编写代码的习惯也是有很大的关系,

下面的代码是实现 对 子串 进行 next 的初始化的代码,

在实现中,next 和 子串 的下标 是从 0开始计算的。

#include <stdio.h>
#include <string.h> int main ( )
{
int len ;
char S[] ;
int next[] ; int k , i ; memset( S , , sizeof(S) ) ;
memset (next , , sizeof (next) ) ; scanf("%s" , S) ; len = strlen(S) ; next[] = ; k = ; for ( i = ; i < len ; i++ )
{
while ( k > && S[k] != S [i] )
{
k = next[k] ;
}
if ( S[k] == S[i] )
{
k = k+ ;
} next[i] = k ;
} for ( i = ; i < len ; i++ )
{
printf("next[%d] : %d\n", i,next[i]) ;
} return ;
}

而下面的代码实现的是, 基于KMP 算法的 对字符串的匹配 代码。

LZ 并没有将 next 数组初始化操作写成一个 单独的函数,而是将它作为 整个main 函数中的一个处理步骤。

#include <stdio.h>
#include <string.h> int main ()
{
int len_s , len_t ;
char S[], T[] ;
int next[] ; int k, i ; scanf("%s %s", S , T) ; len_s = strlen(S) ;
len_t = strlen(T) ; memset(next, , sizeof(next) ) ; //initial next array next[] = ; for (k = , i = ; i < len_s ; i++ )
{
while ( k > && S[k] != S[i] )
k = next[k] ;
if ( S[k] == S[i] )
k = k+ ;
next[i] = k ;
} //show next
for ( i = ; i < len_s ; i++ )
{
printf(" next %d \n", next[i]);
} //Main match for (k= , i = ; i < len_t ; i++ )
{
while ( k > && S[k] != T [i] )
k = next[k] ;
if ( S[k] == T[i] )
k = k+ ; if ( k == len_s- )
{
printf("YES\n") ;
break ;
}
} if (i >= len_t )
printf("NO\n") ;
return ;
}

-------------------------ACM_KMP-----------------------------------

All in All
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 26253   Accepted: 10650

Description

You have devised a new encryption technique which encodes a message by inserting between its characters randomly generated strings in a clever way. Because of pending patent issues we will not discuss in detail how the strings are generated and inserted into the original message. To validate your method, however, it is necessary to write a program that checks if the message is really encoded in the final string.

Given two strings s and t, you have to decide whether s is a
subsequence of t, i.e. if you can remove characters from t such that the
concatenation of the remaining characters is s.

Input

The
input contains several testcases. Each is specified by two strings s, t
of alphanumeric ASCII characters separated by whitespace.The length of s
and t will no more than 100000.

Output

For each test case output "Yes", if s is a subsequence of t,otherwise output "No".

Sample Input

sequence subsequence
person compression
VERDI vivaVittorioEmanueleReDiItalia
caseDoesMatter CaseDoesMatter

Sample Output

Yes
No
Yes
No
----------------------------IDEA-------------------------------------
可以创建两层循环:
循环1{
    接收两个字符串 于 S , T 并调用 strlen 计算出 字符串的长度 s:len_s t:len_t
  
    counter = 0 ;
   for ( i = 0 ; i < len_t ; i++ )
   {
      if ( S[counter] == T[i] )
      {
        if ( counter == len_s -1 )//is S finished?
        { 
         break ;
        
        }
        counter++ ;

      }
  }
 
    if ( counter == len_s-1 ) {printf YES break}
    if ( counter != len_s-1 ) {printf NO break }
}
    
------------------------------SRC-------------------------------------
#include <stdio.h>
#include <string.h> int main()
{
char S[],T[] ;
int s, t;
int counter ,i; while ( ~scanf("%s %s", S,T) )
{
s = strlen ( S ) ;
t = strlen ( T ) ;
counter = ; for ( i = ; i < t ; i++ )
{
if ( T[i] == S[counter] )
{
if (counter == (s-))
{ break ;
} counter++ ;
}
} printf("%d t %d" , i, t) ; if ( counter == s - ) printf("Yes\n") ;
else printf("No\n") ; } return ;
}

//(╰_╯)#  为毛不过呢!!

KMP算法_读书笔记的更多相关文章

  1. 串的应用与kmp算法讲解--学习笔记

    串的应用与kmp算法讲解 1. 写作目的 平时学习总结的学习笔记,方便自己理解加深印象.同时希望可以帮到正在学习这方面知识的同学,可以相互学习.新手上路请多关照,如果问题还请不吝赐教. 2. 串的逻辑 ...

  2. 数据结构与算法JavaScript 读书笔记

    由于自己在对数组操作这块比较薄弱,然后经高人指点,需要好好的攻读一下这本书籍,原本想这个书名就比较高深,这下不好玩了.不过看着看着突然觉得讲的东西都比较基础.不过很多东西,平时还是没有注意到,故写出读 ...

  3. 程序语言的奥妙:算法解读 ——读书笔记

    算法(Algorithm) 是利用计算机解决问题的处理步骤. 算法是古老的智慧.如<孙子兵法>,是打胜仗的算法. 算法是古老智慧的结晶,是程序的范本. 学习算法才能编写出高质量的程序. 懂 ...

  4. <算法图解>读书笔记:第4章 快速排序

    第4章 快速排序 4.1 分而治之 "分而治之"( Divide and conquer)方法(又称"分治术") ,是有效算法设计中普遍采用的一种技术. 所谓& ...

  5. <算法图解>读书笔记:第2章 选择排序

    第2章 选择排序 2.1 内存的工作原理 需要将数据存储到内存时,请求计算机提供存储空间,计算机会给一个存储地址.需要存储多项数据时,有两种基本方式-数组和链表 2.2 数组和链表 2.2.1 链表 ...

  6. <算法图解>读书笔记:第1章 算法简介

    阅读书籍:[美]Aditya Bhargava◎著 袁国忠◎译.人民邮电出版社.<算法图解> 第1章 算法简介 1.2 二分查找 一般而言,对于包含n个元素的列表,用二分查找最多需要\(l ...

  7. 《java数据结构和算法》读书笔记

    大学时并不是读计算机专业的, 之前并没有看过数据结构和算法,这是我第一次看.         从数据结构方面来说:                数组:最简单,遍历.查找很快:但是大小固定,不利于扩展 ...

  8. <算法图解>读书笔记:第3章 递归

    第3章 递归 3.1 递归 程序调用自身的编程技巧称为递归( recursion).递归做为一种算法在程序设计语言中广泛应用. 一个过程或函数在其定义或说明中有直接或间接调用自身的一种方法,它通常把一 ...

  9. KMP算法_模板_C++

    这个博客讲得非常优秀,可惜它是Java版本的 http://blog.csdn.net/yutianzuijin/article/details/11954939/ a 为匹配串,b 为目标串 通俗讲 ...

随机推荐

  1. 【转】修改eclipse中的M2_REPO变量

    转自:http://superseven.iteye.com/blog/1625429 从eclipse中增加了maven2的插件之后,maven默认的本地库的路径是${user}/.m2/repos ...

  2. 深入学习JS: __doPostBack函数

    在.NET中,所有的服务器控件提交到服务器的时候,都会调用__doPostBack这个函数,所以灵活运用这个函数对于我们的帮助还是很大的. 比如,在我们写程序的时候经常会需要动态的生成一些控件,最简单 ...

  3. python导入上级目录中的模块

    python导入同级别模块很方便: import xxx 要导入下级目录页挺方便,需要在下级目录中写一个__init__.py文件 from dirname import xxx 要导入上级目录,可以 ...

  4. C# WinForm窗体界面设置问题

    设置方法: 一:Form对象 属性: 设计中的Name:窗体类的类名AcceptButton:窗口的确定按钮CancelButton:窗口按ESC的取消按钮 1.外观 Backcolor:背景颜色Fo ...

  5. arcGis引入Dll报无法嵌入互操作类型错误解决方法

    arcGis引入Dll报“无法嵌入互操作类型"ESRI.ARCGIS.Geometry.PointClass".请改用通用接口."   解决方法:设置引用DLL的“嵌入互 ...

  6. (转)Python:self

    原文:http://www.douban.com/group/topic/19376685/ 这是对前面一个php程序员问python方法为什么要手写一个self的回答,当时那个帖非常的热闹,但是下面 ...

  7. Python的模块,模块的使用、安装,别名,作用域等概念

    所谓的模块就是将不同功能的函数分别放到不同的文件中,这样不仅有利于函数的维护,也方便了函数的调用.在Python中,一个.py文件就是一个模块(Module). 在模块的上层有一个叫做包(Packag ...

  8. Javascript之<script>标签

    把javascript代码插入到HTML页面中需要使用<script>标签,使用这个元素可以使javascript和html标记混合在一个页面中,也可以引入外部的javascript文件. ...

  9. yii2

    yii2框架官方说明文档 http://www.yiiframework.com/doc/guide/2.0/zh_cn/caching.page yii2创建您的第一个application应用 h ...

  10. Linux 内核开发—内核简单介绍

    内核简单介绍 Linux 构成 Linux 为什么被划分为系统空间和内核空间 隔离核心程序和应用程序,实现对核心程序和数据的保护. 什么内核空间,用户空间 内核空间和用户空间是程序执行的两种不同的状态 ...