20190908 iQiyi, BIGO, ByteDance

IQIYI

1.

Leetcode original title

#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<vector>
#include<sstream>
#include<cmath>
#include<climits>
#include<queue>
#include<cstring>
using namespace std;
#define mod 1000000007
int n;
int dp[1001][1001];
bool flag[1001];
int main(){
    scanf("%d",&n);
    int s;
    for(int i=0;i<n-1;i++){
        scanf("%d",&s);
        if(s){
            flag[i]=true;
        }
        else
        flag[i]=false;
    }
    for(int j=0;j<n;j++){
        dp[0][j]=1;
    }
    for(int i=0;i<n-1;i++){
        if(flag[i]){
            for(int j=0,cur=0;j<n-i-1;j++){
                dp[i+1][j]=cur=(cur+dp[i][j])%mod;
            }
        }
        else{
            for(int j=n-i-2,cur=0;j>=0;j--){
                dp[i+1][j]=cur=(cur+dp[i][j+1])%mod;
            }
        }
    }
    /*for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            printf("%d ",dp[i][j]);
        }
        printf("\n");
    }*/
    printf("%d\n",dp[n-1][0]);
    //system("PAUSE");
    return 0;
}


2.

There are n red balls, m blue balls, and three people from ABC. Start with A and get the ball. AB gets the red ball. The game ends. C is messing up. You can get the red ball and the blue ball, but it doesn’t count if he gets the red ball. Win, if he takes all the red balls, count B wins and calculate the probability of A winning.

Timed out. . .

#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<vector>
#include<sstream>
#include<cmath>
#include<climits>
#include<queue>
#include<cstring>
using namespace std;
int t;
int n,m;
double dfs(int abc,int r,int b){
    if(r<=0)
    return 0.0;
    if(abc==0){
        if(b==0)
        return (double)r/(r+b);
        return (double)r/(r+b)+(double)b/(r+b)*dfs(1,r,b-1);
    }
    else{
        if(abc==1){
            if(b==0)
            return 0.0;
            return (double)b/(r+b)*dfs(2,r,b-1);
        }
        else{
            if(b==0)
            return (double)r/(r+b)*dfs(0,r-1,b);
            return (double)r/(r+b)*dfs(0,r-1,b)+(double)b/(r+b)*dfs(0,r,b-1);
        }
    }
}
int main(){
    scanf("%d %d",&n,&m);
    //int sum=n+m;
    double ans=1.0;
    ans=dfs(0,n,m);
    printf("%.5lf\n",ans);
    //system("PAUSE");
    return 0;
}

 May need to use array temporary storage optimization

#include <iostream>
#include <vector>
#include <set>
#include <queue>
#include <map>
#include <string.h>
#include <math.h>
#include <stack>
using namespace std;
double f[1005][1005][3];
double dfs(int x, int y, int t) {
    if (f[x][y][t] != -1) return f[x][y][t];
    double ans = 0;
    if (x > 0) {
        if (t == 0) ans += (double)x / (double)(x+y);
        if (t == 2) {
            ans += (double)x / (double)(x+y) * dfs(x-1, y, (t+1)%3);
        }
    }

    if (y > 0) {
        ans += (double)y / (double)(x+y) * dfs(x, y-1, (t+1)%3);
    }
    //f[x][y][t] = ans;
    //printf("OOOO %d %d %d %f\n", x, y, t, f[x][y][t]);
    return f[x][y][t] = ans;
}

void work() {

    int n, m;
    scanf("%d%d", &n, &m);
    memset(f, 0, sizeof f);
    for (int i = 0; i <= n+1; i++)
        for (int j = 0; j <= m+1; j++)
            for (int k = 0; k < 3; k++)
                f[i][j][k] = -1;
    double ans = 0;
    ans = dfs(n, m, 0);
    printf("%.5f\n", ans);
}

int main() {
    work();
    return 0;
}

BIGO

1.

Take the stone problem (game theory)

2.

aligned_malloc

3.

Climb n stairs, you can climb 1, 2, 3 steps at a time, find the total number of methods

#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<vector>
#include<sstream>
using namespace std;
int n;
int dfs(int left){
    if(left==0)return 1;
    if(left<0) return 0;
    return dfs(left-1)+dfs(left-2)+dfs(left-3);
}
int climb_base(int n){
    int ans=dfs(n);
    return ans;
}
int main(){
    scanf("%d",&n);
    int ans;
    ans=climb_base(n);
    cout<<ans<<endl;
    //system("PAUSE");
    return 0;
}

4.

Find the maximum sum of two disjoint continuous subarrays

Headline

1.

n = int(input().strip())
a = list(map(int, input().strip().split()))

res = [-1, 0]
i = n-1
stack = [[a[i], i]]
i -= 1
while stack and i >= 0:
	ans = 0
	while stack and a[i] >= stack[-1][0]:
		stack.pop(-1)
		ans += 1
	if ans >= res[1]:
		res = [a[i], ans]
	stack.append([a[i], i])
	i -= 1
print(res[0])

 2. Pour water

 


import java.util.*;

public class Main {
    public static class Water{
        public int l1=0;
        public int l2=0;
        public int l3=0;
        public Water(Water w){
            l1=w.l1;l2=w.l2;l3=w.l3;
        }
        public Water(){
            l1=0;l2=0;l3=0;
        }
        @Override
        public int hashCode(){
            String str=String.valueOf(l1)+String.valueOf(l2)+String.valueOf(l3);
            return  Integer.valueOf(str);
        }
        @Override
        public boolean equals(Object  obj){
            if(obj instanceof Water) {
                Water w = (Water) obj;
                return (w.l1 == l1 && w.l2 == l2 && w.l3 == l3);
            }
            return  false;
        }
    }
    public static void bfs(int kettle[],Water water,HashSet<Water> set,List<Water> queue){

        Water w=new Water(water);
        w.l1=kettle[0];
        if(!set.contains(w)){
            set.add(w);
            queue.add(w);
        }
        w=new Water(water);
        w.l2=kettle[1];
        if(!set.contains(w)){
            set.add(w);
            queue.add(w);
        }
        w=new Water(water);
        w.l3=kettle[2];
        if(!set.contains(w)){
            set.add(w);
            queue.add(w);
        }
        w=new Water(water);
        w.l1=0;
        if(!set.contains(w)){
            set.add(w);
            queue.add(w);
        }
        w=new Water(water);
        w.l2=0;
        if(!set.contains(w)){
            set.add(w);
            queue.add(w);
        }
        w=new Water(water);
        w.l3=0;
        if(!set.contains(w)){
            set.add(w);
            queue.add(w);
        }
        w=new Water(water);
        w.l1=(water.l1+water.l2-kettle[1])<0?0:(water.l1+water.l2-kettle[1]);
        w.l2=(water.l1+water.l2>kettle[1])?kettle[1]:(water.l1+water.l2);
        if(!set.contains(w)){
            set.add(w);
            queue.add(w);
        }

        w=new Water(water);
        w.l2=(water.l1+water.l2-kettle[0])<0?0:(water.l1+water.l2-kettle[0]);
        w.l1=(water.l1+water.l2>kettle[0])?kettle[0]:(water.l1+water.l2);
        if(!set.contains(w)){
            set.add(w);
            queue.add(w);
        }

        w=new Water(water);
        w.l1=(water.l1+water.l3-kettle[2])<0?0:(water.l1+water.l3-kettle[2]);
        w.l3=(water.l1+water.l3>kettle[2])?kettle[2]:(water.l1+water.l3);
        if(!set.contains(w)){
            set.add(w);
            queue.add(w);
        }

        w=new Water(water);
        w.l3=(water.l1+water.l3-kettle[0])<0?0:(water.l1+water.l3-kettle[0]);
        w.l1=(water.l1+water.l3>kettle[0])?kettle[0]:(water.l1+water.l3);
        if(!set.contains(w)){
            set.add(w);
            queue.add(w);
        }

        w=new Water(water);
        w.l2=(water.l2+water.l3-kettle[2])<0?0:(water.l2+water.l3-kettle[2]);
        w.l3=(water.l2+water.l3>kettle[2])?kettle[2]:(water.l2+water.l3);
        if(!set.contains(w)){
            set.add(w);
            queue.add(w);
        }

        w=new Water(water);
        w.l3=(water.l2+water.l3-kettle[1])<0?0:(water.l2+water.l3-kettle[1]);
        w.l2=(water.l2+water.l3>kettle[1])?kettle[1]:(water.l2+water.l3);
        if(!set.contains(w)){
            set.add(w);
            queue.add(w);
        }
    }
    public static int func(int kettle[],int target){
        Water water=new Water();
        HashSet<Water> set=new HashSet<>();
        List<Water> queue=new ArrayList<>();
        queue.add(water);
        set.add(water);
        int deep=0;
        while(!queue.isEmpty()){
            int size=queue.size();
            for(int i=0;i<size;i++){
                Water tmp=queue.remove(0);
                if(tmp.l1==target||tmp.l2==target||tmp.l3==target){
                    return deep;
                }
                bfs(kettle, tmp,set,queue);
                int a=1;
            }

            deep++;
        }
        return -1;
    }
    public static void main(String[] args) {
	// write your code here
        Scanner sc=new Scanner(System.in);
        int kettle[]=new int[3];
        kettle[0]=sc.nextInt();
        kettle[1]=sc.nextInt();
        kettle[2]=sc.nextInt();
        int target=sc.nextInt();
        int res=func(kettle,target);
        System.out.println(res);
        //3 5 8 4
    }
}

3.

 

 

 

4.

 

ch='0ABCDEFGHIJKLMNOPQRSTUVWXYZ'
s = str(input().strip())
n = len(s)
res=''
def dfs(i,l,res):
	if i==l :
		print(res)
	if l-i>=1:
		a=int(s[i])
		if a==0:
			return
		dfs(i+1,l,res+ch[a])
	if l-i >= 2 :
		a=int(s[i:i+2])
		if a<10 or a>=27:
			return
		dfs(i+2,l,res+ch[a])

dfs(0,n,res)

 

Guess you like

Origin blog.csdn.net/LXQ1071717521/article/details/100633742