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 ...
随机推荐
- centos密码策略
centos7密码策略 https://blog.csdn.net/qq_36896749/article/details/80264280 centos7设置密码规则 https://blog.cs ...
- eclipse的Git忽略某些不需要提交的文件
Eclipse切换到Navigator视图,找到.gitignore文件(如果是maven项目,一般找作为modules的项目的.gitignore文件),添加内容: .settings .proje ...
- git本地推送远程
第一次将本地映射到已经存在的仓库 https://techoverflow.net/2017/08/09/how-to-solve-git-fatal-no-configured-push-desti ...
- SSM商城项目(七)
1. 学习计划 1.Solr服务搭建 2.Solrj使用测试 3.把数据库中的数据导入索引库 4.搜索功能的实现 2. Solr服务搭建 2.1. Solr的环境 Solr是java开发. 需 ...
- Java学习笔记(十八):static关键字
- Python学习—基础篇之基本数据类型(一)
数据类型 在python中,能够直接处理的基本数据类型主要有数字类型.字符串类型.字节类型.布尔类型.列表.元组.字典.集合等. 一.数字类型 1.1 数字类型的创建 # 数字类型 a = 10 b ...
- 整数中1出现的次数(从1到n整数中1出现的次数)(python)
题目描述 求出1~13的整数中1出现的次数,并算出100~1300的整数中1出现的次数?为此他特别数了一下1~13中包含1的数字有1.10.11.12.13因此共出现6次,但是对于后面问题他就没辙了. ...
- 小强学渲染之OpenGL的GPU管线
GPU渲染流水线,是硬件真正体现渲染概念的操作过程,也是最终将图元画到2D屏幕上的阶段.GPU管线涵盖了渲染流程的 几何阶段 和 光栅化阶段,但对开发者而言,只有对顶点和片段着色器有可编程控制权,其他 ...
- lambda正则化参数的大小影响
当lambda的值很小时,其惩罚项值不大,还是会出现过拟合现象,当时lambda的值逐渐调大的时候,过拟合现象的程度越来越低,但是当labmda的值超过一个阈值时,就会出现欠拟合现象,因为其惩罚项太大 ...
- TZOJ 5101 A Game(区间DP)
描述 Consider the following two-player game played with a sequence of N positive integers (2 <= N & ...