From d99ea6628731ccf6cfe393dc02b988da166b1f9d Mon Sep 17 00:00:00 2001 From: yanglbme Date: Mon, 20 May 2024 19:03:26 +0800 Subject: [PATCH] feat: add solutions to lcp problem: No.80 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit LCP 80.生物进化录 --- .../README.md" | 106 ++++++++++++++++++ .../Solution.cpp" | 27 +++++ .../Solution.go" | 20 ++++ .../Solution.java" | 22 ++++ .../Solution.py" | 11 ++ 5 files changed, 186 insertions(+) create mode 100644 "lcp/LCP 80. \347\224\237\347\211\251\350\277\233\345\214\226\345\275\225/Solution.cpp" create mode 100644 "lcp/LCP 80. \347\224\237\347\211\251\350\277\233\345\214\226\345\275\225/Solution.go" create mode 100644 "lcp/LCP 80. \347\224\237\347\211\251\350\277\233\345\214\226\345\275\225/Solution.java" create mode 100644 "lcp/LCP 80. \347\224\237\347\211\251\350\277\233\345\214\226\345\275\225/Solution.py" diff --git "a/lcp/LCP 80. \347\224\237\347\211\251\350\277\233\345\214\226\345\275\225/README.md" "b/lcp/LCP 80. \347\224\237\347\211\251\350\277\233\345\214\226\345\275\225/README.md" index 5c4e27a793e7f..b2ce4b4fb2eb2 100644 --- "a/lcp/LCP 80. \347\224\237\347\211\251\350\277\233\345\214\226\345\275\225/README.md" +++ "b/lcp/LCP 80. \347\224\237\347\211\251\350\277\233\345\214\226\345\275\225/README.md" @@ -54,4 +54,110 @@ edit_url: https://github.com/doocs/leetcode/edit/main/lcp/LCP%2080.%20%E7%94%9F% +### 方法一:DFS + + + +#### Python3 + +```python +class Solution: + def evolutionaryRecord(self, parents: List[int]) -> str: + def dfs(i: int) -> str: + t = sorted(dfs(j) for j in g[i]) + return "0" + "".join(t) + "1" + + n = len(parents) + g = [[] for _ in range(n)] + for i in range(1, n): + g[parents[i]].append(i) + return dfs(0)[1:].rstrip("1") +``` + +#### Java + +```java +class Solution { + private List[] g; + + public String evolutionaryRecord(int[] parents) { + int n = parents.length; + g = new List[n]; + Arrays.setAll(g, k -> new ArrayList<>()); + for (int i = 1; i < n; ++i) { + g[parents[i]].add(i); + } + return dfs(0).substring(1).replaceAll("1+$", ""); + } + + private String dfs(int i) { + List t = new ArrayList<>(); + for (int j : g[i]) { + t.add(dfs(j)); + } + Collections.sort(t); + return "0" + String.join("", t) + "1"; + } +} +``` + +#### C++ + +```cpp +class Solution { +public: + string evolutionaryRecord(vector& parents) { + int n = parents.size(); + vector> g(n); + for (int i = 1; i < n; ++i) { + g[parents[i]].push_back(i); + } + + function dfs = [&](int i) -> string { + vector t; + for (int j : g[i]) { + t.push_back(dfs(j)); + } + sort(t.begin(), t.end()); + string res = "0"; + for (const string& s : t) { + res += s; + } + res += "1"; + return res; + }; + + string ans = dfs(0); + return ans.substr(1, ans.find_last_not_of('1')); + } +}; +``` + +#### Go + +```go +func evolutionaryRecord(parents []int) string { + n := len(parents) + g := make([][]int, n) + for i := 1; i < n; i++ { + g[parents[i]] = append(g[parents[i]], i) + } + + var dfs func(int) string + dfs = func(i int) string { + var t []string + for _, j := range g[i] { + t = append(t, dfs(j)) + } + sort.Strings(t) + return "0" + strings.Join(t, "") + "1" + } + + ans := dfs(0)[1:] + return strings.TrimRight(ans, "1") +} +``` + + + diff --git "a/lcp/LCP 80. \347\224\237\347\211\251\350\277\233\345\214\226\345\275\225/Solution.cpp" "b/lcp/LCP 80. \347\224\237\347\211\251\350\277\233\345\214\226\345\275\225/Solution.cpp" new file mode 100644 index 0000000000000..21bc9ee1528df --- /dev/null +++ "b/lcp/LCP 80. \347\224\237\347\211\251\350\277\233\345\214\226\345\275\225/Solution.cpp" @@ -0,0 +1,27 @@ +class Solution { +public: + string evolutionaryRecord(vector& parents) { + int n = parents.size(); + vector> g(n); + for (int i = 1; i < n; ++i) { + g[parents[i]].push_back(i); + } + + function dfs = [&](int i) -> string { + vector t; + for (int j : g[i]) { + t.push_back(dfs(j)); + } + sort(t.begin(), t.end()); + string res = "0"; + for (const string& s : t) { + res += s; + } + res += "1"; + return res; + }; + + string ans = dfs(0); + return ans.substr(1, ans.find_last_not_of('1')); + } +}; \ No newline at end of file diff --git "a/lcp/LCP 80. \347\224\237\347\211\251\350\277\233\345\214\226\345\275\225/Solution.go" "b/lcp/LCP 80. \347\224\237\347\211\251\350\277\233\345\214\226\345\275\225/Solution.go" new file mode 100644 index 0000000000000..b3eca5d0f8d72 --- /dev/null +++ "b/lcp/LCP 80. \347\224\237\347\211\251\350\277\233\345\214\226\345\275\225/Solution.go" @@ -0,0 +1,20 @@ +func evolutionaryRecord(parents []int) string { + n := len(parents) + g := make([][]int, n) + for i := 1; i < n; i++ { + g[parents[i]] = append(g[parents[i]], i) + } + + var dfs func(int) string + dfs = func(i int) string { + var t []string + for _, j := range g[i] { + t = append(t, dfs(j)) + } + sort.Strings(t) + return "0" + strings.Join(t, "") + "1" + } + + ans := dfs(0)[1:] + return strings.TrimRight(ans, "1") +} \ No newline at end of file diff --git "a/lcp/LCP 80. \347\224\237\347\211\251\350\277\233\345\214\226\345\275\225/Solution.java" "b/lcp/LCP 80. \347\224\237\347\211\251\350\277\233\345\214\226\345\275\225/Solution.java" new file mode 100644 index 0000000000000..00697fa95d929 --- /dev/null +++ "b/lcp/LCP 80. \347\224\237\347\211\251\350\277\233\345\214\226\345\275\225/Solution.java" @@ -0,0 +1,22 @@ +class Solution { + private List[] g; + + public String evolutionaryRecord(int[] parents) { + int n = parents.length; + g = new List[n]; + Arrays.setAll(g, k -> new ArrayList<>()); + for (int i = 1; i < n; ++i) { + g[parents[i]].add(i); + } + return dfs(0).substring(1).replaceAll("1+$", ""); + } + + private String dfs(int i) { + List t = new ArrayList<>(); + for (int j : g[i]) { + t.add(dfs(j)); + } + Collections.sort(t); + return "0" + String.join("", t) + "1"; + } +} \ No newline at end of file diff --git "a/lcp/LCP 80. \347\224\237\347\211\251\350\277\233\345\214\226\345\275\225/Solution.py" "b/lcp/LCP 80. \347\224\237\347\211\251\350\277\233\345\214\226\345\275\225/Solution.py" new file mode 100644 index 0000000000000..bfe508233641a --- /dev/null +++ "b/lcp/LCP 80. \347\224\237\347\211\251\350\277\233\345\214\226\345\275\225/Solution.py" @@ -0,0 +1,11 @@ +class Solution: + def evolutionaryRecord(self, parents: List[int]) -> str: + def dfs(i: int) -> str: + t = sorted(dfs(j) for j in g[i]) + return "0" + "".join(t) + "1" + + n = len(parents) + g = [[] for _ in range(n)] + for i in range(1, n): + g[parents[i]].append(i) + return dfs(0)[1:].rstrip("1")