diff --git "a/OJ\351\242\230\350\247\243/src/\345\244\247\346\225\264\346\225\260\350\277\220\347\256\227\346\212\200\345\267\247/HDOJ\351\230\266\344\271\2302940.java" "b/OJ\351\242\230\350\247\243/src/\345\244\247\346\225\264\346\225\260\350\277\220\347\256\227\346\212\200\345\267\247/HDOJ\351\230\266\344\271\2302940.java" index 7907907b0dfeeadf72672bd89e6e370f3d1b582a..b98c91e4b61c95118188217f15e4e87f741a149f 100644 --- "a/OJ\351\242\230\350\247\243/src/\345\244\247\346\225\264\346\225\260\350\277\220\347\256\227\346\212\200\345\267\247/HDOJ\351\230\266\344\271\2302940.java" +++ "b/OJ\351\242\230\350\247\243/src/\345\244\247\346\225\264\346\225\260\350\277\220\347\256\227\346\212\200\345\267\247/HDOJ\351\230\266\344\271\2302940.java" @@ -42,5 +42,4 @@ public class HDOJ阶乘2940 { } } } - } diff --git a/algorithm/UnionFind/UnionFind.java b/algorithm/UnionFind/UnionFind.java index d96b1020e373f1a5b31c15c551a1d0e11b8df1ba..4302bdabdf6f6fea098d6b578cbc1d4e79fce594 100644 --- a/algorithm/UnionFind/UnionFind.java +++ b/algorithm/UnionFind/UnionFind.java @@ -1,13 +1,14 @@ package UnionFind; + //最优实现并查集 public class UnionFind { public int[] parent; private int[] rank; UnionFind(int n) { - parent = new int[n+1]; - rank = new int[n+1]; - for (int i = 0; i < n+1; i++) { + parent = new int[n + 1]; + rank = new int[n + 1]; + for (int i = 0; i < n + 1; i++) { parent[i] = i; rank[i] = 1; } diff --git "a/algorithm/Water/\346\234\200\345\244\247\346\234\200\345\260\217\345\205\254\345\200\215\346\225\260.java" "b/algorithm/Water/\346\234\200\345\244\247\346\234\200\345\260\217\345\205\254\345\200\215\346\225\260.java" new file mode 100644 index 0000000000000000000000000000000000000000..4953b81548e36a9c7c9ece9034f25844718096eb --- /dev/null +++ "b/algorithm/Water/\346\234\200\345\244\247\346\234\200\345\260\217\345\205\254\345\200\215\346\225\260.java" @@ -0,0 +1,7 @@ +package Water; + +public class 最大最小公倍数 { + public static void main(String[] args) { + + } +} diff --git "a/algorithm/Water/\346\235\250\350\276\211\344\270\211\350\247\222\345\275\242.java" "b/algorithm/Water/\346\235\250\350\276\211\344\270\211\350\247\222\345\275\242.java" new file mode 100644 index 0000000000000000000000000000000000000000..d0229eb1d3b1e0268e3b86826ad998d31ad3a0c6 --- /dev/null +++ "b/algorithm/Water/\346\235\250\350\276\211\344\270\211\350\247\222\345\275\242.java" @@ -0,0 +1,67 @@ +package Water; + +import java.util.Scanner; + +/** + * http://lx.lanqiao.cn/problem.page?gpid=T10 + * 问题描述 + * 杨辉三角形又称Pascal三角形,它的第i+1行是(a+b)i的展开式的系数。 + * + * + * 它的一个重要性质是:三角形中的每个数字等于它两肩上的数字相加。 + * + * + * 下面给出了杨辉三角形的前4行: + * + * + * 1 + * + * + * 1 1 + * + * + * 1 2 1 + * + * + * 1 3 3 1 + * + * + * 给出n,输出它的前n行。 + * + * 输入格式 + * 输入包含一个数n。 + * + * 输出格式 + * 输出杨辉三角形的前n行。每一行从这一行的第一个数开始依次输出,中间使用一个空格分隔。请不要在前面输出多余的空格。 + * 样例输入 + * 4 + * 样例输出 + * 1 + * 1 1 + * 1 2 1 + * 1 3 3 1 + * 数据规模与约定 + * 1 <= n <= 34。 + */ +public class 杨辉三角形 { + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + int n = sc.nextInt(); + int[][] dp = new int[n][n]; + dp[0][0] = 1; + for (int i = 1; i < n; i++) { + for (int j = 0; j < i + 1; j++) { + if (j - 1 >= 0) + dp[i][j] = dp[i - 1][j - 1] + dp[i - 1][j]; + else + dp[i][j] = dp[i - 1][j]; + } + } + for (int i = 0; i < n; i++) { + for (int j = 0; j < i + 1; j++) { + System.out.print(dp[i][j] + " "); + } + System.out.println(); + } + } +} diff --git "a/algorithm/Water/\351\230\266\344\271\230.java" "b/algorithm/Water/\351\230\266\344\271\230.java" new file mode 100644 index 0000000000000000000000000000000000000000..b806d7d40fe7efda8a3355baeaa1e2bab8123b8b --- /dev/null +++ "b/algorithm/Water/\351\230\266\344\271\230.java" @@ -0,0 +1,25 @@ +package Water; + +import java.math.BigInteger; +import java.util.Scanner; + +public class 阶乘 { + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + int nextInt = sc.nextInt(); + BigInteger b = BigInteger.ONE; + for (long i = 1; i <= nextInt; i++) { + b = b.multiply(BigInteger.valueOf(i)); + } + System.out.println(s(b.toString())); + } + + + public static char s(String n) { + for (int i = n.length() - 1; i >= 0; i--) { + if (n.charAt(i) != '0') + return n.charAt(i); + } + return 0; + } +} diff --git a/algorithm/com/shiyu/Main.java b/algorithm/com/shiyu/Main.java index b12c0aa6fd2499a6b238ed8bdcdd809ab64e9914..021db2e23eace547675af76f7fb414717d4bfd5d 100644 --- a/algorithm/com/shiyu/Main.java +++ b/algorithm/com/shiyu/Main.java @@ -1,60 +1,12 @@ package com.shiyu; -import java.util.*; +import java.util.Scanner; public class Main { - private static class Node implements Comparable { - public int Juli; - public int You = 0; - - public Node(int juli) { - Juli = juli; - } - - @Override - public int compareTo(Node node) { - return this.Juli - node.Juli; - } - } public static void main(String[] args) { Scanner sc = new Scanner(System.in); - int N = sc.nextInt();//几个加油站 - Node[] node = new Node[N + 1]; - for (int i = 0; i < N; i++) { - node[i] = new Node(sc.nextInt()); - node[i].You = sc.nextInt(); - } - int L = sc.nextInt(); - int P = sc.nextInt(); - for (int i = 0; i < N; i++) { - node[i].Juli = L - node[i].Juli;//更改为加油站距离起点的位置 - } - node[N] = new Node(L); - Arrays.sort(node); - PriorityQueue pq = new PriorityQueue(N + 10, new Comparator() { - @Override - public int compare(Integer t1, Integer t2) { - return t2 - t1; - } - }); - int res = 0;//加油次数 - int pos = 0;//当前所在位置 - int tank = P;//油箱剩的油的数量 - for (int i = 0; i <= N; i++) {//把终点看做加油站,看能不能到这个加油站 - int d = node[i].Juli - pos;//卡车的位置从0开始,到达第i个加油站需要多少油 - while (tank - d < 0) {//如果无法到达第i个加油站 - if (pq.size() == 0)//从优先队列里面没有加油站,没有刻 - System.out.println("-1"); - tank += pq.poll(); - res++; - } - tank -= d;//减去走到第i个加油站需要的油,区间是什么呢, - // 第一次区间(0,A[i])需要这么多油,第二次及以后区间(A[i-1],A[i]) - // 每次都要维护油的数量,区间消耗的油 - pos = node[i].Juli;//卡车开到第i个加油站 - pq.offer(node[i].You);//每到一个加油站,就把能加的油加进优先队列 - } - System.out.println(res); + String next = sc.next(); + System.out.println(next.toLowerCase()); } -} +} \ No newline at end of file diff --git a/algorithm/com/shiyu/tsdt.java b/algorithm/com/shiyu/tsdt.java index 4df40270c39401c4e9fe2ed2e478c64408461d4e..e1774a21271e2058b27a4c03aaf8f56d56e62962 100644 --- a/algorithm/com/shiyu/tsdt.java +++ b/algorithm/com/shiyu/tsdt.java @@ -2,6 +2,10 @@ package com.shiyu; public class tsdt { public static void main(String[] args) { + System.out.println(153); + System.out.println(370); + System.out.println(371); + System.out.println(407); } diff --git "a/algorithm/com/shiyu/\345\274\200\347\201\257.java" "b/algorithm/com/shiyu/\345\274\200\347\201\257.java" index b735e20417452ab5b8be95e11e800f328c4fe1bb..e140d7ccab24ff0c7591193d100fbf88db678d8e 100644 --- "a/algorithm/com/shiyu/\345\274\200\347\201\257.java" +++ "b/algorithm/com/shiyu/\345\274\200\347\201\257.java" @@ -31,4 +31,4 @@ public class 开灯 { System.out.print(i + " "); } } -} +} \ No newline at end of file diff --git a/algorithm/string/KMP.java b/algorithm/string/KMP.java index 4984acf98b553ab163e64007630823c3d2175330..3dc88f246ee90d471294a0b74b4ed78cad264f08 100644 --- a/algorithm/string/KMP.java +++ b/algorithm/string/KMP.java @@ -1,10 +1,34 @@ package string; +import java.util.Arrays; +import java.util.Random; + /** * KMP字符串匹配...未完 */ public class KMP { public static void main(String[] args) { + for (int i = 3; i < 1000; i++) { + String st = randomStr(i); + if (!Arrays.equals(next(st), nextt(st))) { + System.out.println(st); + System.out.println(Arrays.toString(next(st))); + System.out.println(Arrays.toString(nextt(st))); + } + } + System.out.println(Arrays.toString(next("abaabcac"))); + System.out.println(Arrays.toString(nextt("abaabcac"))); + + } + + public static String randomStr(int len) { + StringBuilder s = new StringBuilder(); + for (int i = 0; i < len; i++) { + Random r = new Random(); + char random = (char) (r.nextInt(26) + 'a'); + s.append(random); + } + return s.toString(); } @@ -29,6 +53,13 @@ public class KMP { return -1; } + /** + * 前缀 指除了最后一个字符以外,一个字符串的全部头部组合; + * 后缀 指除了第一个字符以外,一个字符串的全部尾部组合。 + * + * @param ps 模式串 + * @return 求解next数组 + */ public static int[] next(String ps) { int pLength = ps.length(); int[] next = new int[pLength]; @@ -38,6 +69,7 @@ public class KMP { return next; } next[1] = 0; + int j = 1; int k = next[j];//看看j的最长匹配前缀在哪里 while (j < pLength - 1) { @@ -48,8 +80,21 @@ public class KMP { return next; } - public static void m() { - + public static int[] nextt(String ps) { + int pLength = ps.length(); + int[] next = new int[pLength]; + char[] p = ps.toCharArray(); + next[0] = -1; + if (ps.length() == 1) { + return next; + } + int j = 0, k = -1; + while (j < ps.length() - 1) { + if (k == -1 || p[j] == p[k]) { + next[++j] = ++k; + } else k = next[k];//此语句是这段代码最反人类的地方,如果你一下子就能看懂,那么请允许我称呼你一声大神! + } + return next; } } diff --git a/algorithm/wei/zhuanhuan.java b/algorithm/wei/zhuanhuan.java index 3fa58acabf0995c10fc0048617beea3944a187b1..1e8dacfbf58be07621ccabc324d8f9124ae86bdd 100644 --- a/algorithm/wei/zhuanhuan.java +++ b/algorithm/wei/zhuanhuan.java @@ -59,6 +59,4 @@ public class zhuanhuan { } return count; } - - } \ No newline at end of file diff --git "a/algorithm/\350\277\233\345\210\266\350\275\254\346\215\242/\350\277\233\345\210\266\350\275\254\346\215\242.java" "b/algorithm/\350\277\233\345\210\266\350\275\254\346\215\242/\350\277\233\345\210\266\350\275\254\346\215\242.java" index 3291596c65e1a77f2436e44c236626587b3c1640..802c89611751a1dc39bf2a1c500839264d84b86b 100644 --- "a/algorithm/\350\277\233\345\210\266\350\275\254\346\215\242/\350\277\233\345\210\266\350\275\254\346\215\242.java" +++ "b/algorithm/\350\277\233\345\210\266\350\275\254\346\215\242/\350\277\233\345\210\266\350\275\254\346\215\242.java" @@ -3,7 +3,7 @@ package 进制转换; public class 进制转换 { public static void main(String[] args) { System.out.println(Integer.toBinaryString(123)); - System.out.println(change(123,2)); + System.out.println(change(123, 2)); } public static char[] arr = {'a', 'b'}; @@ -41,4 +41,43 @@ public class 进制转换 { } } } + + /** + * @param n 需要转的10进制数字 + */ + public static void Ten转16进制(int n) { + char[] arr = new char[2576]; + int cur = n; + int count = 0; + while (cur != 0) { + if (cur % 16 >= 10) + arr[count++] = (char) (cur % 16 - 10 + 'A'); + else + arr[count++] = (char) (cur % 16 + '0'); + cur /= 16; + } + StringBuilder sb = new StringBuilder(); + for (int i = count - 1; i >= 0; i--) { + sb.append(arr[i]); + } + System.out.println(sb); + } + + /** + * @param n 需要转的16进制数 + */ + public static void Sixto10进制(String n) { + long res = 0; + int t = 0; + int c = 0; + for (int i = n.length() - 1; i >= 0; i--) { + if (n.charAt(i) >= 'A') { + t = n.charAt(i) - 'A' + 10; + } else { + t = n.charAt(i) - '0'; + } + res += t * Math.pow(16, c++); + } + System.out.println(res); + } }