2021 Jingdong Autumn Recruitment C++ Development Engineer Programming Questions

Likou 1143

Given two strings text1 and text2, return the length of the longest common subsequence of these two strings.

A subsequence of a character string refers to a new character string: it is a new character string formed by deleting some characters (or no characters) from the original string without changing the relative order of the characters.
For example, "ace" is a subsequence of "abcde", but "aec" is not a subsequence of "abcde". The "common subsequence" of two strings is a subsequence shared by the two strings.

If the two strings have no common subsequence, 0 is returned.

Example 1:

Input: text1 = "abcde", text2 = "ace"
Output: 3
Explanation: The longest common subsequence is "ace", and its length is 3.

Example 2:

Input: text1 = "abc", text2 = "abc"
Output: 3
Explanation: The longest common subsequence is "abc", and its length is 3.

Example 3:

Input: text1 = "abc", text2 = "def"
Output: 0
Explanation: Two strings have no common subsequence, return 0.

Source: LeetCode (LeetCode)
Link: https://leetcode-cn.com/problems/longest-common-subsequence
Copyright is owned by LeetCode . For commercial reprints, please contact the official authorization. For non-commercial reprints, please indicate the source.
Idea:
dp[i][j] represents the number of the longest common subsequence in the first i characters of the first character and the first j characters of the second character.
When processing input, you can read each char directly into str.
Code:

class Solution {
    
    
public:
    int longestCommonSubsequence(string text1, string text2) {
    
    
        int m = text1.size();
        int n = text2.size();
        vector<vector<int>> dp(m+1,vector(n+1,0));
        for(int i = 1;i <=m;++i){
    
    
            for(int j = 1;j<=n;++j){
    
    
                if(text1[i-1] == text2[j-1]){
    
    
                    dp[i][j] = dp[i-1][j-1] + 1;
                }
                else{
    
    
                    dp[i][j] = max(dp[i-1][j],dp[i][j-1]);
                }
            }
        }
        return dp[m][n];
    }
};

Palindromic prime

There is a positive integer, and hope to get a palindrome prime after removing a certain number in this number. A palindrome prime number means that a prime number is also a palindrome number. Note: One digit is also considered to be a palindrome. Enter two positive integers N and M to satisfy N<M. Please write a program to count how many numbers between N and M satisfy the above requirements?

Idea:
Brute force method, traverse every number in the range, and remove every number in them. Then determine whether it is a palindrome prime. The cumulative number.
Code:

#include <vector>
#include <iostream>
#include <string>
#include <stdlib.h>
#include <cmath>
using namespace std;
class Solution{
    
    
public:
    int solution(int low, int high){
    
    
        int ans = 0;
        for(int i=low; i<=high; ++i){
    
    
            if(helper(i)){
    
    
                ++ ans;
            }
        }
        return ans;
    }
    bool helper(int num){
    
    
        string str = to_string(num);//整数转字符串
        for (int i = 0; i < str.length(); ++i) {
    
    
            string temp = str;
            temp.erase(i,1);
            if (isHuiwen(atoi(temp.c_str())) && isSushu(atoi(temp.c_str()))) {
    
    //字符串转整数
                return true;
            }
        }
        return false;
    }
    bool isSushu(int num){
    
    
        if(num<=1) return false;

        int sqt = sqrt(num);
        for(int i=2; i<=sqt; ++i){
    
    
            if(num % i == 0)
                return false;
        }
        return true;
    }
    //dp 判断回文串
    bool isHuiwen(int num){
    
    
        string s = to_string(num);
        int n = s.size();
        vector<vector<bool>> Mat(n,vector<bool>(n,false));
        for(int i = n-1;i>=0;--i){
    
    
            for(int j = i;j<n;++j){
    
    
                if(j ==i){
    
    //从尾部开始,一个字符的时候,一定是回文串
                    Mat[i][j] =true;
                }
                else if(j == i+1){
    
    //从尾部判断,两个字符的时候,取决于两个字符是否相等?相等的话就是回文串,不相等的话就不是。
                    Mat[i][j] = s[i] == s[j];
                }
                else
                    Mat[i][j] = (s[i] == s[j]) && (Mat[i+1][j-1]);//i和j位置上的字符相等 && [i+1,j-1]是回文串的话,[i,j]就是回文串
            }
        }
        return Mat[0][n-1];
    }
};

int main(){
    
    
    Solution sol;
    int low, high;
    while (cin>>low>>high) {
    
    
        cout << sol.solution(low, high) << endl;
    }
}

How to enter a fixed number of decimal places in C++

#include <iomanip>
#include <iostream>
using namespace std;
int main(){
    
    
  double a = 12.334;
  cout<<fixed<<setprecision(1)<<a<<endl;
}

Guess you like

Origin blog.csdn.net/qq_35353824/article/details/107855315
Recommended