2022-03-10每日刷题打卡
2022-03-10每日刷題打卡
力扣——每日一題
589. N 叉樹的前序遍歷
給定一個 n 叉樹的根節點 root ,返回 其節點值的 前序遍歷 。
n 叉樹 在輸入中按層序遍歷進行序列化表示,每組子節點由空值 null 分隔(請參見示例)。
示例 1:
輸入:root = [1,null,3,2,4,null,5,6]
輸出:[1,3,5,6,2,4]
前序遍歷順序:根節點——>左孩子——>右孩子。
只不過這里不是一般的二叉樹,是n叉樹,它的孩子節點不是left和right,而是存在了數組children里。但前序遍歷就是先遍歷根節點,再從左往右遍歷孩子節點,所以做法和一般二叉樹是一樣的,這里用的是dfs解法,先把根節點送去dfs,我們每次存完根結點的值后,從下標0開始遍歷根節點的children數組,每次遍歷一個點就把它送去下一次dfs。可以準備一個全局數組來存遍歷過的節點的val值。
/* // Definition for a Node. class Node { public:int val;vector<Node*> children;Node() {}Node(int _val) {val = _val;}Node(int _val, vector<Node*> _children) {val = _val;children = _children;} }; */class Solution { public:vector<int>v;void dfs(Node* root){if(!root)return;v.push_back(root->val);int n=root->children.size();for(int i=0;i<n;i++){dfs(root->children[i]);}}vector<int> preorder(Node* root) {dfs(root);return v;} };76. 最小覆蓋子串
給你一個字符串 s 、一個字符串 t 。返回 s 中涵蓋 t 所有字符的最小子串。如果 s 中不存在涵蓋 t 所有字符的子串,則返回空字符串 “” 。
注意:
對于 t 中重復字符,我們尋找的子字符串中該字符數量必須不少于 t 中該字符數量。
如果 s 中存在這樣的子串,我們保證它是唯一的答案。
示例 1:
輸入:s = “ADOBECODEBANC”, t = “ABC”
輸出:“BANC”
哈希表+滑動窗口。用一個哈希表記錄下t的組成情況,然后在遍歷s尋找目標字符串的時候用另一個哈希表記錄我們找到的字符串的組成情況,起初滑動窗口左端不動右端一直向右延申,當滑動窗口內部的字符串包含了t的時候,滑動窗口的左端開始向右移動收縮窗口,當左端口的元素是t的組成字符,且將此字符排除滑動窗口將使得滑動窗口內部不能完全包含t的時候停下來,直到下次右端點遍歷到的字符等于該組成字符時,左端點再繼續移動。在此過程中記錄最短的字符串,并在遍歷結束后輸出。
class Solution { public:string minWindow(string s, string t) {string str;int l = 0, n = s.size(), ans = 0, m = t.size(), min_len = 1e9, sub = -1;if (n < m)return str;map<char, int>mymap, res;for (auto i : t)mymap[i]++;for (int i = 0; i < n; i++){if (mymap[s[i]] != 0 && res[s[i]] < mymap[s[i]])ans++;if (mymap[s[i]] != 0)res[s[i]]++;while ( l<n&&mymap[s[l]] == 0||res[s[l]] > mymap[s[l]] ){if (res[s[l]] > 0)res[s[l]]--;l++;}if (m == ans){if (i - l < min_len){min_len = i - l;sub = l;}}}if (min_len == 1e9)return str;for (int i = 0; i <= min_len; i++){str += s[sub + i];}return str;} };代碼源——div2每日一題
異或和或 - 題目 - Daimayuan Online Judge
對于一個長度為 n 的0101序列 a1,a2,…,an。
你可以執行以下操作任意多次:
- 選擇兩個下標 1≤i,j≤n(i≠j)。
- 記x=ai xor aj , y=ai or aj , 其中 xor 表示按位異或 , or 表示按位或。
- 然后令 ai=x,aj=y 或 ai=y,aj=x。
給定兩個01序列 s,t , 請你判斷是否可以通過有限次(可以為0次)操作將序列 s 變為 t。
輸入格式
第一行一個整數 t , 表示數據的組數(1≤t≤103)。接下來 t 組數據:
每組第一行一個01字符串 s(1≤|s|≤10^3),每組第二行一個01字符串 t(1≤|t|≤103)。
注意:|s|可能不等于 |t|。
輸出格式
如果可以通過有限次(可以為0次)操作將序列 s 變為 t , 輸出 YES , 否則輸出 NO。
樣例輸入
2 001 011 11 101樣例輸出
YES NO樣例解釋
第一組數據選擇 i=2,j=3 , 那么 x=1,y=1 , 接著令 ai=x,aj=y 即可得到 t 序列。
第二組數據 |s|=2,|t|=3 顯然無法滿足要求。
這題寫之前我們先打個草稿來看一下異或和或對于0和1時的各種不同情況,
當兩邊都是1時:1^1=0,1|1=1。
當兩邊都是0時:0^0=0,0|0=0。
當其中一邊是1時:1^0=1,1|0=1 (反過來也是一樣0^1=1,0|1=1)。
通過這幾種情況我們不難看出,1是有辦法變成0的:11=0,也就是說哪怕我s全是1組成,也可以生成0出來,但是,并不能把所有的1都變成0,畢竟只靠1變成0的方法是:11=0,1|1=1,這樣會使得一個1變成0,而另一個依然是1,而只靠1個1是無法變成0的,所以不管怎么樣,1是無法被消除干凈的,最少也會留下一個1。 0并不能只靠自己變成1:0^0=0,0|0=0,所以說,如果s全是0,那就無法發生任何變化了,畢竟只有0,怎么變都是0。至于一個0一個1的情況,就是可以把一個0變成1,另一個1也是變成1(沒變化)。
現在就可以得出結論來了,如果s和t字符串的組成情況,其中一個是沒有1,另一個是有1的情況下,兩者是不可能變成一樣的,畢竟沒有1,只靠0是無法變成1的,而如果你有1,那你也無法消除完所有的1變成全是0。至于其他的情況,我們則總是有辦法在有限的次數中完成s=t的轉化的。還有一點就是,我們一開始就可以先比較他們兩個的長度,如果長度不一樣那就直接輸出NO即可。
#include<iostream> using namespace std; #include<vector> #include<algorithm> #include<math.h> #include<set> #include<numeric> #include<string> #include<map> #include<unordered_map> #include<stack> #include<queue>typedef long long ll; typedef pair<int, int>PII; const int MOD = 1e9 + 7; const int N = 100100;int main() {ios_base::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);int t;cin >> t;while (t--){string s1, s2;cin >> s1 >> s2;int n = s1.size(), m = s2.size();if (n != m){cout << "NO" << endl;continue;}int s1_zero = 0, s1_one = 0, s2_zero = 0, s2_one = 0;for (int i = 0; i < n; i++){if (s1[i] == '0')s1_zero++;else s1_one++;if (s2[i] == '0')s2_zero++;else s2_one++;}if (s2_one == 0 && s1_one != 0)cout << "NO" << endl;else if (s2_one != 0 && s1_one == 0)cout << "NO" << endl;else cout << "YES" << endl;}return 0; }CodeForces
Problem - E - Codeforces
Now Dmitry has a session, and he has to pass n exams. The session starts on day 11 and lasts d days. The iith exam will take place on the day of ai (1≤ai≤d), all ai — are different.
Sample, where n=3, d=12, a=[3,5,9]. Orange — exam days. Before the first exam Dmitry will rest 22 days, before the second he will rest 11 day and before the third he will rest 33 days.
For the session schedule, Dmitry considers a special value μμ — the smallest of the rest times before the exam for all exams. For example, for the image above, μ=1μ=1. In other words, for the schedule, he counts exactly n numbers — how many days he rests between the exam i?1 and ii (for i=0 between the start of the session and the exam ii). Then it finds μ — the minimum among these n numbers.
Dmitry believes that he can improve the schedule of the session. He may ask to change the date of one exam (change one arbitrary value of ai). Help him change the date so that all ai remain different, and the value of μμ is as large as possible.
For example, for the schedule above, it is most advantageous for Dmitry to move the second exam to the very end of the session. The new schedule will take the form:
Now the rest periods before exams are equal to [2,2,5][2,2,5]. So, μ=2.
Dmitry can leave the proposed schedule unchanged (if there is no way to move one exam so that it will lead to an improvement in the situation).
Input
The first line of input data contains an integer t (1≤t≤10^4) — the number of input test cases. The descriptions of test cases follow.
An empty line is written in the test before each case.
The first line of each test case contains two integers n and d (2≤n≤2?105,1≤d≤109) — the number of exams and the length of the session, respectively.
The second line of each test case contains n integers ai (1≤ai≤d,ai<ai+1), where the ii-th number means the date of the ii-th exam.
It is guaranteed that the sum of nn for all test cases does not exceed 2?1052?105.
Output
For each test case, output the maximum possible value of μ if Dmitry can move any one exam to an arbitrary day. All values of ai should remain distinct.
Example
input
93 12 3 5 92 5 1 52 100 1 25 15 3 6 9 12 153 1000000000 1 400000000 5000000002 10 3 42 2 1 24 15 6 11 12 132 20 17 20output
2 1 1 2 99999999 3 0 1 9比賽的時候想到是貪心了,有思路但不會用代碼實現啊哭。
首先肯定就是要找到距離最小的那個位置,那個就是我們要修改的考試時間,我們把那個點拿出來,然后重新計算兩兩點之間的距離,此時,最小距離就有三種可能:
1、重新計算距離后中的最小距離 2、把移出來的那場考試的點移動到最大距離中間后得到的距離 3、把那場考試移動到最后一天
這三種情況得到的距離我們設為min_len,(max_len-1)/2 , d-a[i]-1。因為第一種情況是客觀存在的,我們無法對他進行修改,我們只能從第2和第3種情況中取得到距離最大的,然后再和min_len比較,輸出較小的那個。
有一點要注意的是,我們一開始求得的最小距離可能和兩場考試有關,舉個例子,d=10,考試數是:5 7 8 第三場考試前的休息天數是1,那么第二場考完試后的休息天數也是這個1,造成了這個休息天數只有1天的不光只和第三場考試有關,第二長考試也是和這個最短距離挨著的,所以我們還應該假設一下,如果把第二場考試移動到其它地方,可以獲得的最小距離是多少。方法和上面一樣。最后我們可以從這兩個最小距離中選最大的一種結果,這里可以選擇是因為我們可以選擇移動第二次考試或第三次考試,而不是像求最小距離時那樣不能移動考試場數。既然能選我們就要選最大的。
#include<iostream> using namespace std; #include<vector> #include<algorithm> #include<math.h> #include<set> #include<numeric> #include<string> #include<map> #include<unordered_map> #include<stack> #include<queue>typedef long long ll; typedef pair<int, int>PII; const int MOD = 1e9 + 7; const int N = 100100;int get_min(vector<int>space,int m) {int n = space.size(), max_len = 0, min_len = 1e9;for (int i = 1; i < n; i++){max_len = max(space[i] - space[i - 1] - 1, max_len);min_len = min(space[i] - space[i - 1] - 1, min_len);}return min(min_len, max((max_len - 1) / 2, m - 1 - space[n - 1])); }int main() {ios_base::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);int t;cin >> t;while (t--){int n, m, min_len = 1e9, sub = -1;cin >> n >> m;vector<int>v(n + 1), space;for (int i = 1; i <= n; i++){cin >> v[i];int len = v[i] - v[i - 1] - 1;if (min_len > len){min_len = len;sub = i;}}for (int i = 0; i <= n; i++){if (i != sub)space.push_back(v[i]);}int res = get_min(space, m);if (sub > 1){space[sub-1] = v[sub];}res = max(res, get_min(space, m));cout << res << endl;}return 0; }總結
以上是生活随笔為你收集整理的2022-03-10每日刷题打卡的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Keil安装stm库文件方法及下载地址
- 下一篇: Scrapy爬虫入门教程十 Feed e