T1 5946. Maximum number of words in a sentence
Title Description: give you \ (n \) sentences and find the number of words in the sentence with the most words
Idea: simulate according to the meaning of the topic
Time complexity: \ (O(\sum|S|) \)
Reference code:
class Solution { public: int mostWordsFound(vector<string>& sentences) { int res = 0; string t; for(auto s : sentences){ stringstream text(s); int cnt = 0; while(text >> t) ++cnt; res = max(res , cnt); } return res; } };
T2 5947. Find all the dishes you can make from a given raw material
Title Description: you have information about \ (n \) different dishes. Give you a string array \ (recipes \) and a two-dimensional string array \ (ingredients \). The name of the \ (I \) dish is \ (recipes[i] \). If you have all its raw materials \ (ingredients[i] \), you can make this dish. The raw material of one dish may be another dish, that is to say \ (ingredients[i] \) may contain another string in \ (recipes \). At the same time, a string array \ (supplies \) is given to you, which contains all the raw materials you have at the beginning. You have unlimited quantities of each raw material. Please return to all the dishes you can make. You can return them in any order.
Idea: for obvious topological relationship, consider mapping dish names into points with map, and then build a map to run the topology
Time complexity: \ (O(n) \)
Reference code:
class Solution { public: vector<string> findAllRecipes(vector<string>& recipes, vector<vector<string>>& ingredients, vector<string>& supplies) { vector<string> res; vector<int>income(1000, 0); map<string, int> mp1; map<int, string> mp2; int cnt = 0; vector<vector<int>>graph(1000); int n = recipes.size(); for (auto s : recipes) { if (mp1[s]) continue; mp1[s] = ++cnt; mp2[cnt] = s; } for (int i = 0; i < n; ++i) { for (int j = 0; j < ingredients[i].size(); ++j) { string s = ingredients[i][j]; if (mp1[s] == 0) { mp1[s] = ++cnt; mp2[cnt] = s; } int dx = mp1[s]; graph[dx].push_back(mp1[recipes[i]]); income[mp1[recipes[i]]] ++; } } vector<int>vis(cnt + 1, 0); queue<int> q; for (auto s : supplies) { if (mp1[s] == 0) continue; int dx = mp1[s]; vis[dx] = true; q.push(dx); } while (!q.empty()) { int ver = q.front(); q.pop(); for (auto g : graph[ver]) { if (vis[g]) continue; if (--income[g] == 0) { vis[g] = true; q.push(g); } } } for (auto s : recipes) { if (vis[mp1[s]]) res.push_back(s); } return res; } };
T3 5948. Determine whether a bracket string is valid
Topic Description: give you two strings \ (s \) and \ (t \), \ (s \) string only contains \ ('\) and \ (' '\), \ (t \) string only contains \ (0, 1 \), which means that if \ (0 \) means that the characters on the corresponding bit of \ (s \) string can be changed, otherwise they cannot be changed. Judge whether the \ (s \) string is a legal bracket sequence.
Idea: consider taking the position of \ (t_i = 0 \) as a wildcard. Define \ (minCntleft \) to represent the least left parenthesis at present and \ (maxCntleft \) to represent the most left parenthesis at present. Each time you encounter the position of \ (t_i = 1 \), you will both \ (+ 1 \) or \ (- 1 \). If you encounter places that can be changed, you will \ (maxCntleft + 1 \) and \ (minCntleft - 1 \), Note that the minimum number of left parentheses should not be less than \ (0 \), and \ (maxCntleft \) must be non negative at any time. Finally, judge whether \ (minCntleft \) is equal to \ (0 \).
Time complexity: \ (O(n) \)
Reference code:
class Solution { private: public: bool canBeValid(string s, string t) { int n = s.size(); if(n % 2 == 1) return false; int minCntleft = 0 , maxCntleft = 0; for(int i = 0 ; i < n ; ++i){ if(s[i] == '(' && t[i] == '1') ++minCntleft, ++maxCntleft; else if(s[i] == ')' && t[i] == '1') --minCntleft , --maxCntleft; else --minCntleft, ++maxCntleft; minCntleft = max(0 , minCntleft); if(maxCntleft < 0) return false; } return minCntleft == 0; } };
T4 5949. Abbreviation for the product of all numbers in an interval
Title Description: give you two positive integers \ (left \) and \ (right \) to meet \ (left \leq right \). Please calculate the product of all integers in the closed interval $[left, right] $. Since the product may be very large, you need to abbreviate it as follows: count the number of suffixes \ (0 \) in the product and record this number as $C $.
For example, there are \ (3 \) suffixes \ (0 \) in \ (1000 \) and no suffixes \ (0 \) in \ (546 \). Mark the remaining numbers in the product as \ (D \). If \ (d > 10 \), the product is expressed in the form of \ (< pre >... < suf > \), where \ (< pre > \) represents the first \ (5 \) digits of the product and \ (< suf > \) represents the last \ (5 \) digits after the suffix \ (0 \) is deleted. If \ (d \leq 10 \), we will not modify it. For example, we express \ (123456765421 \) as \ (12345... 54321 \), but \ (1234567 \) is still expressed as \ (1234567 \). Finally, the product is expressed as a string \ ('' < pre >... < suf > EC '' \). For example, \ (12345678987600000 \) is represented as \ ('' 12345...89876e5 '' \). Please return a string representing the abbreviation of the product of all integers in the closed interval \ ([left, right] \).
Idea: first, consider the mantissa \ (C \), and calculate the number of each number in \ ([left, right] \) containing factor \ (2 \) and factor \ (5 \), the smaller of which is \ (C \); For mantissa, consider removing the factors \ (2 \) and \ (5 \) from the numbers between \ ([left, right] \), and then form a new array. The product of these numbers multiplied by multiple eliminated \ (2 \) or \ (5 \) is the part of the product without mantissa \ (0 \), and we only need to modulo the product \ (1e5 \) in the calculation process; For the prefix part, it is considered to reserve the high \ (12 \) bit during the calculation of the product (theoretically incorrect), and then reserve the high \ (5 \) bit at last; Judge whether the product exceeds \ (10 \) bits. You can judge it when calculating the prefix part.
You can refer to the problem solution written by the boss: Disassemble the abbreviation of the product of all numbers in an interval
Time complexity: \ (O(nlogn) \)
Reference code:
class Solution { private: string cal(vector<int>& nums, int cnt2, int cnt5) { long long res = 1, ans = 1;//Prefix suffix const int mod = 1e5;//model bool flag = false;//Whether the mark length exceeds 10 digits int cnt = 5;//The number of divisions required during prefix retention is at least 5 //Calculate the score of the elements in the array for (auto num : nums) { res *= num; ans *= num; ans %= mod; if (res >= 1e10)flag = true;//The product exceeds 10 bits while (res >= 1e12)res /= 10, --cnt;//Keep the upper 12 bits and count the times of division - 1 } //Multiply by 2 for (int i = 1; i <= cnt2; ++i) { res *= 2; ans *= 2; ans %= mod; if (res >= 1e10)flag = true; while (res >= 1e12)res /= 10, --cnt; } //Multiply by 5 for (int i = 1; i <= cnt5; ++i) { res *= 5; ans *= 5; ans %= mod; if (res >= 1e10)flag = true; while (res >= 1e12)res /= 10, --cnt; } //Only 5 digits are reserved for the prefix while (res >= 100000) res /= 10, --cnt; //If you haven't divided it five times, you need to continue to divide it while (cnt > 0) res /= 10, --cnt; string t1 = to_string(res), t2 = to_string(ans); if (flag) {//If the length exceeds 10 while (t2.size() < 5) t2 = '0' + t2;//Prefix 0 is required for suffix return t1 + "..." + t2; } //Length less than 10 else if (res == 0) return to_string(ans);//Prefix equal to 0 //If the prefix is not equal to 0, you need to supplement the leading 0 for the suffix while (t2.size() < 5) t2 = '0' + t2; return t1 + t2; } public: string abbreviateProduct(int left, int right) { //Convert the interval into an array and eliminate factors 2 and 5 vector<int> nums; int cnt2 = 0, cnt5 = 0; for (int i = left; i <= right; ++i) { int j = i; while (j % 2 == 0) ++cnt2, j /= 2; while (j % 5 == 0) ++cnt5, j /= 5; nums.push_back(j); } //The number of 0 is equal to the smaller of 2 and 5 int C = min(cnt2, cnt5); //Calculate the previous part string res = cal(nums, cnt2 - C, cnt5 - C); return res + "e" + to_string(C); } };