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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#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;
    }
     
}

 

posted @   kid551  阅读(199)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· winform 绘制太阳,地球,月球 运作规律
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 上周热点回顾(3.3-3.9)
点击右上角即可分享
微信分享提示