2018 Worship Algorithm Engineer Pen Questions

Source: Internet
Author: User
Tags array sort first string

First, the string deformation

Enter the length of two strings A and b,a less than B. You can now insert any character at each position in a, so that the length of the final A is equal to the length of B, and the number of locations where the corresponding position characters are in the last A and B.

The problem can be seen as: the longest common subsequence with constraints
Consider two strings that are shaped like abcx and AYYBC, and the first string cannot add too many characters, or it will cause it to be too long. When A[i]=b[j], I must not be greater than J, otherwise a[i] is not likely to be equal to J.

The test sample is so simple that many error solutions can be passed all the way.

  import Java.util.Scanner; public class Main {main () {Scanner cin = new Scanner (system.in);    Char[] A = Cin.next (). Trim (). ToCharArray (), B = cin.next (). Trim (). ToCharArray ();    Int[][] m = new Int[a.length][b.length]; for (int i = 0, i < a.length; i++) {for (int j = 0; J < B.length; J + +) {if (i > 0) m[i][j] =            Math.max (M[i-1][j], m[i][j]);            if (J > 0) m[i][j] = Math.max (M[i][j-1], m[i][j]);                if (a[i] = = B[j] && a.length-i <= b.length-j && i <= j) {int last = 0;                if (i > 0 && J > 0) last = m[i-1][j-1];            M[I][J] = Math.max (M[i][j], last + 1);    }}} int common = m[a.length-1][b.length-1];    int add = b.length-a.length;//System.out.println (common);    int ans = b.length-common-add; System.out.println (ans);} public static void Main (string[] args) {new Main ()}}  
Second, probability + dynamic programming

n Individuals, M gifts, the number of each gift is c[i], the first person to choose the probability of the first J Gift P[i][j], when the gift, n a one-time to determine what they want to be a gift, and then in order once issued, if a type of gift has not, will not give the gift of the person sent. Because the number of gifts is limited, ask how many people expect to get gifts.

The key is to understand the problem transformation: The number of people who expect to receive the gift = The task that each gift expects to send. Because people and gifts correspond to each other.
The definition array f[m][n],f[i][j] denotes the probability that the number of times a gift is requested is J, and that the final matrix can be obtained from the 0~n increment derivation, which is equivalent to the dynamic programming in the form of a rolling array.

Import Java.util.Scanner; public class Main {int N, m;int[] c;double p[][];d ouble left[][];    Main () {Scanner cin = new Scanner (system.in);    N = Cin.nextint ();    M = Cin.nextint ();    c = new Int[m];    p = new Double[n][m];        for (int i = 0; i < M; i++) {C[i] = Cin.nextint ();    if (C[i] > N) c[i] = n;    } for (int i = 0, i < N; i++) for (int j = 0; J < M; J + +) P[i][j] = cin.nextdouble ();    left = new Double[m][n + 1];    for (int i = 0; i < left.length; i++) left[i][0] = 1;                for (int i = 0, i < N; i++) {for (int j = 0; J < M; J + +) {for (int k = N; k >= 0; k--) {                LEFT[J][K] = left[j][k] * (1-p[i][j]);                if (k > 0) {left[j][k] + = left[j][k-1] * P[i][j];    }}}} double s = 0;        for (int i = 0; i < M; i++) {for (int j = 0; J <= N; j + +) {s + = left[i][j] * math.min (C[i], J); }} System.out.printf ("%.1f", s);} public static void Main (string[] args) {new Main ();}}
Three, array sort: greedy

An unordered array that can only perform one operation at a time: Move any element to the end. The minimum number of operations that can make an array more orderly.

For element x, as long as there is a smaller element behind it, it is bound to be moved to the last. And for the need to move to the last batch of elements, it is necessary to move the relatively small number of those elements, so as to ensure that they as far as possible forward.

Import Java.io.ioexception;import java.util.arrays;import Java.util.comparator;import Java.util.LinkedList;import Java.util.scanner;import java.util.stream.Collectors;    public class Main {main () {Scanner cin = new Scanner (system.in);    int N = Cin.nextint ();    Int[] A = new int[n];    for (int i = 0; i < N; i++) A[i] = Cin.nextint ();    int s = 0;        while (true) {linkedlist<integer> left = new linkedlist<> ();        linkedlist<integer> right = new linkedlist<> ();        int mi = integer.max_value;            for (int i = a.length-1; I >= 0; i--) {if (Mi < a[i]) {Right.add (a[i]);            } else {Left.add (a[i]);        } mi = math.min (mi, a[i]);        } right.sort (Comparator.comparing (x, x));        s + = Right.size ();        if (right.size () = = 0) break;        int ai = 0;        for (int i:left) a[n-1-Right.size ()-(ai++)] = i; for (int i:right) a[ai++] = i; } System.out.println (s);} public static void Main (string[] args) {new Main ();}}

2018 Prayer Algorithm Engineer pen question

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.