专栏首页机器学习入门4.7字符串匹配(3)

4.7字符串匹配(3)

挑战程序竞赛系列(68):4.7字符串匹配(3)


题意:

找茬:从大图中找出特定方块小图,旋转翻转皆可。

先编码,后hash计算,最后匹配输出结果,和书上一个思路。

代码如下:

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.StringTokenizer;

public class Main{

    String INPUT = "./data/judge/201709/A1312.txt";

    public static void main(String[] args) throws IOException {
        new Main().run();
    }

    static final long B1 = 9973;
    static final long B2 = 1000000007;

    int base64(char ch) {
        if (ch >= 'A' && ch <= 'Z') {
            return ch - 'A';
        }
        if (ch >= 'a' && ch <= 'z') {
            return ch - 'a' + 26;
        }
        if (ch >= '0' && ch <= '9') {
            return ch - '0' + 52;
        }
        return ch == '+' ? 62 : 63;
    }

    void base64_to_bit(char[][] raw, int[][] bit, int n) {
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < raw[i].length; ++j) {
                int base = base64(raw[i][j]);
                for (int k = 0; k < 6; ++k) {
                    bit[i][j * 6 + 5 - k]  = (base >> k) & 1;
                }
            }
        }
    }

    long computePatternHash(int[][] bit, int n, int m){
        long[][] hs = new long[n][m];
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < m; ++j) {
                hs[i][j] = (j > 0 ? hs[i][j - 1] : 0) * B1 + bit[i][j];
            }
        }

        long[][] hs2 = new long[n][m];
        for (int j = 0; j < m; ++j) {
            for (int i = 0; i < n; ++i) {
                hs2[i][j] = (i > 0 ? hs2[i - 1][j] : 0) * B2 + hs[i][j];
            }
        }


        return hs2[n - 1][m - 1];
    }

    long[][] computeHash(int[][] imageBit, int n, int m, int p){
        long[][] hs = new long[n][m];
        for (int i = 0; i < n; ++i) {
            long pw = 1;
            for (int j = 0; j < p; ++j) {
                hs[i][j] = (j > 0 ? hs[i][j - 1] : 0) * B1 + imageBit[i][j];
                pw *= B1;
            }

            for (int j = p; j < m; ++j) {
                hs[i][j] = hs[i][j - 1] * B1 + imageBit[i][j] - pw * imageBit[i][j - p];
            }
        }

        long[][] hs2 = new long[n][m];
        for (int j = 0; j < m; ++j) {
            long pw = 1;
            for (int i = 0; i < p; ++i) {
                hs2[i][j] = (i > 0 ? hs2[i - 1][j] : 0) * B2 + hs[i][j];
                pw *= B2;
            }

            for (int i = p; i < n; ++i) {
                hs2[i][j] = hs2[i - 1][j] * B2 + hs[i][j] - pw * hs[i - p][j];
            }
        }

        return hs2;
    }

    void rotate(int[][] bit, int n) {
        int[][] tmp = new int[n][n];
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < n; ++j) {
                tmp[i][j] = bit[i][j];
            }
        }

        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < n; ++j) {
                bit[j][n - i - 1] = tmp[i][j];
            }
        }
    }

    void overturn(int[][] bit, int n) {
        int[][] tmp = new int[n][n];
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < n; ++j) {
                tmp[i][j] = bit[i][j];
            }
        }

        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < n; ++j) {
                bit[i][n - j - 1] = tmp[i][j];
            }
        }
    }

    void read() {
        while (true) {
            int W = ni();
            int H = ni();
            int P = ni();
            if (W + P + H == 0) break;

            char[][] image = new char[H][];
            char[][] pattern = new char[P][];
            for (int i = 0; i < H; ++i) {
                image[i] = ns().toCharArray();
            }

            for (int i = 0; i < P; ++i) {
                pattern[i] = ns().toCharArray();
            }


            if (H < P || W < P) {
                out.println(0);
                continue;
            }

            int[][] imageBit   = new int[H][1024];
            int[][] patternBit = new int[P][128];

            base64_to_bit(image, imageBit, H);
            base64_to_bit(pattern, patternBit, P);

            Map<Long, Integer> mem = new HashMap<>();

            for (int d = 0; d < 8; ++d) {
                rotate(patternBit, P);
                if (d == 4) {
                    overturn(patternBit, P);
                }
                long pat = computePatternHash(patternBit, P, P);
                Num.inc(mem, pat);
            }

            long[][] hs = computeHash(imageBit, H, W, P);
            int ans = 0;
            for (int i = 0; i < H; ++i) {
                for (int j = 0; j < W; ++j) {
                    if (i >= P - 1 && j >= P - 1) {
                        if (mem.containsKey(hs[i][j])) {
                            ans ++;
                        }
                    }
                }
            }

            out.println(ans);
        }
    }


    FastScanner in;
    PrintWriter out;

    void run() throws IOException {
        boolean oj;
        try {
            oj = ! System.getProperty("user.dir").equals("F:\\java_workspace\\leetcode");
        } catch (Exception e) {
            oj = System.getProperty("ONLINE_JUDGE") != null;
        }

        InputStream is = oj ? System.in : new FileInputStream(new File(INPUT));
        in = new FastScanner(is);
        out = new PrintWriter(System.out);
        long s = System.currentTimeMillis();
        read();
        out.flush();
        if (!oj){
            System.out.println("[" + (System.currentTimeMillis() - s) + "ms]");
        }
    }

    public boolean more(){
        return in.hasNext();
    }

    public int ni(){
        return in.nextInt();
    }

    public long nl(){
        return in.nextLong();
    }

    public double nd(){
        return in.nextDouble();
    }

    public String ns(){
        return in.nextString();
    }

    public char nc(){
        return in.nextChar();
    }

    class FastScanner {
        BufferedReader br;
        StringTokenizer st;
        boolean hasNext;

        public FastScanner(InputStream is) throws IOException {
            br = new BufferedReader(new InputStreamReader(is));
            hasNext = true;
        }

        public String nextToken() {
            while (st == null || !st.hasMoreTokens()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (Exception e) {
                    hasNext = false;
                    return "##";
                }
            }
            return st.nextToken();
        }

        String next = null;
        public boolean hasNext(){
            next = nextToken();
            return hasNext;
        }

        public int nextInt() {
            if (next == null){
                hasNext();
            }
            String more = next;
            next = null;
            return Integer.parseInt(more);
        }

        public long nextLong() {
            if (next == null){
                hasNext();
            }
            String more = next;
            next = null;
            return Long.parseLong(more);
        }

        public double nextDouble() {
            if (next == null){
                hasNext();
            }
            String more = next;
            next = null;
            return Double.parseDouble(more);
        }

        public String nextString(){
            if (next == null){
                hasNext();
            }
            String more = next;
            next = null;
            return more;
        }

        public char nextChar(){
            if (next == null){
                hasNext();
            }
            String more = next;
            next = null;
            return more.charAt(0);
        }
    }

    static class ArrayUtils {

        public static void fill(int[][] f, int value) {
            for (int i = 0; i < f.length; ++i) {
                Arrays.fill(f[i], value);
            }
        }

        public static void fill(int[][][] f, int value) {
            for (int i = 0; i < f.length; ++i) {
                fill(f[i], value);
            }
        }

        public static void fill(int[][][][] f, int value) {
            for (int i = 0; i < f.length; ++i) {
                fill(f[i], value);
            }
        }
    }

    static class Num{
        public static <K> void inc(Map<K, Integer> mem, K k) {
            if (!mem.containsKey(k)) mem.put(k, 0);
            mem.put(k, mem.get(k) + 1);
        }
    }

    static class D{

        public static void pp(int[][] board, int row, int col) {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < row; ++i) {
                for (int j = 0; j < col; ++j) {
                    sb.append(board[i][j] + (j + 1 == col ? "\n" : " "));
                }
            }
            System.out.println(sb.toString());
        }

        public static void pp(char[][] board, int row, int col) {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < row; ++i) {
                for (int j = 0; j < col; ++j) {
                    sb.append(board[i][j] + (j + 1 == col ? "\n" : " "));
                }
            }
            System.out.println(sb.toString());
        }
    }
}

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • 挑战程序竞赛系列(80):4.3 2-SAT(4)

    挑战程序竞赛系列(80):4.3 2-SAT(4) 传送门:POJ 2749: Building roads 题意: 阳关路与独木桥:有N个农场,其中A对相...

    用户1147447
  • LeetCode Weekly Contest 31解题思路

    版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.n...

    用户1147447
  • 挑战程序竞赛系列(28):3.5最小费用流

    版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.n...

    用户1147447
  • SDOI 2018二轮题解(除Day2T1)

    然鹅学了不到一个月文化课再回来看OI的东西有一种恍如隔世的感觉,烤前感觉也没啥可复习的,就补一补去年二轮的题吧。

    attack
  • 洛谷P4783 【模板】矩阵求逆(高斯消元)

    attack
  • ZROJ#397. 【18提高7】模仿游戏(爆搜)

    读完题目后不难发现,每次约束的条件相当于是\(b[((x[i] + i) % N + (i / N) % N) % N] = y[i]\)

    attack
  • 挑战程序竞赛系列(80):4.3 2-SAT(4)

    挑战程序竞赛系列(80):4.3 2-SAT(4) 传送门:POJ 2749: Building roads 题意: 阳关路与独木桥:有N个农场,其中A对相...

    用户1147447
  • 挖掘机技术哪家强(c++实现)

    描述:为了用事实说明挖掘机技术到底哪家强,组织一场挖掘机技能大赛。现请你根据比赛结果统计出技术最强的那个学校。

    用户2038589
  • BZOJ 1083: [SCOI2005]繁忙的都市【Kruscal最小生成树裸题】

    1083: [SCOI2005]繁忙的都市 Time Limit: 10 Sec  Memory Limit: 162 MB Submit: 2925  Sol...

    Angel_Kitty
  • LeetCode Weekly Contest 31解题思路

    版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.n...

    用户1147447

扫码关注云+社区

领取腾讯云代金券