第一题：用户喜好

5
1 2 3 3 5
3
1 2 1
2 4 5
3 5 3

1
0
2

代码

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

public class Main {

public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while(scanner.hasNext()) {
int n = scanner.nextInt();
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = scanner.nextInt();
}
Map<Integer, List<Integer>> map = new HashMap<>();
for (int i = 0; i < n; i++) {
int key = a[i];
if (map.containsKey(key)) {
List<Integer> list = map.get(key);
}else {
List<Integer> list = new ArrayList<>();
map.put(key, list);
}
}
int q = scanner.nextInt();
while((q--) > 0) {
int l = scanner.nextInt();
int r = scanner.nextInt();
int k = scanner.nextInt();
int count = 0;
List<Integer> list = map.get(k);
if (list != null) {
for(Integer integer : list) {
if (integer >= l && integer <= r) {
count++;
}
}
}
System.out.println(count);
}
}
}

}


第二题：手串

5 2 3
3 1 2 3
0
2 2 3
1 2
1 3

2

分析

m个连续串珠内不能一种颜色出现两次，记录不合格的颜色数量，就变成了获取每种颜色的串珠标号列表List，判断List中相邻的两个数之间的差值是否大于m。

代码

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

public class Main2 {

public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while(scanner.hasNext()) {
int n = scanner.nextInt();
int m = scanner.nextInt();
int c = scanner.nextInt();
Map<Integer, List<Integer>> map = new HashMap<>();
for(int i=0; i<n; i++) {
int num = scanner.nextInt();
for(int j=0; j<num; j++) {
int key = scanner.nextInt();
if (map.containsKey(key)) {
List<Integer> list = map.get(key);
}else {
List<Integer> list = new ArrayList<>();
map.put(key, list);
}
}
}
int count = 0;
Boolean flag;
for (int i = 1; i <= c; i++) {
flag = false;
List<Integer> list = map.get(i);
if (list != null) {
Collections.sort(list);
int j;
for (j = 0; j < list.size()-1; j++) {
int left = list.get(j);
int right = list.get(j+1);
if (left + m > right) {
count++;
flag = true;
break;
}
}
if (!flag && (j > 0) && ((list.get(j) + m) % n) > list.get(0)) {
count++;
}
}
}
System.out.println(count);
}
}

}


第三题：字母交换

abcbaa 2

2

分析

https://blog.csdn.net/flushhip/article/details/79416715

代码

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

public class Main3 {

public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while(scanner.hasNext()) {
String string = scanner.next();
int m = scanner.nextInt();
char[] cs = string.toCharArray();
Map<Integer, List<Integer>> map = new HashMap<>();
for (int i = 0; i < cs.length; i++) {
int key = cs[i] - 'a';
if (map.containsKey(key)) {
List<Integer> list = map.get(key);
}else {
List<Integer> list = new ArrayList<>();
map.put(key, list);
}
}
int count = 0;
for(int i=0;i<26;i++) {
List<Integer> list = map.get(i);

if (list != null) {
int[][] dp = new int[list.size()][list.size()];
//len指连续子串长度
for(int len=2;len<=list.size();len++) {
//j指子串起始位置，(j + len -1)指子串结束位置，
//dp[j][j+len-1]指在j~(j+len-1)这段子串移动成连续子串所需要的次数
for (int j = 0; j + len - 1 < list.size(); j ++) {
/*dp[j+1][j+len-2]指将(j+1)~(j+len-2)之间的目标字母移动到一起，
这个移动次数就是dp[i + 1][i + len - 2];
然后将两个端点的字母向中间移，移动的距离就为
(list.get(j+len-1) - list.get(j)) - (len-1)
*/
dp[j][j+len-1] = dp[j+1][j+len-2] + list.get(j+len-1) - list.get(j) + 1 -len;
if (dp[j][j+len-1] <= m && count < len) {
count = len;
}
}
}
}
}
System.out.println(count);
}
}

}


©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客