2021 Autumn Recruitment NetEase Written Test

Likou 214 shortest palindrome

Given a string s, you can convert it to a palindrome by adding characters in front of the string. Find and return the shortest palindrome that can be converted in this way.

Example 1:

Input: "aacecaaa"
Output: "aaacecaaa"

Example 2:

Input: "abcd"
Output: "dcbabcd"

Source: LeetCode
Link: https://leetcode-cn.com/problems/shortest-palindrome The
copyright is owned by LeetCode . For commercial reprints, please contact the official authorization. For non-commercial reprints, please indicate the source.
Idea:
divide the characters to be processed into palindrome strings and non-palindromic strings, and then add the non-palindromic strings to the head in reverse. It should be noted that the case where the boundary of the longest palindrome has been moved, this case needs to be processed recursively.
Code:

class Solution {
    
    
public:
    string shortestPalindrome(string s) {
    
    
    int n = s.size();
    int i = 0;
    for (int j = n - 1; j >= 0; j--) {
    
    
        if (s[i] == s[j])
            i++;
    }
    if (i == n)
        return s;
    string remain_rev = s.substr(i, n);
    reverse(remain_rev.begin(), remain_rev.end());
    //为什么要递归,因为i可能已经移动出了最长字符串的范围
    return remain_rev + shortestPalindrome(s.substr(0, i)) + s.substr(i);//ababbcefbbaba
    }
};

This question is similar to the first C++ written test of NetEase, except that one is inserted in the front and the other is inserted in the back.

Give a bunch (up to 15) of valuable things and divide them equally among 2 people. Throw them away if they can’t be divided equally, and ask for the smallest amount

Idea:
Refer to the code of the boss and solve it with violence. I feel that my understanding of DFS is not deep enough.
Code:

#include <bits/stdc++.h>
using namespace std;
int a[20];
int suffixSum[20];
int n;
int minLoss;
void dfs(int first, int second, int cost, int index)
{
    
    
    if (index >= n)
    {
    
    
        if (first == second)
            minLoss = min(minLoss, cost);
        return;
    }
    if (first == second)
        minLoss = min(minLoss, suffixSum[index] + cost);
    if (abs(first - second) > suffixSum[index])
        return;
    // 给first,给second,扔掉
    dfs(first + a[index], second, cost, index + 1);
    dfs(first, second + a[index], cost, index + 1);
    dfs(first, second, cost + a[index], index + 1);
}
int main(int argc, char const *argv[])
{
    
    
    int t;
    cin >> t;
    while (t--)
    {
    
    
        scanf("%d", &n);
        for (int i = 0; i < n; ++i)
            scanf("%d", &a[i]);
        memset(suffixSum, 0, sizeof(suffixSum));
        for (int i = n - 1; i >= 0; --i)
            suffixSum[i] = suffixSum[i + 1] + a[i];
        minLoss = INT32_MAX;
        dfs(0, 0, 0, 0);
        cout << minLoss << endl;
    }
    return 0;
}

DP solution:

#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
typedef long long ll;
typedef pair<ll,ll> P;
const int inf=0xffffffff;

int dp[20][1500005];

int main(){
    
    
    int T;scanf("%d",&T);
    while(T--){
    
    
        memset(dp,-0x3f,sizeof(dp));
        int n;scanf("%d",&n);
        vector<int>a(n+1);
        int sum=0;
        for(int i=1;i<=n;i++){
    
    scanf("%d",&a[i]);sum+=a[i];}
        dp[0][0]=0;
        for(int i=1;i<=n;i++){
    
    
            for(int j=0;j<=sum;j++){
    
    
                dp[i][j]=dp[i-1][j];
                dp[i][j]=max(dp[i][j],max(dp[i-1][j+a[i]]+a[i],dp[i-1][abs(j-a[i])]+a[i]));
            }
        }
        printf("%d\n",sum-dp[n][0]);
    }
    return 0;
}

n (not more than 2000) Individuals line up to buy tickets, the i-th person either buys the ticket alone, or buys it with the next person (conversely, it can be bought with the previous person), it takes time to buy it alone a[i], buy it together It is b[i], ask for the shortest time, the conductor can go home. The start time is 08:00:00 am, and the end time is output.

Idea:
Refer to other people's code
code:

#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
using namespace std;

int getMinCost(vector<int>a, vector<int>b) {
    
    
	vector<int>dp(a.size() + 1);
	dp[1] = a[0];
	for (int i = 2; i < dp.size(); ++i) {
    
    
		dp[i] = min(dp[i - 1] + a[i - 1], dp[i - 2] + b[i - 2]);
	}
	return dp.back();
}

string getTime(int cost) {
    
    
	int h = 8, m = 0, s = 0;
	s = cost % 60;
	cost /= 60;
	m += cost % 60;
	cost /= 60;
	h += cost % 24;
	bool flag = false;
	if (h > 12) {
    
    
		h -= 12;
		flag = true;
	}
	string sh = to_string(h);
	if (sh.size() < 2) sh = "0" + sh;
	string sm = to_string(m);
	if (sm.size() < 2) sm = "0" + sm;
	string ss = to_string(s);
	if (ss.size() < 2) ss = "0" + ss;
	return sh + ":" + sm + ":" + ss + " " + (flag ? "pm" : "am");

}

int main() {
    
    
	int m;
	cin >> m;
	while (m-- > 0) {
    
    
		int n;
		cin >> n;
		vector<int>a(n);
		vector<int>b(n);
		for (int i = 0; i < n; ++i) cin >> a[i];
		for (int i = 0; i < n - 1; ++i) cin >> b[i];
		int cost = getMinCost(a, b);
		cout << getTime(cost) << endl;
	}
	
}

PS: DP really needs a lot of practice

Guess you like

Origin blog.csdn.net/qq_35353824/article/details/107891671