• MD5交并集


    package example.data;
    
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.Map;
    import java.util.UUID;
    
    public class TFKTest {
    
        public static void main(String[] args) {
            TFKTest test = new TFKTest();
            test.test();
        }
    
        /**
         * 计算两个集合交集
         */
        public void test() {
    
            Node root = new Node();
    
            // 初始化两条目标数据
            Path data = Path.sentenceToPath("b71b060decc045e09769044784588755");
            NodeOperate.addNode(root, data);
    
            data = Path.sentenceToPath("bacbe8961a0145bb87c9c8ab5f494123");
            NodeOperate.addNode(root, data);
    
            // 初始化第一颗树
            for (int i = 0; i < 5; i++) {
                String sentence = makeCharacter();
                Path path = Path.sentenceToPath(sentence);
                NodeOperate.addNode(root, path);
            }
    
            // 第二树中的一个节点
            String sentence = "bacbe8961a0145bb87c9c8ab5f494123";
            Path target = Path.sentenceToPath(sentence);
    
            // 交集结果存储在temp中
            Node temp = new Node();
    
            // 进行或者运行
            andComputer(root, temp, target);
    
            System.err.println("test");
        }
    
        public String makeCharacter() {
            String uuid = UUID.randomUUID().toString().replace("-", "");
            System.err.println(uuid);
            return uuid;
        }
    
        public void andComputer(Node root, Node temp, Path path) {
            if (NodeOperate.existsNode(root, path)) {
                System.err.println(Path.pathToSentence(path) + ":exists");
                NodeOperate.addNode(temp, path);
            }
        }
    
        public void orCOmputer(Node root, Path path) {
    
        }
    }
    
    class Node {
        char key;
        Node value;
        Map<Character, Node> pair;
    }
    
    class NodeOperate {
    
        public static void addNode(Node node, Path path) {
    
            if (node == null || path == null) {
                return;
            }
    
            if (contains(node, path)) {
    
                Node next = findNode(node, path);
                if (next != null) {
                    updateNode(node);
                    addNode(next, path.next);
                }
            } else {
                Node next = new Node();
    
                saveNode(node, path, next);
    
                addNode(next, path.next);
            }
        }
    
        public static void addNode0(Node node, Path path) {
    
            if (node == null || path == null) {
                return;
            }
    
            if (contains(node, path)) {
                Node next = findNode(node, path);
                addNode(next, path.next);
            } else {
                Node next = new Node();
    
                if (node.key != 'u0000') {
                    updateNode(node);
                }
                saveNode(node, path, next);
    
                addNode(next, path.next);
            }
        }
    
        public static Node findNode(Node node, Path path) {
            if (node.key != 'u0000') {
                if (node.key == (path.word)) {
                    return node.value;
                }
            }
    
            return node.pair.get(path.word);
        }
    
        public static boolean existsNode(Node node, Path path) {
    
            Path it = path;
            Node next = node;
            int i = 0;
            do {
                next = findNode(next, it);
                if (next != null) {
                    it = it.next;
                    i++;
                } else {
                    return false;
                }
            } while (it != null);
    
            return i == 32;
        }
    
        public static void saveNode(Node node, Path path, Node next) {
            if (node.pair != null) {
                node.pair.put(path.word, next);
            } else {
                node.key = path.word;
                node.value = next;
            }
        }
    
        public static void updateNode(Node node) {
            if (node.pair == null) {
                node.pair = new HashMap<>();
            }
    
            node.pair.put(node.key, node.value);
    
            node.key = 'u0000';
            node.value = null;
        }
    
        public static boolean contains(Node node, Path path) {
    
            if (node.pair != null) {
                return node.pair.containsKey(path.word);
            }
    
            if (node.key != 'u0000') {
                return node.key == (path.word);
            }
            return false;
        }
    }
    
    /**
     * Linked list representation of Pattern Path and Input Path
     */
    class Path extends ArrayList<String> {
    
        private static final long serialVersionUID = 1L;
    
        public char word;
        public Path next;
        public int length;
    
        /**
         * Constructor - class has public members
         */
        private Path() {
            next = null;
            word = 'u0000';
            length = 0;
        }
    
        /**
         * convert a sentence (a string consisting of words separated by single spaces)
         * into a Path
         *
         * @param sentence
         *            sentence to convert
         * @return sentence in Path form
         */
        public static Path sentenceToPath(String sentence) {
            sentence = sentence.trim();
            return arrayToPath(sentence.toCharArray());
        }
    
        /**
         * The inverse of sentenceToPath
         *
         * @param path
         *            input path
         * @return sentence
         */
        public static String pathToSentence(Path path) {
            StringBuilder result = new StringBuilder(32);
            for (Path p = path; p != null; p = p.next) {
                result.append(p.word);
            }
            return result.toString();
            /*
             * if (path == null) return ""; else return
             * path.word+" "+pathToSentence(path.next);
             */
        }
    
        /**
         * convert an array of strings to a Path
         *
         * @param array
         *            array of strings
         * @return sequence of strings as Path
         */
        private static Path arrayToPath(char[] array) {
            Path tail = null;
            Path head = null;
            for (int i = array.length - 1; i >= 0; i--) {
                head = new Path();
                head.word = array[i];
                head.next = tail;
                if (tail == null)
                    head.length = 1;
                else
                    head.length = tail.length + 1;
                tail = head;
            }
            return head;
            // return arrayToPath(array, 0);
        }
    
        /**
         * recursively convert an array to a Path
         *
         * @param array
         *            array of strings
         * @param index
         *            array index
         * @return Path form
         */
        static Path arrayToPath(char[] array, int index) {
            if (index >= array.length)
                return null;
            else {
                Path newPath = new Path();
                newPath.word = array[index];
                newPath.next = arrayToPath(array, index + 1);
                if (newPath.next == null)
                    newPath.length = 1;
                else
                    newPath.length = newPath.next.length + 1;
                return newPath;
            }
        }
    
        /**
         * print a Path
         */
        public String print() {
            String result = "";
            for (Path p = this; p != null; p = p.next) {
                result += p.word + ",";
            }
            if (result.endsWith(","))
                result = result.substring(0, result.length() - 1);
    
            return result;
        }
    
        @Override
        public String toString() {
            return this.print();
        }
    
    }
  • 相关阅读:
    12.python笔记之mysqldb模块
    13.python笔记之pyyaml模块
    11.python之线程,协程,进程,
    2.saltstack笔记之目标,模块,返回写入数据库
    6.django笔记之orm
    5.django笔记之form保存表单信息,动态select
    4.django笔记之admin
    docker批量删除none镜像
    docker 给none镜像打镜像
    jenkins卡在等待界面解决方法
  • 原文地址:https://www.cnblogs.com/jpit/p/9003989.html
Copyright © 2020-2023  润新知