日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

天池在线编程 2020年9月26日 日常周赛题解

發(fā)布時間:2024/7/5 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 天池在线编程 2020年9月26日 日常周赛题解 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

文章目錄

    • 1. K步編輯
    • 2. 折紙
    • 3. 字符串的不同排列
    • 4. 硬幣排成線

題目地址,請點這

1. K步編輯

給出一個只含有小寫字母的字符串的集合以及一個目標串(target),輸出所有可以經(jīng)過不多于 k 次操作得到目標字符串的字符串。

你可以對字符串進行一下的3種操作:

  • 加入1個字母
  • 刪除1個字母
  • 替換1個字母

考查動態(tài)規(guī)劃:最短編輯距離

class Solution { public:/*** @param words: a set of stirngs* @param target: a target string* @param k: An integer* @return: output all the strings that meet the requirements*/vector<string> kDistance(vector<string> &words, string &target, int k) {// write your code herevector<string> ans;for(auto& w : words){if(minDistance(w, target) <= k)ans.push_back(w);}return ans;}int minDistance(string word1, string word2) {int n1 = word1.size(), n2 = word2.size(), i, j;if(n1==0 || n2==0) return max(n1,n2);int dp[n1+1][n2+1];for(i = 0; i < n1+1; i++)dp[i][0] = i;for(j = 0; j < n2+1; j++)dp[0][j] = j;// DPint left, up, left_up;for(i = 1; i < n1+1; i++) {for(j = 1; j < n2+1; j++) {left = dp[i-1][j];up = dp[i][j-1];left_up = dp[i-1][j-1];if(word1[i-1] != word2[j-1]) dp[i][j] = 1 + min(left, min(up, left_up));else// word1[i-1] == word2[j-1]dp[i][j] = left_up;}}return dp[n1][n2];} };

2. 折紙

折紙,每次都是將紙從右向左對折,凹痕為 0,凸痕為 1,求折 n 次后,將紙展開所得折痕組成的 01 序列。
1<=n<=201<=n<=201<=n<=20

  • 找規(guī)律,拿張紙,折幾次就會發(fā)現(xiàn)規(guī)律了。下一個序列是在前一個序列的基礎(chǔ)上插空加入010101...
class Solution { public:/*** @param n: The folding times* @return: the 01 string*/string getString(int n) {// Write your code hereif(n==1) return "0";string ans, temp = "0";while(--n){int flag = 0;for(int i = 0; i < temp.size(); i++){ans += string(1, flag+'0')+temp[i];flag = (flag==0 ? 1 : 0);}ans += "1";temp = ans;ans = "";}return temp;} };

3. 字符串的不同排列

給出一個字符串,找到它的所有排列,注意同一個字符串不要打印兩次。 請以字典序從小到大輸出。 0<=n<=20

排列組合,回溯+剪枝

class Solution { public:/*** @param str: A string* @return: all permutations*/vector<string> ans;vector<string> stringPermutation(string &str) {// write your code heresort(str.begin(), str.end());vector<bool> vis(str.size(), false);string s;dfs(str, s, 0, vis);return ans;}void dfs(string &str, string &s, int idx, vector<bool> &vis){if(idx == str.size()){ans.push_back(s);return;}for(int i = 0; i < str.size(); i++){if(vis[i])continue;if(i >= 1 && !vis[i-1] && str[i-1] == str[i])continue;//剪枝,去重s += str[i];vis[i] = true;dfs(str, s, idx+1, vis);vis[i] = false;s.pop_back();}} };

4. 硬幣排成線

有 n 個硬幣排成一條線, 第 i 枚硬幣的價值為 values[i].

兩個參賽者輪流從任意一邊取一枚硬幣, 直到?jīng)]有硬幣為止. 拿到硬幣總價值更高的獲勝.

請判定 第一個玩家 會贏還是會輸. 1<=n<=20001<=n<=20001<=n<=2000

  • 博弈 動態(tài)規(guī)劃,dp[i][j] 表示剩余硬幣為 [i,j] 時,我跟對手的最大差值
class Solution { public:/*** @param values: a vector of integers* @return: a boolean which equals to true if the first player will win*/bool firstWillWin(vector<int> &values) {// write your code hereint n = values.size(), i, j;vector<vector<int>> dp(n, vector<int>(n, INT_MIN));for(i = 0; i < n; ++i)dp[i][i] = values[i];for(int len = 1; len < n; ++len){for(i = 0; i+len < n; ++i){dp[i][i+len] = max(values[i]-dp[i+1][i+len], values[i+len]-dp[i][i+len-1]);}}return dp[0][n-1] > 0;} };

我的CSDN博客地址 https://michael.blog.csdn.net/

長按或掃碼關(guān)注我的公眾號(Michael阿明),一起加油、一起學(xué)習(xí)進步!

總結(jié)

以上是生活随笔為你收集整理的天池在线编程 2020年9月26日 日常周赛题解的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。