Alibaba's written test 2020.3.27 (greedy / differential / probability)

Affirmation : Probably the title is from the discussion area of ​​Niu Ke. The input, output and data range of the title are also a bit unknown. Just look at the ideas. These details don't matter QAQ. Any mistakes are welcome to be corrected ~
3.27

Topic one (greedy)

Given the character strings s1, s2, find the minimum number of movements from s1 to s2 (requires that you can only select any character from s1 and put it at the end of s1)
Idea: Greedy. First, if the number of characters corresponding to the two strings does not match, then the answer must be -1. Otherwise, we must be able to construct a new s, making it equal to t. Because the character of s can move, the character of t cannot move. So the essence is to find the longest discontinuous substring of s so that it matches the prefix substring of t . Since it matches the prefix substring of t, then we can match it greedily. Every time a character of s matches the current character of t, the character of t moves forward one bit. The final answer is n- the maximum number of digits that can be matched.

#include<bits/stdc++.h>
using namespace std;
const int maxn = 100010;

char s[maxn],t[maxn];
int mp1[27],mp2[27];//统计各个字符数 
int n;
int main() {
	scanf("%d",&n);
	scanf("%s%s",s,t);
	int j = 0;
	for(int i = 0;i < n;i++) {
		if(s[i] == t[j]) j++;
		mp1[s[i]-'a']++;
		mp2[t[i]-'a']++;
	}
	bool flag = 1;
	for(int i = 0;i < 26;i++) {
		if(mp1[i] != mp2[i]) {
			flag = 0;break;
		}
	}
	if(flag == 0) printf("-1\n");
	else printf("%d\n",n-j);
	return 0;
} 

Topic 2 (Difference / Probability)

Refer
to the given N intervals (range 1 ~ 2000), each interval includes the left and right ranges (1 <= L <= R <= 2000), select an integer from all interval ranges at a time, and find the number of all selected numbers Expectation of the minimum.
Idea: The probability of a single value here is not easy to find, but in turn we can first find > = p o s >=pos The probability of , and then the difference can be used to find the probability of a single value.

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int maxn = 2010;

int n;
int l[maxn],r[maxn];
double p[maxn];
int main() {
	int mn = 2000,mx;//确定最小值范围 
	scanf("%d",&n);
	for(int i = 0;i < n;i++) 
		scanf("%d",&l[i]),mn = min(mn,l[i]);
	for(int i = 0;i < n;i++) 
		scanf("%d",&r[i]);
	double ans = 0;
	bool flag = 1;
	for(int pos = mn;pos <= 2000;++pos) {
		double tmp = 1.0;
		for(int i = 0;i < n;++i) {
			if(r[i] < pos) {
				flag = 0;break;
			}
			if(l[i]<=pos && pos <= r[i])
				tmp *= 1.0*(r[i]-pos+1)/(r[i]-l[i]+1);
		}
		if(!flag) {
			mx = pos-1;break;
		}
		p[pos] = tmp;//计算出最小值>=pos的概率 
	}
	for(int i = mn;i < mx;++i) {
		p[i] -= p[i+1];//差分计算最小值为i的概率 
		ans += p[i]*i; 
	}
	ans += p[mx]*mx;
	printf("%.6f\n",ans);
} 
/*
2
1 2
3 3 
*/
Published 152 original articles · won praise 2 · Views 6442

Guess you like

Origin blog.csdn.net/weixin_43918473/article/details/105278826