2020 Alibaba practice Gang pen questions

Title Description

Here Insert Picture Description
Test Case:

cbadg
cbgda

Output:
2

public boolean check(String S,String T){
      int len = S.length();
      int[] alphet = new int[26];
      for(int i =0;i<len;i++){
        alphet[S.charAt(i) - 'a'] ++;
      }
      for(int i =0;i<len;i++){
        alphet[T.charAt(i) - 'a'] --;
        if( alphet[T.charAt(i) - 'a'] < 0){
          return false;
        }
      }
      return true;
    }

    public int ToMove(String S,String T){
        if(!check(S,T)){
          return -1;
        }
        int j =0;
        for(int i=0;i<S.length();i++){
          if(S.charAt(i) == T.charAt(j)){
            j++;
          }
        }
        return S.length() - j;
    }

Title Description

N random numbers in seeking the minimum expected value.

Enter the three lines, namely:

The number of random numbers: n

l1, l2, ... ln: li is the minimum value of the i-th random number

r1, r2, ... rn: ri is the i-th maximum value of the random number

That value interval of the i-th random number is [li, ri], probability is equal to the number in the interval

Example Input:
2
. 1 2
. 3. 3

Output:
1.83333

public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    int n = in.nextInt();
    int[] l = new int[n];
    int[] r = new int[n];
    int min1 = Integer.MAX_VALUE;
    int min2 = Integer.MAX_VALUE;
    for (int i = 0; i < n; i++) {
      l[i] = in.nextInt();
      min1 = min1 > l[i] ? l[i]:min1;
    }

    for (int i = 0; i < n; i++) {
      r[i] = in.nextInt();
      min2 = min2 > r[i] ? r[i]:min2;
    }

    //一致性:min2-min1>0
    double[] p = new double[min2-min1+2];
    for(int i = min1;i <= min2; i++){
      p[i - min1] = 1;
      for(int j = 0;j<n;j++){
        if(l[j]<= i && i <= r[j]){
          p[i-min1] *= (r[j]-i + 1.0)/(r[j]-l[j] + 1.0);
        }
      }
    }
    double ans = 0;
    for(int i = 0;i <= min2-min1;i++){
      ans += (i + min1) * (p[i]-p[i+1]);
    }
    System.out.printf("%.6f",ans);
  }
Published 121 original articles · won praise 1333 · Views 230,000 +

Guess you like

Origin blog.csdn.net/JAck_chen0309/article/details/105136315