栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

【快速幂,字符串hash,前缀字典树】

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

【快速幂,字符串hash,前缀字典树】

package com.test.autimatic;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class PowAndHashString {

    public static void main(String[] args) {
        PowAndHashString string = new PowAndHashString();
        System.out.println(string.getPow(3,1));
        //--test
        System.out.println(1e5);
        //--hashString
        string.stringHash("asdfcfdgrasdfcvgfliuoiuliuoiuiwernhchzh",5);
    }

    
    public int getPow(int p,int time) {
        int temp = 1;
        while (time > 0){
            if((time & 1) == 1){
                temp *= p;
            }
            p *= p;
            time >>= 1;
        }
        return temp;
    }

    
    public void stringHash(String hash,int len){
        long[] hashS = new long[hash.length() + 1];
        long[] hashE = new long[hash.length() + 1];
        //解决冲突,散列分布值
        hashS[0] = 1;int mod = 131;
        for (int i = 0; i < hash.length(); i++) {
            hashS[i + 1] = hashS[i] * mod;
            hashE[i + 1] = hashE[i] * mod + hash.charAt(i);
        }
        //获取元素---
        List result = new ArrayList<>(16);
        Map map = new HashMap<>(16);
        for (int i = 1,j = i + len - 1; j < hash.length(); i++, j++) {
            long key = hashE[j] - hashE[i - 1] * hashS[len];
            Integer val = map.getOrDefault(key, 0);
            if(val == 1){ result.add(hash.substring(i-1,j)); }
            map.put(key,val + 1);
        }
        System.out.println(result);
    }

    
    public static class Trie{
        public Trie[] children;
        public boolean end;

        public Trie() {
            children = new Trie[26];
            end = false;
        }

        public Trie(Trie[] children, boolean end) {
            this.children = children;
            this.end = end;
        }

        
        public void insert(String word){
            Trie node = this;
            for (int i = 0; i < word.length(); i++) {
                if(node.children[word.charAt(i) - 'a'] == null){
                    node.children[word.charAt(i) - 'a'] = new Trie();
                }
                node = node.children[word.charAt(i) - 'a'];
            }
            node.end = true;
        }

        
        public Trie find(String temp){
            Trie node = this;
            for (int i = 0; i < temp.length(); i++) {
                if(node.children[temp.charAt(i) - 'a'] == null){
                    return null;
                }
                node = node.children[temp.charAt(i) - 'a'];
            }
            return node;
        }

        
        public boolean isEnds(String end){
            Trie trie = find(end);
            return trie != null && trie.end;
        }

        
        public boolean prefis(String prefix){
            return find(prefix) != null;
        }

    }


}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/682474.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号