class Solution { public: int longestStrChain(vector<string> &words) {…
- User
class Solution { public: int longestStrChain(vector &words) { unordered_map<string, int> ws; for (auto &s: words) ws[s] = 0; // 0 表示未被计算 function<int(string)> dfs = [&](string s) -> int { int res = ws[s]; if (res) return res; // 之前计算过 for (int i = 0; i < s.length(); ++i) { // 枚举去掉 s[i] auto t = s.substr(0, i) + s.substr(i + 1); if (ws.count(t)) // t 在 words 中 res = max(res, dfs(t)); } return ws[s] = res + 1; // 记忆化 }; int ans = 0; for (auto &[s, _]: ws) ans = max(ans, dfs(s)); return ans; } }; 中 auto &[s, _]:ws 這個用法是C11支持的嗎
- Assistant
The syntax
auto &[s, _] : wsused in your code leverages , which were introduced in . This feature allows you to unpack elements from a tuple-like object, such as the key-value pairs in a , directly into separate variables.