大一蓝桥杯JAVAB组国一算法板子分享

98 阅读15分钟

大一蓝桥杯JAVAB组国一算法板子分享

我先叠个甲,我很菜 ,但感觉更多是运气和这个🏀太水了。

我先简述一下我的算法的经历:我是大一期间过了一遍B组会考的数据结构与和算法,然后在洛谷,牛客上刷题,打打牛客周赛,小白月赛啥的。我自己知道我有多菜反而说明javab组有多水(很菜!)。现在才写这个,主要是想写博客了(bushi),也快到12月蓝桥杯报名的时候了(我是不会再打了,学校也不报销,蓝桥杯因为pdf事件看着也要凉的感觉),对了第一次写博客可能格式什么的都不太好,有建议可以多提一下谢谢啦。

切入正题,蓝桥杯是OI比赛,所以也有很多OI需要注意的点(打表骗分,对拍,java快读快写什么的,也别re了)

先看一下第一步,进考场要修改eclipse的一些设置

1代码补齐->window->最下面的preference->java->editor->content->启用,添加所有字母的大小写
2自定义模板在2的基础上找到Templates->new,这里我一般自定义for
3代码自动保存window-preference-general-editor-autosave
4自动格式化window-preference-java-editor-saveactions-前两个打勾

然后下面是以前记笔记写的板子(有的算法没有固定的板子就只写了一种的思路,下面的板子有的省略了快读快写直接调用了别忘了加上或者都写在初始板子里面)

0:初始:

import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.StringTokenizer;

public class kd {
	static int f1, f2, f3, f4, f5, f6, f[] = new int[155], lj[] = new int[150], n, m, sl, h, l, cd = 100005;
	static int ydh[] = { -1, 0, 1, 0 }, ydl[] = { 0, 1, 0, -1 };
	static long jg, ans, sum, pre, a[] = new long[cd], b[], c[], d[];

	static void pd() {
		for (int i = 0; i < sl; i++) {
			print(lj[i + 1] + " ");
		}
		println();
	}

	static void dfs(int x) {
		if (x == sl + 1) {
			pd();
			return;
		}
		for (int i = 1; i <= sl; i++) {
			if (f[i] == 0) {
				f[i] = 1;
				lj[x] = i;
				dfs(x + 1);
				f[i] = 0;
			}
		}
	}

	static void solve() throws NumberFormatException, IOException {
		n = nextint();
		sl = n;
		for (int i = 0; i < n; i++) {
			a[i] = nextlong();
		}
		print(jg);
	}

	public static void main(String[] args) throws NumberFormatException, IOException {
		// TODO Auto-generated method stub
		int t = 1;
		while (t-- > 0) {
			solve();
		}
		flush();
	}

	static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
	static StringTokenizer sk;
	static PrintWriter pw = new PrintWriter(new BufferedOutputStream(System.out));

	static boolean hasnext() throws IOException {
		if (sk != null && sk.hasMoreElements())
			return true;
		br.mark(1024);
		String s = br.readLine();
		br.reset();
		return sk != null && !s.isEmpty();
	}

	static String next() throws IOException {
		while (sk == null || !sk.hasMoreElements()) {
			String s = br.readLine();
			sk = new StringTokenizer(s);
		}
		return sk.nextToken();
	}

	static int nextint() throws NumberFormatException, IOException {
		return Integer.parseInt(next());
	}

	static long nextlong() throws NumberFormatException, IOException {
		return Long.parseLong(next());
	}

	static double nextdouble() throws NumberFormatException, IOException {
		return Double.parseDouble(next());
	}

	static BigInteger nextbig() throws NumberFormatException, IOException {
		return new BigInteger(next());
	}

	static void print(Object o) {
		pw.print(o);
	}

	static void println(Object o) {
		pw.println(o);
	}

	static void println() {
		pw.println();
	}

	static void printf(String s, Object... o) {
		pw.printf(s, o);
	}

	static void flush() {
		pw.flush();
	}
}

这是进考场的一件事搓板子,然后复制粘贴每个代码都写在这里面(有的需要改一些变量就去修改)

1:工具

1:快读快写

	static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
	static StringTokenizer st;
	static PrintWriter pw = new PrintWriter(new BufferedOutputStream(System.out));

	static String next() throws IOException {
		while (st == null || !st.hasMoreElements()) {
			String s = br.readLine();
			st = new StringTokenizer(s);
		}
		return st.nextToken();

	}

	static boolean hasnext() throws IOException {// 还是添加一个新方法比改变原方法好一点
		if (st != null && st.hasMoreElements()) {
			return true;
		}
		br.mark(1000);
		String s = br.readLine();
		br.reset();
		return s != null && !s.isEmpty();

	}

	static int nextint() throws NumberFormatException, IOException {
		return Integer.parseInt(next());
	}

	static long nextlong() throws NumberFormatException, IOException {
		return Long.parseLong(next());
	}

	static double nextdouble() throws NumberFormatException, IOException {
		return Double.parseDouble(next());
	}

	static BigInteger nextBigInteger() throws IOException {
		return new BigInteger(next());
	}

	static BigDecimal nextBigDecimal() throws IOException {
		return new BigDecimal(next());
	}

	static void println() {
		pw.println();

	}

	static void println(Object o) {

		pw.println(o);
	}

	static void printf(String s, Object... o) {

		pw.printf(s, o);
	}

	static void flsh() {
		pw.flush();
	}

2:对拍

	public static void main(String[] args) throws InterruptedException, IOException {
		int i = 1;
		while (i < 12) {
			// TODO Auto-generated method stub
			new ProcessBuilder("java", "-cp", "bin", "in").redirectOutput(new File("in.txt")).start().waitFor();
			new ProcessBuilder("java", "-cp", "bin", "t1").redirectInput(new File("in.txt"))
					.redirectOutput(new File("Main.out")).start().waitFor();
			new ProcessBuilder("java", "-cp", "bin", "t2").redirectInput(new File("in.txt"))
					.redirectOutput(new File("Main2.out")).start().waitFor();
			int pd = new ProcessBuilder("fc", "/w", "Main.out", "Main2.out").start().waitFor();
			if (pd == 0) {
				System.out.printf("AC%d\n", i);
				new File("in.txt").delete();
				new File("Main.out").delete();
				new File("Main2.out").delete();
			} else {
				System.out.printf("WA%d\n", i);
				printfile("in.txt");
				printfile("Main.out");
				printfile("Main2.out");
			}
			i++;
		}
	}
	static void printfile(String filename) throws IOException {
		System.out.println(new String(Files.readAllBytes(new File(filename).toPath())).trim());
	}

还要写in,t1,t2三个类在同一文件夹来对拍

3:打表

	public static void main(String[] args) throws InterruptedException, IOException {
		// TODO Auto-generated method stub
		int i = 1;
		while (i <= 20) {
			new ProcessBuilder("java", "in").redirectOutput(new File("in.txt")).start().waitFor();
			new ProcessBuilder("java", "test").redirectInput(new File("in.txt")).redirectOutput(new File("test.out"))
					.start().waitFor();
			System.out.printf("#%d:", i);
			printfile("in.txt");
			printfile("test.out");
			new File("in.txt").delete();
			new File("test.out").delete();
			i++;
		}
	}

	static void printfile(String filename) throws IOException {
		System.out.println(new String(Files.readAllBytes(new File(filename).toPath())).trim());
	}

2:数论基础

1:质数筛

1:埃氏筛
 public class zhishuaishishai {
	static int a[] = new int[1000005], f;

	public static void main(String[] args) throws NumberFormatException, IOException {
		// TODO Auto-generated method stub
		int jg[] = new int[1000005];
		int n = nextint();
		a[0] = a[1] = 1;
		for (int i = 2; i < n; i++) {
			if (a[i] == 0) {
				jg[f++] = i;
				for (int p = i * i; p < n; p += i) {
					a[p] = 1;
				}
			}
		}
		for (int i = 0; i < f; i++) {
			println(jg[i]);
		}
		flsh();
	}
 }
2:欧拉筛
 public class zhishuoula {
	static int a[] = new int[1000005], f;

	public static void main(String[] args) throws NumberFormatException, IOException {
		// TODO Auto-generated method stub
		int jg[] = new int[1000005];
		int n = nextint();
		a[0] = a[1] = 1;
		for (int i = 2; i < n; i++) {
			if (a[i] == 0) {
				jg[f++] = i;
			}
			for (int p = 0; p < f; p++) {
				int sum = jg[p] * i;
				if (sum > n)
					break;
				a[sum] = 1;
				if (i % jg[p] == 0)
					break;
			}
		}
		for (int i = 0; i < f; i++) {
			println(jg[i]);
		}
		flsh();
	}
}

2:快速幂

 public class ksm {

	public static void main(String[] args) throws NumberFormatException, IOException {
		// TODO Auto-generated method stub
		long a = nextlong();
		long b = nextlong();
		long c = nextlong();
		long jg = 1, sum = a;
		String s = Long.toBinaryString(b);// 从左到右是0到l-1
		for (int i = 0; i < s.length(); i++) {
			if (s.charAt(s.length() - i - 1) == '1') {
				jg = (jg % c * sum % c) % c;
			}
			sum = (sum % c * sum % c) % c;
		}
		printf("%d^%d mod %d=%d", a, b, c, jg);
		flsh();
	}
 
}

java自带BigInterger,了解一下自带的api即可,赛时也有文档看)

        long a = nextlong();
		long b = nextlong();
		long c = nextlong();

		BigInteger jg = BigInteger.valueOf(a).modPow(BigInteger.valueOf(b), BigInteger.valueOf(c));

3:图相关的

1:bfs

import java.io.*;
import java.util.*;

class Point {
    int x, y;
    Point(int x, int y) {
        this.x = x;
        this.y = y;
    }
}

public class Main {
    static int[][] a = new int[1005][1005];
    static char[][] b = new char[1005][1005];
    static Queue<Point> q = new LinkedList<>();
    static int[] dx = {1, -1, 0, 0};  
    static int[] dy = {0, 0, 1, -1}; 
    static int n;

    static void bfs() {
        q.add(new Point(0, 0));
        a[0][0] = 1;
        while (!q.isEmpty()) {
            Point cur = q.poll();
            if (cur.x == n-1 && cur.y == n-1) {
                println(a[n-1][n-1]);
                return;
            }
            for (int i = 0; i < 4; i++) {
                int nx = cur.x + dx[i];
                int ny = cur.y + dy[i];
                if (nx >= 0 && nx < n && ny >= 0 && ny < n 
                    && a[nx][ny] == 0 && b[nx][ny] != '*') {
                    a[nx][ny] = a[cur.x][cur.y] + 1;
                    q.add(new Point(nx, ny));
                }
            }
        }
        println(-1);
    }

    public static void main(String[] args) throws Exception {
        n = nextint();
        for (int i = 0; i < n; i++) {
            b[i] = next().toCharArray();
        }
        bfs();
        flsh();
    }
     
}

//dfs放搜索了

2:dij

单源最短路,邻接表,优先队列优化

class lj {
	long rbw;
	int zd;

	lj(int zd, long rbw) {
		this.zd = zd;
		this.rbw = rbw;
	}
}

public class dij {
	static int n, m;
	static List<List<lj>> a = new ArrayList<>();
	static long jg[] = new long[100005];
	static char bj[] = new char[100005];

	static void dijs() {
		jg[0] = 0;
		PriorityQueue<lj> p = new PriorityQueue<>((o, p2) -> Long.compare(o.rbw, p2.rbw));
		p.add(new lj(0, 0));
		while (!p.isEmpty()) {
			lj xLj = p.poll();
			int wz1 = xLj.zd;
			long wzd = xLj.rbw;
			if (bj[wz1] == 0) {
				bj[wz1] = '1';
				for (int i = 0; i < a.get(wz1).size(); i++) {
					lj yLj = a.get(wz1).get(i);
					int wz2 = yLj.zd;
					long w2 = yLj.rbw;
					if (jg[wz2] > jg[wz1] + w2) {
						jg[wz2] = jg[wz1] + w2;

						p.add(yLj);
					}
				}
			}
		}
	}

	public static void main(String[] args) throws NumberFormatException, IOException {
		// TODO Auto-generated method stub
		n = nextint();
		m = nextint();
		Arrays.fill(jg, (long) 2e8);
		for (int i = 0; i < n; i++) {
			a.add(new ArrayList<>());
		}
		for (int i = 0; i < m; i++) {
			int z = nextint();
			int r = nextint();
			long w = nextlong();
			a.get(z).add(new lj(r, w));
			a.get(r).add(new lj(z, w));
		}
		dijs();
		for (int i = 0; i < n; i++) {
			println(jg[i]);
		}
		flsh();
	}

3:fld

多源最短路

	public static void main(String[] args) throws NumberFormatException, IOException {
		// TODO Auto-generated method stub
		long dp[][] = new long[2005][2005];
		int n = nextint();
		int m = nextint();
		for (int i = 0; i < n; i++) {
			Arrays.fill(dp[i], (long) 2e8);
			for (int p = 0; p < n; p++) {
				if (i == p)
					dp[i][p] = 0;
			}
		}
		for (int i = 0; i < m; i++) {
			int l = nextint();
			int r = nextint();
			long w = nextlong();
			dp[l][r] = dp[r][l] = w;
		}
		for (int i = 0; i < n; i++) {
			for (int p = 0; p < n; p++) {
				for (int q = 0; q < n; q++) {
					dp[p][q] = Math.min(dp[p][q], dp[p][i] + dp[i][q]);
				}
			}
		}
		for (int i = 0; i < n; i++) {
			for (int p = 0; p < n; p++) {
				printf("%d ", dp[i][p]);
			}
			println();
		}
		flsh();
	}

4:并查集

(树和图都一样,想最小生成树的选边就用到了,看一条边的左右祖先是否相同来选

  
public class bingchaji {
	static int fq[] = new int[100005], n, m;

	static int zhao(int x) {
		if (fq[x] == x)
			return fq[x];
		return fq[x] = zhao(fq[x]);
	}

	public static void main(String[] args) throws NumberFormatException, IOException {
		// TODO Auto-generated method stub
		n = nextint();
		m = nextint();
		for (int i = 0; i < n; i++) {
			fq[i] = i;
		}
		for (int i = 0; i < m; i++) {
			int l = nextint();
			int r = nextint();
			if (zhao(l) != zhao(r)) {
				fq[zhao(l)] = zhao(r);
			}//父亲是假的祖宗是真的
		}
		for (int i = 0; i < n; i++) {
			println(zhao(i));
		}
		flsh();
	}
}

5:拓扑排序

1.bfs
//小心有环计数检验,还有没有起点的坏情况
import java.io.*;
import java.util.*;

public class Main {
    static int n, rudu[], sl;
    static List<List<Integer>> res = new ArrayList<>();
    static Queue<Integer> q = new LinkedList<>();

    static void bfs() {
        for (int i = 0; i < n; i++) {
            if (rudu[i] == 0) {
                q.add(i);
                sl++;
            }
        }
        while (!q.isEmpty()) {
            int sl2 = q.size();
            for (int i = 0; i < sl2; i++) {
                int wz = q.poll();
                for (int x : res.get(wz)) {
                    rudu[x]--;
                    if (rudu[x] == 0) {
                        q.add(x);
                        sl++;
                    }
                }
            }
        }
    }

    public static void main(String[] args) throws Exception {
        n = nextint();
        rudu = new int[n];
        for (int i = 0; i < n; i++) {
            res.add(new ArrayList<>());
        }

        int m = nextint();
        for (int i = 0; i < m; i++) {
            int qd = nextint();
            int zd = nextint();
            rudu[zd]++;
            res.get(qd).add(zd);
        }
        bfs();
        if (sl == n) {
            println("YES");
        } else {
            println("NO");
        }
        flsh();
    }
}
2.dfs

(自然生成逆后续,感觉bfs的好写点?)

import java.io.*;
import java.util.*;

public class Main {
    static int n, m;
    static List<List<Integer>> res = new ArrayList<>();
    static boolean[] vis, path;
    static List<Integer> topo = new ArrayList<>();
    static boolean hasCycle = false;
    
    static void dfs(int u) {
        if (path[u]) {
            hasCycle = true;
            return;
        }
        if (vis[u]) return;
        vis[u] = true;
        path[u] = true;
        for (int v : res.get(u)) {
            dfs(v);
        }
        path[u] = false;
        topo.add(u);
    }
    
    public static void main(String[] args) throws Exception {
        n = nextint();
        m = nextint();
        for (int i = 0; i < n; i++) {
            res.add(new ArrayList<>());
        }
        for (int i = 0; i < m; i++) {
            int qd = nextint();
            int zd = nextint();
            res.get(qd).add(zd);
        }
        vis = new boolean[n];
        path = new boolean[n];
        for (int i = 0; i < n; i++) {
            if (!vis[i]) {
                dfs(i);
            }
        }
        
        if (hasCycle) {
            println("NO");
        } else {
            println("YES");
            for (int i = topo.size() - 1; i >= 0; i--) {
                print(topo.get(i) + " ");
            }
            println();
        }
        flsh();
    }
}

6:最小生成树

1:选边
class bjsz {
	int l, r;
	long w;

	bjsz(int l, int r, long w) {
		this.l = l;
		this.r = r;
		this.w = w;
	}
}

public class ktree {
	static int n, m, fq[] = new int[10005];
	static bjsz a[] = new bjsz[10005];

	static int zhao(int x) {
		if (fq[x] == x)
			return x;
		return fq[x] = zhao(fq[x]);
	}

	public static void main(String[] args) throws NumberFormatException, IOException {
		// TODO Auto-generated method stub
		n = nextint();
		m = nextint();
		for (int i = 0; i < n; i++) {
			fq[i] = i;
		}
		for (int i = 0; i < m; i++) {
			int l = nextint();
			int r = nextint();
			long w = nextlong();

			a[i] = new bjsz(l, r, w);
		}
		Arrays.sort(a, 0, m, (x, y) -> Long.compare(y.w, x.w));

		int z = 0, sl = 0;
		long jg = 0;
		while (z < m && sl < n) {
			int l = a[z].l;
			int r = a[z].r;
			if (zhao(l) != zhao(r)) {
				fq[zhao(r)] = zhao(l);

				sl++;
				jg += a[z].w;
			}
			z++;
		}
		println(jg);
		flsh();
	}
2:选点
class jgb {
	int zd;
	long rb;

	jgb(int r, long w) {
		zd = r;
		rb = w;
	}
}

public class Ptree {
	static char f[] = new char[10005];
	static List<List<jgb>> aList = new ArrayList<>();
	static long jg[] = new long[10005], ans;
	static int n, m;

	static void dij() {
		PriorityQueue<jgb> p = new PriorityQueue<>((la, u) -> Long.compare(la.rb, u.rb));
		p.add(new jgb(0, 0));
		jg[0] = 0;
		while (!p.isEmpty()) {
			jgb xJgb = p.poll();
			int qd = xJgb.zd;
			if (f[qd] == 0) {
				f[qd] = '1';
				ans += xJgb.rb;
				for (int i = 0; i < aList.get(qd).size(); i++) {
					jgb y = aList.get(qd).get(i);
					int qd2 = y.zd;
					long w1 = y.rb;
					if (jg[qd2] > jg[qd] + w1) {
						jg[qd2] = jg[qd] + w1;
						p.add(y);
					}
				}
			}
		}
		println(ans);
	}

	public static void main(String[] args) throws NumberFormatException, IOException {
		// TODO Auto-generated method stub
		n = nextint();
		m = nextint();
		for (int i = 0; i < n; i++) {
			aList.add(new ArrayList<>());

		}
		Arrays.fill(jg, (long) 2e9);
		for (int i = 0; i < m; i++) {
			int l = nextint();
			int r = nextint();
			long w = nextlong();
			aList.get(l).add(new jgb(r, w));
			aList.get(r).add(new jgb(l, w));
		}
		dij();
		flsh();
	}

4:数据结构

1:单调栈

    public int largestRectangleArea(int[] a) {
        int n = a.length, jg = 0, b[] = new int[n];
        Deque<Integer> dq1 = new ArrayDeque<>();
        for (int i = 0; i <= n; i++) {
            int sum=i==n?0:a[i];
            while (!dq1.isEmpty() && sum < a[dq1.peek()]) {
                int w = a[dq1.pop()];
                int l = dq1.isEmpty() ? -1 : dq1.peek();
                jg = Math.max(jg, w * (i - l - 1));
            }
            dq1.push(i);
        }
        return jg;
    }
    //这里拿力扣的之前写的一道单调栈当板子了

2:线段树

挖个坑放这里吧(*)

3:树状数组

挖坑没学(*)

5:搜索

1:记忆化

//一种思维,类似剪枝的感觉(?
if(a[i]!=0)continue;
//大概这种

2:dfs

    List<List<Integer>> jg = new ArrayList<>();
    int n, f[], ans[],b[];
    void pd() {
        List<Integer> a = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            a.add(b[ans[i]-1]);
        }
        jg.add(a);
    }

    void dfs(int x) {
        if (x == n) {
            pd();
            return;
        }
        for (int i = 1; i <= n; i++) {
            if (f[i] == 0) {
                f[i] = 1;
                ans[x] = i;
                dfs(x + 1);
                f[i] = 0;
            }
        }
    }

记得加剪枝(个人喜欢用这种的什么都能套)

6:dp

1:01背包

		for(int i=1;i<=n;i++) {
			for(int p=m;p>=w[i];p--) {//自有防止越界机制
				 dp[p]=Math.max(dp[p],dp[p-w[i]]+v[i]);
			}
		}

2:完全背包

		for(int i=1;i<=n;i++) {
			for(int p=w[i];p<=h;p++) {//自有防止越界机制
				 dp[p]=Math.min(dp[p],dp[p-w[i]]+v[i]);
			}
		}

3:线性

相邻转移什么的,这里放个打家劫舍
    public int rob(int[] a) {
        int n = a.length,dp[]=new int[n],max=0;
        if(n==1)return a[0];
        if(n==2)return Math.max(a[0],a[1]);
        if(n==3)return Math.max(a[1],a[0]+a[2]);
        dp[0]=a[0];dp[1]=a[1];dp[2]=dp[0]+a[2];
        for (int i = 0; i < 3; i++) {
            max=Math.max(dp[i],max);
        }
        for (int i = 3; i <n ; i++) {
            dp[i]=Math.max(dp[i-2]+a[i],dp[i-3]+a[i]);max=Math.max(dp[i],max);
        }
        return max;
    }

4:区间

   public String longestPalindrome(String s) {
        int n = s.length(),max=0,zb=0,rb=0;
        boolean dp[][] = new boolean[n + 1][n + 1];
        for (int i = 0; i < n; i++) {
            dp[i][i] = true;
        }

            for (int j = 2; j <= n; j++) {
                for (int i = 0; i+j-1 < n; i++) {
                if (s.charAt(i) == s.charAt(i + j - 1)) {
                    if (j != 2) {
                        if (!dp[i + 1][i + j - 2]) continue;
                    }
                    dp[i][i + j - 1] = true;
                    if(j>max){
                        max=j;
                        zb=i;rb=i+j-1;
                    }
                }

            }
        }
        if(max==0)return s.substring(0,1);
        return s.substring(zb,rb+1);
    }
    最长回文子串虽然不是最优时间但是也区间dp放这里了(

5:状压

挖坑(*)

6:树形

挖坑(*)

7:字符串的匹配

1:哈希

简单好写但时间复杂度高点

		String s = next();
		int l = s.length();
		HashMap<String, Integer> m = new HashMap<>();
		for (int i = 0; i < l; i++) {
			for (int p = i + 1; p <= l; p++) {
				String s2 = s.substring(i, p);
				m.put(s2, m.getOrDefault(s2, 0) + 1);
			}
		}
		String x = next();
		println(m.getOrDefault(x, 0));
2:kmp
//这个是旧的,输入输出没改快读快写
import java.util.Scanner;

public class Main {
    static String s1,s2;
    static char[]c1;
    static char[]c2;
    static int[]jg=new int[100005];
    static int l1,l2,f;
    static int[]next=new int[1000005];
    static void get(){
        next[0]=-1;
        int i=0,j=-1;
        while(i<l2){
            if(j==-1||c2[i]==c2[j]){
                i++;
                j++;
                next[i]=j;
            }
            else{
                j=next[j];
            }
        }
    }
    static void kmp(){
        int i=0,j=0;
        while(i<l1&&j<l2){
            if(j==-1||c1[i]==c2[j]){
                i++;
                j++;
            }
            else{
                j=next[j];
            }
            if (j == l2) {
                jg[f++]=i-j+1;
                j=next[j];
            }
        }
    }
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        s1 = sc.next();
        s2 = sc.next();
        c1=s1.toCharArray();
        c2=s2.toCharArray();
        l1=s1.length();
        l2=s2.length();
        get();
        kmp();
        for (int i = 0; i < f; i++) {
            System.out.printf("%d ",jg[i]);
        }
    }
}

贪心,双指针,滑动窗口,二分,前缀和这种这里没记,这里主要记的也是自己考前过一遍,怕这种记忆板子忘记的()

其它一些零碎的

一些别的什么的()

1Big

BigInterger可以实现gcd,modpow自带的)
nigdemical小数->setscale保存
       System.out.println(num.setScale(2, RoundingMode.HALF_UP));  // 3.14,这也是四舍五入的
        
        // 截断到整数位
        System.out.println(num.setScale(0, RoundingMode.DOWN));     // 3
        
        // 向上取整!!!
        System.out.println(num.setScale(0, RoundingMode.CEILING));   // 4
        
        // 向下取整!!!
        System.out.println(num.setScale(0, RoundingMode.FLOOR));     // 3

2生成随机字符

1建立一个字符串,生成随即索引进行访问
char[] specials = {'!','@','#','$','%','^','&','*'};
return specials[new Random().nextInt(specials.length)];//都想起来了
2随机as码再强转

3时间类

Date用起来简单但是灵活度不高
创建对象时如果是空的就是显示时间剩下复制ms再计算
可以toString再切割取出来唉唉
//小心东八区的诱惑

4hashmap

还有merge可以用

 public class test2 {
	static int t;
	static long n;
	static HashMap<Long, Long> h = new HashMap<>();

	static long dfs(long x) {
		if (x <= 2) {
			h.put(x, 0L);
			return h.get(x);
		}
		if (x == 3) {
			h.put(x, 1L);
			return h.get(x);
		}
		if (x % 2 == 0) {
			h.put(x, 2 * dfs(x / 2));
			return h.get(x);
		}

		else {

			h.put(x, dfs(x / 2) + dfs(x / 2 + 1) + 1);
			return h.get(x);
		}
	}

	public static void main(String[] args) throws NumberFormatException, IOException {
		// TODO Auto-generated method stub
		t = nextint();
		for (int i = 0; i < t; i++) {

			n = nextlong();
			if (h.containsKey(n)) {
				println(h.get(n));
			} else {
				dfs(n);
				println(h.get(n));
			}
		}
		flsh();
	}

	 

5拼接字符串 StringBuilder 文件内容变字符串存内存里,接下来进行处理再吐出去

 		StringBuilder s=new StringBuilder();
         FileReader fr=new FileReader("in.txt");
         int ch;
         while((ch=fr.read())!=-1){
             s.append((char)ch);
         }
         fr.close();
 StringBuilder 是 Java 中一个可变的字符序列类,用于在不生成大量临时 String 对象的情况下进行字符串操作,从而提高性能。它的方法包含:
append():向已有字符串末尾追加内容
insert():在指定位置插入内容
delete():删除指定范围内的字符
reverse():将当前字符序列反转
toString():将 StringBuilder 转换为 String  
https://www.nowcoder.com/practice/e10ed38e315442fc9772004c58582c8f?channelPut=w25springcamp
对这个的使用
	static void solve() throws NumberFormatException, IOException {
		n = nextint();
		m = nextint();
		StringBuilder sb = new StringBuilder(next());
		for (int i = 0; i < m; i++) {
			int l = nextint() - 1;
			int r = nextint() - 1;
			f1 = 0;
			for (int p = l; p <= r; p++) {
				sb.insert(p + f1, sb.charAt(p + f1));
				f1++;
			}
		}
		println(sb.toString());

	}

6不使用类(类似结构体)来存排序位置 索引Integer 排序(索引,值[o,p]) 索引对应排序后的位置

		for (int i = 0; i < n; i++) {
			c[i] = i;
			d[i] = nextint();
		}
		Arrays.sort(c, (o, p) -> {
			return Long.compare(d[o], d[p]);
		});
		写法简便
		long a[] = new long[n];
		for (int i = 0; i < n; i++) {
			a[i] = nextlong() * (i + 1);
		}
		Integer b[] = new Integer[n];
		Integer c[] = new Integer[n];
		int d[] = new int[n];
		for (int i = 0; i < n; i++) {
			b[i] = c[i] = i;
			d[i] = nextint();
		}
		Arrays.sort(b, (o, p) -> {
			return Long.compare(a[o], a[p]);
		});
		Arrays.sort(c, (o, p) -> {
			return d[o] - d[p];
		});
		int ans[] = new int[n];
		for (int i = 0; i < n; i++) {
			ans[b[i]] = d[c[i]];
		}
        代替
		TreeMap<Integer, Integer> t = new TreeMap<>();
		for (int i = 0; i < n; i++) {
			a[i] = new px(nextlong() * (i + 1), i);
		}
		Arrays.sort(a, 0, n, (o, p) -> {
			return Long.compare(o.w, p.w);
		});
		for (int i = 0; i < n; i++) {
			c[a[i].wz] = n - i;
		}
		for (int i = 0; i < n; i++) {
			b[i] = nextint();
		}
		Arrays.sort(b, 0, n, (o, p) -> p - o);
		for (int i = 0; i < n; i++) {
			t.put(i + 1, b[i]);
		}       

7其他的

神秘小知识(不要忘了本手处理才是一切)
1自带的进制转换int num = 10;
String binary = Integer.toBinaryString(num);//其他的类似
//别记错了不是bigint独有是String
2低效查找字符串用indexof
3二分没找到的返回值满足(-wz)-1=实际(((这里的实际是针对数组绝对位置比如找23那么只会1||3,同理120||2
4Collection的好用方法,max,min,
5String的subSring可以任意拿一部分作为新的字符串
6 // 使用getOrDefault一行解决
int count = map.getOrDefault("banana", 0); // 不存在时返回默认值0
7long越界就biginterger搞进去
8a.endsWith(b.substring(0, k))后是前(枚举k
9	t.computeIfAbsent((long) 0, op -> new ArrayList<>()).add((long) 0);
10ctrl alt t一键if else什么的

再说一下JAVAB组比CPP的B组感觉拿奖更简单(py不知道),如果就为了奖可以试试,我那会刚出考场还以为寄了呢,大部分都纯暴力(或者只解决小样例,也可以对着不同的n的范围写不同的函数),或者打表来骗分,还记得有个并查集板子没写出来...没想到出成绩还国一了...

这里推荐一个b站up yeVegetable (牛客的苯环大佬)和他的洛谷题单(可以b站简介进他的交流群群公告有题单),我当时做了他的洛谷的省赛和国赛题单感觉挺多帮助的吧!

再叠个甲,我纯苟若(有错误的地方也请大佬指出我改一下)如果有帮到你请点个赞!

最后附一个