第一题给的题目意思是输入两个数组,第一个数组是子线程(子节点),第二个数组是父线程(父节点),删除一个线程会删除此线程的所有子线程,问给出一个要删除的线程,总共要删除掉几个线程?
package test.test05;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
public class Main3
{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
String a = scanner.nextLine();
String b = scanner.nextLine();
int temp = scanner.nextInt();
String[] s1 = a.split(" ");
int[] kids = new int[s1.length];
for (int i = 0; i < s1.length; i++)
{
kids[i] = Integer.parseInt(s1[i]);
}
String[] s2 = b.split(" ");
int[] parents = new int[s2.length];
for (int i = 0; i < s2.length; i++)
{
parents[i] = Integer.parseInt(s2[i]);
}
scanner.close();
Queue<Integer> queue = new LinkedList<>();
queue.offer(temp);
int length = parents.length;
int count = 0;
while (!queue.isEmpty())
{
temp = queue.poll();
for (int i = 0; i < kids.length; i++)
{
if (parents[i] == temp && kids[i] != 0)
{
queue.offer(kids[i]);
}
}
count++;
}
System.out.println(count);
}
}
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/1af1676ea49de01d857acff7bb5ad19e.png)
package test.test05;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class Main2
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
String s = sc.nextLine();
char[] chars = s.toCharArray();
int length = chars.length;
int count = 0;
Map<Character, Integer> map = new HashMap<>();
ArrayList<Integer> list = new ArrayList<>();
int i;
for ( i = 0; i < length; i++)
{
for (int j = i; j < length ; j++)
{
if (!map.containsKey(chars[j]))
{
map.put(chars[j], null);
++count;
}else {
break;
}
}
list.add(count);
count = 0;
map.clear();
}
int result = list.get(0);
for (int k = 1; k < list.size(); k++)
{
if(list.get(k) >= result){
result = list.get(k);
}
}
System.out.println(result);
}
}