排列字母
原题链接
问题描述
小蓝要把一个字符串中的字母按其在字母表中的顺序排列。
例如,LANQIAO 排列后为 AAILNOQ。
又如,GOODGOODSTUDYDAYDAYUP 排列后为 AADDDDDGGOOOOPSTUUYYY。
请问对于以下字符串,排列之后字符串是什么?WHERETHEREISAWILLTHEREISAWAY
解题思路
排序
参考代码
import java.util.*;
import java.io.*;
public class Main {
static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
static PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
public static void main(String[] args) throws Exception {
char[] ch = "WHERETHEREISAWILLTHEREISAWAY".toCharArray();
Arrays.sort(ch);
for (int i = 0; i < ch.length; i++) out.print(ch[i]);
out.flush();
in.close();
}
}
GCD
原题链接
问题描述
解题思路
根据上述定理 gcd(a, b) = gcd(a, b - a) (题目保证 b > a)
可以得到 gcd(a + k, b + k) = gcd(a + k, b + k - (a + k)) = gcd(a + k, b - a)
设 c = b - c 可以得到 gcd(a + k, c)
a + k
与c
的最大公约数是多少呢?答案是 c
如果 a + k < c ,则最大公约数为 a + k;如果 a + k >= c,则最大公约数为 c
当(a + k) % c = 0
时,最大公约数为 c
如果 a % c = 0,则 k = 0,否则k = (a / c + 1) * c - a
参考代码
import java.io.*;
public class Main {
static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
static PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
public static void main(String[] args) throws Exception {
String[] s = in.readLine().split(" ");
long a = Long.parseLong(s[0]), b = Long.parseLong(s[1]);
long c = b - a;
if (a % c == 0) {
out.println(0);
} else {
out.println((a / c + 1) * c - a);
}
out.flush();
in.close();
}
}
选数异或
原题链接
问题描述
解题思路
异或运算满足:a ^ a = 0
a ^ b = x 可以转换为 a ^ b ^ b = x ^ b,即 a = x ^ b
在遍历这个数组的时候,当前的遍历到了 arr[i] ,借助哈希表可以很快的找到 x ^ arr[i] 是否在之前出现过(这里哈希表存放的是数 x 最后一次出现的位置,这样可以有更多的区间能覆盖到)
参考代码
import java.util.*;
import java.io.*;
public class Main {
static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
static PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
static int N = (int)1e5 + 10, n, m, x;
static int[] a = new int[N], f = new int[N];
public static void main(String[] args) throws Exception {
String[] s = in.readLine().split(" ");
n = Integer.parseInt(s[0]);
m = Integer.parseInt(s[1]);
x = Integer.parseInt(s[2]);
Map<Integer,Integer> map = new HashMap<>();
s = in.readLine().split(" ");
for (int i = 1; i <= n; i++) {
a[i] = Integer.parseInt(s[i - 1]);
f[i] = Math.max(map.getOrDefault(a[i] ^ x, 0), f[i - 1]);
map.put(a[i], i);
}
while (m-- > 0) {
s = in.readLine().split(" ");
int l = Integer.parseInt(s[0]), r = Integer.parseInt(s[1]);
if (f[r] >= l) {
out.println("yes");
} else {
out.println("no");
}
}
out.flush();
in.close();
}
}
背包与魔法
原题链接
问题描述
解题思路
01背包变形
参考代码
import java.util.*;
import java.io.*;
public class Main {
static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
static PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
static int N = 2010, M = 10010, n, m, k;
static int[] v = new int[N], w = new int[N];
static int[][] f = new int[M][2];
public static int max(int... nums) {
int ans = nums[0];
for (int num: nums) ans = Math.max(ans, num);
return ans;
}
public static void main(String[] args) throws Exception {
String[] s = in.readLine().split(" ");
n = Integer.parseInt(s[0]);
m = Integer.parseInt(s[1]);
k = Integer.parseInt(s[2]);
for (int i = 1; i <= n; i++) {
s = in.readLine().split(" ");
v[i] = Integer.parseInt(s[0]);
w[i] = Integer.parseInt(s[1]);
}
for (int i = 1; i <= n; i++) {
for (int j = m; j >= v[i]; j--) {
// 不使用魔法
f[j][0] = max(f[j][0], f[j - v[i]][0] + w[i]);
// 使用魔法
if (v[i] + k <= j) {
f[j][1] = max(f[j][1], f[j - v[i]][1] + w[i], f[j - v[i] - k][0] + w[i] * 2);
}
}
}
out.println(Math.max(f[m][0], f[m][1]));
out.flush();
in.close();
}
}