import java.util.ArrayList;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (in.hasNextLine()) {
int n = in.nextInt(); // 手串珠子数量
int m = in.nextInt();// 连续m个。
int c = in.nextInt(); // 颜色有很多种。
ArrayList<ArrayList<Integer>> inputData = new ArrayList<>();
for (int i = 0; i < n; i++) {
int currentC = in.nextInt();
ArrayList<Integer> oneData = new ArrayList<>();
for (int j = 0; j < currentC; j++) {
oneData.add(in.nextInt());
}
inputData.add(oneData);
}
getResult(n, m, c, inputData);
}
}
public static void getResult(int n, int m, int c, ArrayList<ArrayList<Integer>> colors) {
boolean[] result = new boolean[c + 1];
int[] memo = new int[c + 1];//窗口的当前状态
for (int i = 0; i < n + m; i++) {
if (i < m) {
putMemo(colors.get(i), memo);
} else {
outMemo(colors.get(i - m), memo);//移除窗口
putMemo(colors.get(i % n), memo);//移入窗口
}
for (int k = 1; k <= c; k++) {
if (memo[k] > 1) {
result[k] = true;
}
}
}
int num = 0;
for (int k = 1; k <= c; k++) {
if (result[k]) {
num++;
}
}
System.out.println(num);
}
public static void putMemo(ArrayList<Integer> color, int[] memo) {
for (Integer col : color) {
memo[col]++;
}
}
public static void outMemo(ArrayList<Integer> color, int[] memo) {
for (Integer col : color) {
memo[col]--;
}
}
}
今日头条编程题 - 珠子颜色【窗口滑动】
最新推荐文章于 2022-03-08 13:47:11 发布