算法进阶--模板及经典题型

算法模板持续更新中

1、KMP算法—next数组

import java.util.Scanner;
public class KMP_nextArray{
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
  	String s1 = in.next();
  	String s2 = in.next();
  	int indexof = getIndexOf(s1,s2);
  	System.out.println(indexof);
  	in.close();
    }
    private static int getIndexOf(String s1, String s2) {
        if(s1 == null || s2==null || s2.length()<1 || s1.length()<s2.length()) {
            return -1;
        }
  	char[] str1 = s1.toCharArray();
  	char[] str2 = s2.toCharArray();
  	int i1 = 0;
  	int i2 = 0;
  	int[]next = getNextArray(str2);
 	while(i1<str1.length && i2<str2.length) {
   	    if(str1[i1] == str2[i2]) {
    	        i1++;
    		i2++;
   	    }else if(next[i2]==-1) {
    		i1++;
   	    }else {
    		i2 = next[i2];
   	    }
        }
 	return i2 == str2.length ? i1-i2 : -1;
    }
    private static int[] getNextArray(char[] str2) {
       // TODO Auto-generated method stub
        if(str2.length == 1) {
  	    return new int[] {-1};
  	}
  	int [] next = new int[str2.length];
  	next[0] = -1;
  	next[1] = 0;
  	int i = 2;
  	int cn = 0;
  	while(i < next.length) {
   	    if(str2[i-1]==str2[cn]) {
    		next[i++] = ++cn;
   	    }else if(cn > 0) {
    		cn = next[cn];
   	    }else {
    		next[i++] = 0;
   	    }
         }
         return next;
    }
}

2、Manacher‘s Algorithm–马拉车算法求最大回文子串

在这里插入代码片

3.并查集

import java.util.List;
import java.util.HashMap;
public class Main448_unionFind {
	    public static class Node{
	      // whatever you like int String char......
	    }
	    public class UnionFindSet{
	  	public HashMap<Node,Node> fatherMap;// key: child  value: father
	  	public HashMap<Node, Integer> sizeMap;
	  	public UnionFindSet(List<Node> nodes) {
	   		fatherMap = new HashMap<Node,Node>();
	   		sizeMap = new HashMap<Node, Integer>();
	   		makeSets(nodes);
	        }
		public void makeSets(List<Node> nodes) {
	   	fatherMap.clear();
	   	sizeMap.clear();
	   	for(Node node : nodes) {
	    	    fatherMap.put(node, node);
	            sizeMap.put(node, 1);
	   	}
	    }
	    // 寻找该节点的集合 的代表结点
	    private Node findHead(Node node) {
	   	Node father = fatherMap.get(node);
	   	if(father != node) {
	    	    father = findHead(father);
	   	}
	   	fatherMap.put(father, node);
	   	return father;
	    }
	    // 判断两个元素是否在同一个集合中
	    public boolean isSameSet(Node a,Node b) {
	   	return findHead(a) == findHead(b);
	    }
	    //  合并两个集合
	    public void union(Node a,Node b) {
	   	if(a == null || b == null) {
	    	    return;
	    	}
	    	Node aHead = findHead(a);
	    	Node bHead = findHead(b);
	    	if(aHead != bHead) {
	    		int aSetSize = sizeMap.get(aHead);
	    		int bSetSize = sizeMap.get(bHead);
	    		if(aSetSize<=bSetSize) {
		    	    fatherMap.put(aHead, bHead);
		     	    sizeMap.put(bHead, aSetSize+bSetSize);
	    		}else {
		            fatherMap.put(bHead, aHead);
		            sizeMap.put(aHead, aSetSize+bSetSize);
	    	        }
	    	 }
	     }
	 }
	 public static void main(String[] args) {
	     // TODO Auto-generated method stub 
	 }
}
4、A星寻路法

A※寻路法-是一种寻找最短路径并避开障碍物的算法
适用于解决迷宫问题和有障碍地图问题
上代码

//迷宫地图
public static final int[][] MAZE = {
	{0,0,0,0,0,0,0},
	{0,0,0,1,0,0,0},
	{0,0,0,1,0,0,0},
	{0,0,0,1,0,0,0},
	{0,0,0,0,0,0,0},
};
/*
*  @param start 迷宫起点
*  @param end  迷宫终点
*/
public static Grid aStartSearch(Grid start , Grid end){
    Arraylist<Grid> openList = new ArrayList<Grid>();
    Arraylist<Grid> closeList = new ArrayList<Grid>();
    //把起点加入 openList
    openList.add(start);
    //主循环,每一轮检查一个当前方格结点
    while(openList.size()>0{
        // 在openList中查找 F值最小的结点,将其作为当前方格结点
        Grid currentGrid = findMinGrid(openList);
        //将当前方格从openList中移除
        openList.remove(currentGrid);
        //当前方格进入 closeList
        closeList.add(currentGrid);
        //找到所有邻近结点
        List<Grid>neighbors = findNeighbors(currentGrid,openList,closeList);
        for(Grid grid : neighbors){
            if(!openList.contains(grid)){
                //邻近结点不在openList中,标记“父节点”、G、H、F,并放入openList
                grid.inintGrid(currentGrid,end);
                openList.add(grid);
            }
        }
        //如果终点在openList中,直接返回终点格子
        for(Grid grid : openList){
            if((grid.x == end.x) && (grid.y == end.y)){
                return grid;
            }
        }
    }
    //openList用尽,仍然找不到终点,说明终点不可到达,返回空
    return null;
}

private static Grid finMinGrid(ArrayList<Grid> openList){
    Grid tempGrid = openList.get(0);
    for(Grid grid : openList){
        if(grid.f < tempGrid.f){
            tempGrid = grid;
        }
    }
    return tempGrid;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值