周练

第一题:
Bizon the Champion isn’t just a bison. He also is a favorite of the “Bizons” team.
At a competition the “Bizons” got the following problem: “You are given two distinct words (strings of English letters), s and t. You need to transform word s into word t”. The task looked simple to the guys because they know the suffix data structures well. Bizon Senior loves suffix automaton. By applying it once to a string, he can remove from this string any single character. Bizon Middle knows suffix array well. By applying it once to a string, he can swap any two characters of this string. The guys do not know anything about the suffix tree, but it can help them do much more.
Bizon the Champion wonders whether the “Bizons” can solve the problem. Perhaps, the solution do not require both data structures. Find out whether the guys can solve the problem and if they can, how do they do it? Can they solve it either only with use of suffix automaton or only with use of suffix array or they need both structures? Note that any structure may be used an unlimited number of times, the structures may be used in any order.

import java.util.*;
public class Main{
     
	public static void main(String[] args) {
     
		Scanner sc = new Scanner(System.in);
		String a = sc.next();
		String b = sc.next();
		int s[] = new int[26];
		int x = 0;
		if (a.length() == b.length()) {
     
			for (int i = 0; i < a.length(); i++)
				s[a.charAt(i) - 97]++;
			for (int i = 0; i < b.length(); i++)
				s[b.charAt(i) - 97]--;
			Arrays.sort(s);
			if (s[0] < 0 || s[25] > 0)
				System.out.println("need tree");
			else
				System.out.println("array");
		} else if (a.length() > b.length()) {
     
			boolean boo = true;
			boolean bo = false;
			for (int i = 0; i < a.length(); i++)
				s[a.charAt(i) - 97]++;
			for (int i = 0; i < b.length(); i++) {
     
				s[b.charAt(i) - 97]--;
				if (s[b.charAt(i) - 97] < 0) {
     
					boo = false;
					break;
				}
			}
			if (boo) {
     
				for (int i = 0, j = 0; i < a.length(); i++) {
     
					if (a.charAt(i) == b.charAt(j)) {
     
						j++;
						x++;
					}
					if (x == b.length()) {
     
						bo = true;
						break;
					}
				}
				if (bo)
					System.out.println("automaton");
				else
					System.out.println("both");
			} else
				System.out.println("need tree");
		} else
			System.out.println("need tree");
 
	}
}

第二题,dp
Finally, a basketball court has been opened in SIS, so Demid has decided to hold a basketball exercise session.
2⋅n
2⋅n
students have come to Demid’s exercise session, and he lined up them into two rows of the same size (there are exactly
n
n
people in each row). Students are numbered from
1
1
to
n
n
in each row in order from left to right.

Now Demid wants to choose a team to play basketball. He will choose players from left to right, and the index of each chosen player (excluding the first one taken) will be strictly greater than the index of the previously chosen player. To avoid giving preference to one of the rows, Demid chooses students in such a way that no consecutive chosen students belong to the same row. The first student can be chosen among all
2n
2n
students (there are no additional constraints), and a team can consist of any number of students.
Demid thinks, that in order to compose a perfect team, he should choose students in such a way, that the total height of all chosen students is maximum possible. Help Demid to find the maximum possible total height of players in a team he can choose.

import java.util.Scanner;
public class Main {
     
	public static void main(String[] args) {
     
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		long x[] = new long[n];
		long y[] = new long[n];
		for (int i = 0; i < n; i++)
			x[i] = sc.nextInt();
		for (int i = 0; i < n; i++)
			y[i] = sc.nextInt();
		if (n == 1)
			System.out.println(Math.max(x[0], y[0]));
		else {
     
			for (int i = 1; i < n; i++) {
     
				x[i] = Math.max(y[i - 1] + x[i], x[i - 1]);
				y[i] = Math.max(x[i - 1] + y[i], y[i - 1]);
			}
			System.out.println(Math.max(x[n - 1], y[n - 1]));
		}
	}
}

你可能感兴趣的:(java)