Zhu-Takaoka Two-dimensional Pattern Matching
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的更多相关文章
- Beginning Scala study note(5) Pattern Matching
The basic functional cornerstones of Scala: immutable data types, passing of functions as parameters ...
- Symbols of String Pattern Matching
Symbols of String Pattern Matching in Introduction to Algorithms. As it's important to be clear when ...
- scala pattern matching
scala语言的一大重要特性之一就是模式匹配.在我看来,这个怎么看都很像java语言中的switch语句,但是,这个仅仅只是像(因为有case关键字),他们毕竟是不同的东西,switch在java中, ...
- [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 ...
- [Scala] Pattern Matching(模式匹配)
Scala中的match, 比起以往使用的switch-case有著更強大的功能, 1. 傳統方法 def toYesOrNo(choice: Int): String = choice match ...
- pattern matching is C# 7.0
https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/is 原来的版本 private static s ...
- C#9.0 终于来了,带你一起解读Pattern matching 和 nint 两大新特性玩法
一:背景 1. 讲故事 上一篇跟大家聊到了Target-typed new 和 Lambda discard parameters,看博客园和公号里的阅读量都达到了新高,甚是欣慰,不管大家对新特性是多 ...
- 函数式编程之-模式匹配(Pattern matching)
模式匹配在F#是非常普遍的,用来对某个值进行分支匹配或流程控制. 模式匹配的基本用法 模式匹配通过match...with表达式来完成,一个完整的模式表达式长下面的样子: match [somethi ...
- KMP string pattern matching
The function used here is from the leetcode. Details can be found in leetcode problem: Implement str ...
随机推荐
- python大法好——Python2.x与3.x版本区别
python大法好——Python2.x与3.x版本区别 Python的3.0版本,常被称为Python 3000,或简称Py3k.相对于Python的早期版本,这是一个较大的升级. 为了不带 ...
- Http协议和Https协议的安全性问题
https://www.cnblogs.com/intsmaze/p/6009648.html https://blog.csdn.net/jeffleo/article/details/768630 ...
- English-商务英文邮件例句100句
最常用最专业的商务英文邮件例句100句——塞依SAP培训 字体大小:大 | 中 | 小2013-08-27 17:24 阅读(74) 评论(0) 分类:sap职场 1. I am writing t ...
- Linux命令:builtin
语法: builtin [shell-builtin [arg ...]] 说明: 明确告诉bash执行的是内建命令,而不是和内建命令同名的函数.这实际就是bash容许用户自定义和builtin命令同 ...
- 通过yum安装php7
Linux下全局安装composer方法: //下载composercurl -sS https://getcomposer.org/installer | php //将composer.phar文 ...
- jQuery之必会增删改查Dom操作
.next .prev <button>change</button> <span class = '.demo'>aaa</span> <p ...
- Windows server 2012 R2 解决“无法完成域加入,原因是试图加入的域的SID与本计算机的SID相同
Windows server 2012 R2 解决“无法完成域加入,原因是试图加入的域的SID与本计算机的SID相同.”使用克隆的系统时,加域是出现如下问题.“无法完成域加入,原因是试图加入的域的SI ...
- android 个人使用总结
android 中button控件去除阴影背景 style="?android:attr/borderlessButtonStyle" android 中输入账号和密码是做判断处 ...
- 156. Binary Tree Upside Down反转二叉树
[抄题]: Given a binary tree where all the right nodes are either leaf nodes with a sibling (a left nod ...
- 命令实现linux和客户端文件上传下载
1.rz/sz命令 linux端使用rz/sz实现和windows客户端 linux服务器需要装了rz,sz yum install lrzsz 当然你的本地windows主机也通过ssh连接了lin ...