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 ...
随机推荐
- 游戏搭服自动化脚本shell
#!/bin/bash #;g=6006c= 178pop_s10 rm-bp1gy2r82o607w4v8.mysql.rds.aliyuncs.com # basedir=/data/jzadmi ...
- [C语言]进阶|指针与字符串
------------------------------------------------------------------------------------ 回顾:[C语言]指针与字符串 ...
- ie7ajax 跨域 no transport 解决办法
客户端js <script src="jquery-1.8.0.min.js"></script> <script src="JavaScr ...
- jQuery——检测滚动条是否到达底部
一.jQuery检测浏览器window滚动条到达底部 jQuery获取位置和尺寸相关函数:$(document).height() 获取整个页面的高度:$(window).height() ...
- 36 【kubernetes】coredns
CoreDNS是k8s中,master节点和从节点及其pods之间通信的接口. 但是上个博客中遇到一个问题dns一直处于crush的状态. https://www.cnblogs.com/helww/ ...
- 35 【kubernetes】configMap
kubernetes可以驱动容器的运行,并且把容器的运行放置在kubernetes定义的体系结构中pods这一级. 但是容器运行通常会需要某些参数,比如环境变量或者硬件使用情况. 为了解决对每个con ...
- 93. Restore IP Addresses产生所有可能的ip地址
[抄题]: Given a string containing only digits, restore it by returning all possible valid IP address c ...
- 73. Set Matrix Zeroes 把矩阵同一行列的元素都改成0
[抄题]: Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in-place. ...
- [leetcode]78. Subsets数组子集
Given a set of distinct integers, nums, return all possible subsets (the power set). Note: The solut ...
- python的基本用法(四)文件操作使用
#读文件,文件必须存在才能读f=open('操作文件',encoding='utf-8')res =f.read()print(res)f.close()#写文件fw=open('操作文件',mode ...