Two dimensional pattern matching.

Details may be added later....

Corresponding more work can be found in Pattern Matching and Text Compression Algorithm, Maxime Crochemore, Thierry Lecroq.

Let's enjoy the code first:

#define REHASH(a, b, h)    (((h - a * d) << 1) + b)

void getNext(char *pattern, int n, int next[]){
int i = 0, j = -1;
next[i] = j; while(i < n){
while(j >= 0 && pattern[i] != pattern[j])
j = next[j]; ++i; ++j;
next[i] = j;
}
} void bitsCompare(BIG_IMAGE bigImg, SMALL_IMAGE smallImg, int bigRow, int bigCol, int smallRow, int smallCol, int lastRow, int lastCol){
// The beginning coordinate in big image.
int i0 = lastRow - smallRow + 1;
int j0 = lastCol - smallCol + 1; for(int i = 0; i < smallRow; ++i)
for(int j = 0; j < smallCol; ++j)
if(bigImg[i0 + i][j0 + j] != smallImg[i][j])
return; // Record the position of successful match.
OUTPUT(i0, j0);
} void KMP_Inline(BIG_IMAGE bigImg, SMALL_IMAGE smallImg, int bigRow, int bigCol, int smallRow, int smallCol, int bigImgHashArr[], int smallImgHashArr[], int next[], int lastRow){
int i = 0, j = 0; while(i < bigCol){
while(j >= 0 && bigImgHashArr[i] != smallImgHashArr[j])
j = next[j]; ++i; ++j // If matched with pattern, then j should be not less than smallCol
if(j >= smallCol){
bitsCompare(bigImg, smallImg, bigRow, bigCol, smallRow, smallCol, lastRow, i-1);
j = next[smallCol];
} }
} void ZT_TwoDimMatch(BIG_IMAGE bigImg, SMALL_IMAGE smallImg, int bigRow, int bigCol, int smallRow, int smallCol){
int bigImgHashArr[BIG_COL], smallImgHashArr[SMALL_COL], next[SMALL_COL]; // Preprocessing
// Compute first bigImg hash array
for(int j = 0; j < bigCol; ++j){
bigImgHashArr[j] = 0;
for(int i = 0; i < smallRow; ++ i)
bigImgHashArr[j] = (bigImgHashArr[j] << 1) + bigImg[i][j]; // The mod we use implicitly here is MAX_INT
} // Compute the smallImg hash array
for(int j = 0; j < smallCol; ++j){
smallImgHashArr[j] = 0;
for(int i = 0; i < smallRow; ++i)
smallImgHashArr[j] = (smallImgHashArr[j] << 1) + smallImg[i][j]; // The mod we use implicitly here is MAX_INT
} // Last row of one checking window
lastRow = smallRow - 1;
// digit of re-hash
d = 1;
for(int j = 1; j < smallRow; ++j)
d <<= 1; getNext(smallImgHashArr, smallCol, next); // Searching
while(lastRow < bigRow){
KMP_Inline(bigImg, smallImg, bigRow, bigCol, smallRow, smallCol, bigImgHashArr, smallImgHashArr, next, lastRow); // Rehash the big hash array
if(lastRow < bigRow - 1)
for(int j = 0; j < bigCol; ++j)
bigImgHashArr[j] = REHASH(bigImg[lastRow - smallRow + 1][j], bigImg[lastRow + 1][j], bigImgHashArr[j]); // The mod we use implicitly here is MAX_INT ++lastRow;
} }

Zhu-Takaoka Two-dimensional Pattern Matching的更多相关文章

  1. Beginning Scala study note(5) Pattern Matching

    The basic functional cornerstones of Scala: immutable data types, passing of functions as parameters ...

  2. Symbols of String Pattern Matching

    Symbols of String Pattern Matching in Introduction to Algorithms. As it's important to be clear when ...

  3. scala pattern matching

    scala语言的一大重要特性之一就是模式匹配.在我看来,这个怎么看都很像java语言中的switch语句,但是,这个仅仅只是像(因为有case关键字),他们毕竟是不同的东西,switch在java中, ...

  4. [PureScript] Break up Expressions into Cases in PureScript using Simple Pattern Matching

    Pattern matching in functional programming languages is a way to break up expressions into individua ...

  5. [Scala] Pattern Matching(模式匹配)

    Scala中的match, 比起以往使用的switch-case有著更強大的功能, 1. 傳統方法 def toYesOrNo(choice: Int): String = choice match ...

  6. pattern matching is C# 7.0

    https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/is 原来的版本 private static s ...

  7. C#9.0 终于来了,带你一起解读Pattern matching 和 nint 两大新特性玩法

    一:背景 1. 讲故事 上一篇跟大家聊到了Target-typed new 和 Lambda discard parameters,看博客园和公号里的阅读量都达到了新高,甚是欣慰,不管大家对新特性是多 ...

  8. 函数式编程之-模式匹配(Pattern matching)

    模式匹配在F#是非常普遍的,用来对某个值进行分支匹配或流程控制. 模式匹配的基本用法 模式匹配通过match...with表达式来完成,一个完整的模式表达式长下面的样子: match [somethi ...

  9. KMP string pattern matching

    The function used here is from the leetcode. Details can be found in leetcode problem: Implement str ...

随机推荐

  1. 【转】完整精确导入Kernel与Uboot参与编译了的代码到Source Insight,Understand, SlickEdit

    The linux kernel and u-boot contains lots of files, when we want to broswe the source code,we just w ...

  2. el-tabs添加滚动条

    element-ui的el-tabs默认是没有滚动条的,可在 el-tab-pane上添加: <el-tab-pane style="height:90%;overflow-y:aut ...

  3. linux 强制删除yum安装的php7.2

    由于支付宝SDK只支持php7.1,因为需要删除之前安装的7.2版,进行降级.通过yum remove不能完全删除php,必须通过rpm方式卸载.由于php安装模块间有依赖,因此需要按顺序进行卸载.如 ...

  4. MySQL中的重做日志(redo log),回滚日志(undo log),以及二进制日志(binlog)的简单总结

    MySQL中有六种日志文件,分别是:重做日志(redo log).回滚日志(undo log).二进制日志(binlog).错误日志(errorlog).慢查询日志(slow query log).一 ...

  5. linux上安装字体

    安装字体命令: yum install wqy-microhei-fonts wqy-zenhei-fonts   安装完字体的存放目录:/usr/share/fonts   默认会在fonts目录下 ...

  6. (转)C# Windows服务 弹出消息提醒框

    出处:http://blog.csdn.net/donghui6116773/article/details/53467069 服务(Service)对于大家来说一定不会陌生,它是Windows 操作 ...

  7. Springboot学习04-默认错误页面加载机制源码分析

    Springboot学习04-默认错误页面加载机制源码分析 前沿 希望通过本文的学习,对错误页面的加载机制有这更神的理解 正文 1-Springboot错误页面展示 2-Springboot默认错误处 ...

  8. redis集群密码设置

    1.密码设置(推荐)方式一:修改所有Redis集群中的redis.conf文件加入: masterauth passwd123 requirepass passwd123 说明:这种方式需要重新启动各 ...

  9. AutoCAD开发1---获取块属性

    Private Sub CommandButton1_Click() Dim pEntity As AcadObject Dim pBlock As AcadBlockReference Dim pP ...

  10. Android SDK Manager 无法打开

    环境变量已经设置(安装JDK8后 其实无需设置,之前记得Win7有个巧妙的地方是创建了3个快捷方式到某文件夹,现在Win10上直接将java.exe等放到System32目录下). 但是依然不行,网上 ...