2022-02-21 swipe questions and punch in every day

Posted by phpfreakjav on Mon, 21 Feb 2022 12:44:25 +0100

1, Informatics OJ: 1281: Longest ascending subsequence

(1) Problem description

        

A number sequence bibi, when B1 < B2 << bSb1<b2<...< BS, we call this sequence rising. For a given sequence (a1,a2,...,aN)(a1,a2,...,aN), we can get some ascending subsequences (ai1,ai2,...,aiK)(ai1,ai2,...,aiK), where 1 ≤ I1 < I2 << iK≤N1≤i1<i2<...< iK≤N. For example, for the sequence (1,7,3,5,9,4,8), there are some ascending subsequences, such as (1,7), (3,4,8) and so on. The longest length of these subsequences is 4, such as subsequences (1,3,5,8).

Your task is to find the length of the longest ascending subsequence for a given sequence.

[input]

The first line of input is the length N of the sequence (1 ≤ N ≤ 1000). The second line gives N integers in the sequence, all of which have values ranging from 0 to 10000.

[output]

The length of the longest ascending subsequence.

[input example]

7
1 7 3 5 9 4 8

[output example]

4

(2) Code implementation

         

#include <stdio.h>
#define MAXN 1010
int a[MAXN],f[MAXN]; 
int max(int x,int y)
{
	return x > y ? x : y;
}
int main()
{
	int n;
	int i,j,ans=1;
	scanf("%d",&n);
	for(i=1;i<=n;i++)
	{
		scanf("%d",&a[i]);
		f[i]=1;     // Initialize f[i]
	}
	// Dynamic update f[i]
	for(i=2;i<=n;i++)
	{
		for(j=1;j<i;j++)
		{
        	if(a[i]>a[j])
				f[i]=max(f[i],f[j]+1);
		}
		ans=max(ans,f[i]);
	}
	printf("%d\n",ans);
	return 0;
}

2, Informatics OJ: 1260: [example 9.4] interceptor missile (noip 1999)

(1) Problem description

        

In order to defend against the missile attack of the enemy country, a certain country has developed a missile interception system. However, this missile interception system has a defect: although its first shell can reach any height, each shell in the future cannot be higher than the height of the previous one. One day, the radar caught the enemy's missile attack. Since the system is still in the trial stage, there is only one system, so it may not be able to intercept all missiles.

Input the altitude of the missiles in turn (the altitude data given by the radar is a positive integer of no more than 30000, and the number of missiles does not exceed 1000), and calculate the maximum number of missiles that can be intercepted by this system and the minimum number of such missile interception systems if all missiles are to be intercepted.

[input]

Enter the altitude of the missile in turn.

[output]

Line 1: the maximum number of missiles that can be intercepted;

Line 2: the minimum number of systems to intercept all missiles.

[input example]

389 207 155 300 299 170 158 65

[output example]

6
2

(2) Code implementation

        

#include <stdio.h>
#define N 1010
 
int dp1[N];           
int dp2[N];            
int f[N];            
 
int max(int x,int y)
{
	return x > y ? x : y;
}
 
int main()
{
	int idx=0,ans=0,cnt=0;
	int i,j;
	while(scanf("%d", &f[idx])!=EOF)          
		idx++;
	for(i=0;i<idx;i++)
	{
		dp1[i]=dp2[i]=1;
		for(j=0;j<i;j++)
		{
			if(f[i]<=f[j])                   
				dp1[i]=max(dp1[i],dp1[j]+1);
			else                              
				dp2[i]=max(dp2[i],dp2[j]+1);
		}
		if(ans<dp1[i])
			ans=dp1[i];
		if(cnt<dp2[i])
			cnt=dp2[i];
	}
	printf("%d\n%d\n",ans,cnt);
	return 0;
}

3, Blue Bridge Cup} test question algorithm improves the longest common subsequence

(1) Problem description

        

Problem description

Given two strings, find the longest common subsequence between the two strings.

Input format

Enter two lines, each containing a string containing only lowercase letters.

Output format

The length of the longest common subsequence.

sample input

abcdgh
aedfhb

sample output

3

Example description

The longest common subsequence is a, d, h.

Data scale and agreement

The string length is 1 ~ 1000.

(2) Code implementation

        

import java.util.*;

public class Main {
    static Scanner in=new Scanner(System.in);
    static int a,b;
    public static void main(String[] args) {
        String a=in.next();
        String b=in.next();
        int n=a.length(),m=b.length();
        int[][] f=new int[n+1][m+1];
        for(int i=1;i<=n;i++){
            char c1=a.charAt((i-1));
            for(int j=1;j<=m;j++){
                char c2=b.charAt(j-1);
                if(c1==c2) f[i][j]=f[i-1][j-1]+1;
                else f[i][j]=Math.max(f[i-1][j],f[i][j-1]);
            }
        }
        System.out.println(f[n][m]);
    }
}

Topics: Algorithm