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 ...
随机推荐
- Android下的几种时间格式转换
更多更全的工具类,请参考github上的Blankj/AndroidUtilCode 将毫秒转换为小时:分钟:秒格式 public static String ms2HMS(int _ms){ Str ...
- leetcode437
/** * Definition for a binary tree node. * public class TreeNode { * public int val; * public TreeNo ...
- win7获取system32所有权
(1)cmd -> takeown /f C:\Windows\System32* /r (2)右击system32文件夹属性 -> 安全 ->高级 ->所有者 —改为当前 ...
- STS(eclipse)3.7.3新建项目报错:org.apache.maven.archiver.MavenArchiver.getManifest(org.apache.maven.project.MavenProject, org.apache.maven.archiver.MavenArchiveConfiguration)
烦人的版本兼容问题 没有使用sts3.7.3系统内嵌的maven3.3.3,调整为稍高版本的maven3.5.2,懒得修改配置了. 升级eclipse插件吧. Eclipse,Help -> I ...
- Delphi中Chrome Chromium、Cef3学习笔记(一)
原文 http://blog.csdn.net/xtfnpgy/article/details/46635225 官方下载地址:https://cefbuilds.com/ CEF简介: 嵌入 ...
- echarts Cannot read property 'getAttribute' of null 问题的解决方法
今天在使用echarts练习官方给的实例的时候,代码没有问题 却总是报错“Cannot read property 'getAttribute' of null” 找了好久的文档没有看明白 ... 最 ...
- MFC笔记3
1. C6有默认的提示代码功能,但是其默认的快捷键是Ctrl + Space,这一般情况下是切换输入法快捷键,所以,只需重新设置一下快捷键就可以实现提示代码功能,具体设置位置如下: 工具(T) -&g ...
- 项目总结18-使用textarea无法判断空值之坑
项目总结18-使用textarea无法判断空值之坑 今天使用js判断textarea为空,发现怎么都无法成功仔细做了对比测试,发现结果如下: 1-JS代码 if($("#content&qu ...
- sqlserver 更新通过 select 查询出的结果集
update Babies set BirthOrder =tb.sn from Babies b1, (select ROW_NUMBER() over (partition by familyid ...
- Hadoop格式化namenode 出错 Error: A JNI error has occurred, please check your installation and try again Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/hadoop/security/authorize/Refr
一般是修改配置文件:etc/hadoop/hadoop-env.sh的时候出现的错误 export JAVA_HOME=/usr/jdk export HADOOP_COMMON_HOME=~/had ...