diff --git a/@_M%)AU31060_V$0LDVY]F9.png b/@_M%)AU31060_V$0LDVY]F9.png new file mode 100644 index 0000000000000000000000000000000000000000..842e5a32351566e0fd41b541c3d7865b987e10fb Binary files /dev/null and b/@_M%)AU31060_V$0LDVY]F9.png differ diff --git a/K1[@K`S1RNQW~[1_5{$6Q98.png b/K1[@K`S1RNQW~[1_5{$6Q98.png new file mode 100644 index 0000000000000000000000000000000000000000..af894c0de7b265ab5d5de12945d3a828305937a4 Binary files /dev/null and b/K1[@K`S1RNQW~[1_5{$6Q98.png differ diff --git a/Subset.cpp b/Subset.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1ed1c0ed0afdba4fb7cd7f97151bda09463535b9 --- /dev/null +++ b/Subset.cpp @@ -0,0 +1,24 @@ +#include +#include +using namespace std; +vector> subsets(vector& nums) +{ + vector t; + vector> ans; + int n = nums.size(); + for (int mask = 0; mask < (1 << n); ++mask) { + t.clear(); + for (int i = 0; i < n; ++i) { + if (mask & (1 << i)) { + t.push_back(nums[i]); + } + } + ans.push_back(t); + } + return ans; +} +int main() +{ + vector nums = {1,2,3}; + subsets(nums); +} \ No newline at end of file diff --git a/U$T4H13}PTOP8S}}@P~V`QK.png b/U$T4H13}PTOP8S}}@P~V`QK.png new file mode 100644 index 0000000000000000000000000000000000000000..06096a6039daee5e11efadba1d20afb587a2f3b5 Binary files /dev/null and b/U$T4H13}PTOP8S}}@P~V`QK.png differ diff --git a/WordBreak.py b/WordBreak.py new file mode 100644 index 0000000000000000000000000000000000000000..93babe1354e2883e0f00048bf33926a094ab8b50 --- /dev/null +++ b/WordBreak.py @@ -0,0 +1,16 @@ + +def wordBreak( s: str, wordDict: list[str]) -> bool: + n=len(s) + dp=[False]*(n+1) + dp[0]=True + for i in range(n): + for j in range(i+1,n+1): + if(dp[i] and (s[i:j] in wordDict)): + dp[j]=True + print(dp[-1]) + return dp[-1] + + +s="applephone" +wordDict=['apple','phone'] +wordBreak( s, wordDict) diff --git a/alg.md b/alg.md deleted file mode 100644 index 5e691e6552f623b44990e84910b1ba83d099188d..0000000000000000000000000000000000000000 --- a/alg.md +++ /dev/null @@ -1,39 +0,0 @@ -# 算法设计与分析第四次作业 - -#### 介绍 -{**以下是 Gitee 平台说明,您可以替换此简介** -Gitee 是 OSCHINA 推出的基于 Git 的代码托管平台(同时支持 SVN)。专为开发者提供稳定、高效、安全的云端软件开发协作平台 -无论是个人、团队、或是企业,都能够用 Gitee 实现代码托管、项目管理、协作开发。企业项目请看 [https://gitee.com/enterprises](https://gitee.com/enterprises)} - -#### 软件架构 -软件架构说明 - - -#### 安装教程 - -1. xxxx -2. xxxx -3. xxxx - -#### 使用说明 - -1. xxxx -2. xxxx -3. xxxx - -#### 参与贡献 - -1. Fork 本仓库 -2. 新建 Feat_xxx 分支 -3. 提交代码 -4. 新建 Pull Request - - -#### 特技 - -1. 使用 Readme\_XXX.md 来支持不同的语言,例如 Readme\_en.md, Readme\_zh.md -2. Gitee 官方博客 [blog.gitee.com](https://blog.gitee.com) -3. 你可以 [https://gitee.com/explore](https://gitee.com/explore) 这个地址来了解 Gitee 上的优秀开源项目 -4. [GVP](https://gitee.com/gvp) 全称是 Gitee 最有价值开源项目,是综合评定出的优秀开源项目 -5. Gitee 官方提供的使用手册 [https://gitee.com/help](https://gitee.com/help) -6. Gitee 封面人物是一档用来展示 Gitee 会员风采的栏目 [https://gitee.com/gitee-stars/](https://gitee.com/gitee-stars/) diff --git a/alg.py b/alg.py deleted file mode 100644 index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..0000000000000000000000000000000000000000 diff --git a/gen.py b/gen.py deleted file mode 100644 index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..0000000000000000000000000000000000000000 diff --git a/integerReplacement.cpp b/integerReplacement.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d91ca49718b2887d66f1444bf9bcb94f26027ec0 --- /dev/null +++ b/integerReplacement.cpp @@ -0,0 +1,24 @@ +#include +using namespace std; +int main() +{ + int n; cin >> n; + int ans = 0; + while (n != 1) { + if (n % 2 == 0) + { + n = n / 2; + } + else if (n == 3) + { + n = 2; + } + else + { + n = n + 1; + } + ans++; + } + cout << ans; + return 0; +} \ No newline at end of file diff --git a/test.py b/test.py deleted file mode 100644 index b0f1d0bb981822db57d05cdfceeb4266aa78d5c3..0000000000000000000000000000000000000000 --- a/test.py +++ /dev/null @@ -1,36 +0,0 @@ -# 算法设计与分析第四次作业 - -#### Description -{**When you're done, you can delete the content in this README and update the file with details for others getting started with your repository**} - -#### Software Architecture -Software architecture description - -#### Installation - -1. xxxx -2. xxxx -3. xxxx - -#### Instructions - -1. xxxx -2. xxxx -3. xxxx - -#### Contribution - -1. Fork the repository -2. Create Feat_xxx branch -3. Commit your code -4. Create Pull Request - - -#### Gitee Feature - -1. You can use Readme\_XXX.md to support different languages, such as Readme\_en.md, Readme\_zh.md -2. Gitee blog [blog.gitee.com](https://blog.gitee.com) -3. Explore open source project [https://gitee.com/explore](https://gitee.com/explore) -4. The most valuable open source project [GVP](https://gitee.com/gvp) -5. The manual of Gitee [https://gitee.com/help](https://gitee.com/help) -6. The most popular members [https://gitee.com/gitee-stars/](https://gitee.com/gitee-stars/) diff --git "a/\347\256\227\346\263\225\350\256\276\350\256\241\345\210\206\346\236\220s=\345\256\236\351\252\214\345\233\233.md" "b/\347\256\227\346\263\225\350\256\276\350\256\241\345\210\206\346\236\220s=\345\256\236\351\252\214\345\233\233.md" new file mode 100644 index 0000000000000000000000000000000000000000..1f532e9f16f524cb6d36b4a80dad032e3d1aa643 --- /dev/null +++ "b/\347\256\227\346\263\225\350\256\276\350\256\241\345\210\206\346\236\220s=\345\256\236\351\252\214\345\233\233.md" @@ -0,0 +1,191 @@ +# 算法设计分析 + +### 单词拆分 + +###### 1.算法实现及测试 + +```python +def wordBreak( s: str, wordDict: list[str]) -> bool: + n=len(s) + dp=[False]*(n+1) + dp[0]=True + for i in range(n): + for j in range(i+1,n+1): + if(dp[i] and (s[i:j] in wordDict)): + dp[j]=True + print(dp[-1]) + return dp[-1] + + +s="applephone" +wordDict=['apple','phone'] +wordBreak( s, wordDict) +``` + +*思路*: + +1.把单词拆分成每一个字母, + +| | a | p | p | l | e | p | h | o | n | e | +| ---- | :---: | :--: | :--: | :--: | :--: | :--: | :--: | :--: | ---- | :--: | +| true | false | f | f | f | t | f | f | f | f | t | + +2.初始化dp=[f,f,........,f]长度为n+1.n为字符串长度。dp[i]表示s的前i位是否可以用字典中的单词表示。 + +3.初始化dp[0]=true,空字符可以表示。 + +4.遍历字符串的所有子串,遍历开始索引i,遍历区间(0,n).遍历结束索引j,遍历区间(i+,n+1). + + + +**动态规划**:动态规划(英语:Dynamic programming,简称 DP),是一种在数学、管理科学、计算机科学、经济学和生物信息学中使用的,通过把原问题分解为相对简单的子问题的方式求解复杂问题的方法。动态规划常常适用于有重叠子问题和最优子结构性质的问题。 + +###### 3.性能分析 + +**时间复杂度**: +$$ +O(n^2) +$$ +**空间复杂度**: +$$ +O(n) +$$ + + + +### 整数转换 + +###### 1.算法实现及测试 + +```c++ +#include +using namespace std; +int main() +{ + int n; cin >> n; + int ans = 0; + while (n != 1) { + if (n % 2 == 0) + { + n = n / 2; + } + else if (n == 3) + { + n = 2; + } + else + { + n = n + 1; + } + ans++; + } + cout << ans; + return 0; +} +``` + +*思路*: + +不断的转换,将偶数转换成其1/2,奇数先加一或减一再转换成其1/2,考虑特殊情况3,此时加一,变成四,减一变成二,所以到三时减一更优。 + +**贪心算法**:贪心算法(greedy algorithm ,又称贪婪算法)是指,在对[问题求解](https://baike.baidu.com/item/问题求解/6693186?fromModule=lemma_inlink)时,总是做出在当前看来是最好的选择。也就是说,不从整体最优上加以考虑,[算法](https://baike.baidu.com/item/算法/209025?fromModule=lemma_inlink)得到的是在某种意义上的局部最优解 。贪心算法不是对所有问题都能得到整体最优解,关键是贪心策略的选择 + +###### 3.性能分析 + +**时间复杂度**: +$$ +O(n) +$$ +**空间复杂度**: +$$ +O(n) +$$ + + + +### 求所有子集 + +###### 1.算法实现及测试 + +```c++ +#include +#include +using namespace std; +vector> subsets(vector& nums) +{ + vector t; + vector> ans; + int n = nums.size(); + for (int mask = 0; mask < (1 << n); ++mask) { + t.clear(); + for (int i = 0; i < n; ++i) { + if (mask & (1 << i)) { + t.push_back(nums[i]); + } + } + ans.push_back(t); + } + return ans; +} +int main() +{ + vector nums = {1,2,3}; + subsets(nums); +} +``` + +*思路*: + +记原序列中元素总数为n,原序列每个数字ai的状态有两种,在或不在子集中。我们用1表示在,0表示不在,那么每一个子集对应一个长度为n的0/1序列。如{1,2,3}. + +| 0/1序列 | 子集 | 二进制数 | +| ------- | --------- | -------- | +| 000 | {} | 0 | +| 001 | {3} | 1 | +| 010 | {2} | 2 | +| 011 | {2,3} | 3 | +| 100 | {1} | 4 | +| 101 | {1,3} | 5 | +| 110 | {1,2} | 6 | +| 111 | {1,2,3} | 7 | + +发现0/1序列对应的二进制数正好从0到2的n次方减一。枚举mask。其而记者表示是一个0/1序列。按照0/1序列从原集中取数。 + +**贪心算法**:回溯算法实际上一个类似枚举的搜索尝试过程,主要是在搜索尝试过程中寻找问题的解,当发现已不满足求解条件时,就“回溯”返回,尝试别的路径。回溯法是一种选优[搜索](https://baike.baidu.com/item/搜索/2791632?fromModule=lemma_inlink)法,按选优条件向前搜索,以达到目标。但当探索到某一步时,发现原先选择并不优或达不到目标,就退回一步重新选择,这种走不通就退回再走的技术为回溯法,而满足回溯条件的某个状态的点称为“回溯点”。许多复杂的,规模较大的问题都可以使用回溯法,有“通用解题方法”的美称。 + +###### 3.性能分析 + +**时间复杂度**: +$$ +O(n*2^n) +$$ +**空间复杂度**: +$$ +O(n) +$$ + + + + + +**代码测试** + +![](C:\Users\20292\Desktop\@_M%)AU31060_V$0LDVY]F9.png) + +![](C:\Users\20292\Desktop\K1[@K`S1RNQW~[1_5{Q98.png) + +![](C:\Users\20292\Desktop\U$T4H13}PTOP8S}}@P~V`QK.png) + +5.心得体会 + +这是最后一次实验,代码部分分别用了python和c++,因为对他们的掌握程度不同,比较熟悉的用了c++,写起来相对容易的用了python。对于git的使用,基本的提交,合并等操作已经没有什么问题了。markdown语法也比较熟练。最主要的问题还是在算法的理解和代码的编写上,有的部分仍然不太清楚,有参考网上的代码。自我感觉还是应该多上手写。 + +###### 6.实验环境 + +win11. + +python3.11 + +vs2022 + +git. \ No newline at end of file