Huawei 2021 batch of written test questions

Summary: The data structure fw was overturned...

T1

The main idea of ​​the topic: give two strings a, ba, b of length ka,b , two strings of length nc, dc, dc,d ( k < = n ) (k<=n) (k<=n ) , find a minimumlll such thatc, dc, dc,The substring of length k at the beginning of l in d is exactly the same asa, ba, ba,b matches.
n, k <= 1 e 6 n,k<=1e6n,k<=1e6

I'm coming! ! It is also a must-have in written test! ! !

(Really, there are hashes even for n games. Everyone understands it. If you learn hash well, you must pass the written test.jpg)

C++ code:

#pragma GCC optimize(2)
#include<bits/stdc++.h>
#define ull unsigned long long
#define maxn 10000005
#define inf 1e9
#define pb push_back
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define per(i,a,b) for(int i=a;i>=b;i--)
using namespace std;

inline int read()
{
    
    
	int x=0,w=1; char c=getchar();
	while(c<'0'||c>'9') {
    
    if(c=='-') w=-1; c=getchar();}
	while(c<='9'&&c>='0') {
    
    x=(x<<1)+(x<<3)+c-'0'; c=getchar();}
	return w==1?x:-x;
}

ull h1[maxn],h2[maxn],p1,p2,B[maxn];
int n,k,x;

inline ull gh(int l,int r,int opt)
{
    
    
	if(opt==1) return h1[r]-h1[l-1]*B[r-l+1];
	else return h2[r]-h2[l-1]*B[r-l+1];
}

int main()
{
    
    
	freopen("t1.in","r",stdin);
	k=read(); B[0]=1; rep(i,1,maxn-5) B[i]=B[i-1]*233;
	rep(i,1,k) x=read(),p1=p1*233+x;
	rep(i,1,k) x=read(),p2=p2*233+x;
	n=read();
	rep(i,1,n) x=read(),h1[i]=h1[i-1]*233+x;
	rep(i,1,n) x=read(),h2[i]=h2[i-1]*233+x;
	int F=0;
	rep(l,1,n)
	{
    
    
		int r=l+k-1; if(r>n) break;
		ull x1=gh(l,r,1),x2=gh(l,r,2);
		if(x1==p1&&x2==p2) {
    
    F=l; break;}
	}
	cout<<F<<endl;
	return 0;
}

T2

The main idea of ​​the topic: Given an n*m matrix, you can go to a place less than your own weight in the four directions at a time, and you are required to choose a starting point and find the longest path.
n, m <= 1000 n, m<=1000n,m<=1000

Just put the link to the original question: SHOI 2002 SHOI2002S H O I 2 0 0 2 Ski

Memory search, consider dp [i] [j] dp[i][j]dp[i][j]为在 ( i , j ) (i,j) (i,j ) The longest way to the location.

Then since each point will only be dfs once, the overall complexity is O (n ∗ m) O(n*m)O ( nm ) ...

It may not be easy to understand, but see the code for details... the code is quite easy to understand.

Java code:

import java.io.*;
import java.util.*;


public class zbr01
{
    
    
	public static int [][]a=new int [1005][1005];
	public static int [][]dp=new int [1005][1005];
	public static int [][]k={
    
    {
    
    1,0},{
    
    0,1},{
    
    -1,0},{
    
    0,-1}};
	public static int n,m,ans;
	
	public static int dfs(int x,int y)
	{
    
    
		if(dp[x][y]!=0) return dp[x][y];
		for(int i=0;i<=3;i++)
		{
    
    
			int tx=x+k[i][0],ty=y+k[i][1];
			if(tx<1||ty<1||tx>n||ty>m||a[tx][ty]>=a[x][y]) continue;
			dp[x][y]=Math.max(dp[x][y],1+dfs(tx,ty));
		}
		return dp[x][y];
	}

	
	public static void main(String[] args)
	{
    
    
		Scanner S=new Scanner(System.in);
		n=S.nextInt(); m=S.nextInt();
		for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) a[i][j]=S.nextInt();
		for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) ans=Math.max(ans,dfs(i,j));
		System.out.println(ans+1);
	}
}

T3

The main idea of ​​the topic: Given a fixed form, a point-weighted binary tree of the root node, find the maximum value of a xor path that can only go down (the xor path weight is the exclusive OR value of all nodes on the path).
n <= 1 e 5 n<=1e5n<=1e5

Mistakes countless times...

(Pre-knowledge)
First consider the question "In a sequence of length n, choose two numbers to maximize xor".
This problem can be solved with 01trie... You can go to Baidu to learn...

Then in this problem, we consider the preprocessing first, w [x] w[x]w [ x ] is the xor value of the path from x to the root node, then the xor value of a path that can only go down is converted to the max of w[x]^(w[all parent nodes]).

This part can be represented by the following pseudo code:

void dfs(int u)
{
    
    
	trie插入当前值;
	trie查询当前的xor最大值
	dfs(儿子节点);

	trie删除当前值;
}

You can complete the subject requirements.

(Due to the sudden mental retardation of the weak dish blogger... Forgot how to write a trie to delete... Write a persistent 01 trie... Finally, it was not able to debug...

upd:
Tucao: How come everyone O (n 2) O(n^2)O ( n2 ) Ihave been let go...Strongly condemned...What data? Does the author not consider the violence when writing the question...Strongly condemn it! ! ! Just make a chain and it can get stuck...this data is too unqualified hhh

Guess you like

Origin blog.csdn.net/qq_38649940/article/details/108501724