diff --git "a/data/1.\347\256\227\346\263\225\345\210\235\351\230\266/7.\350\223\235\346\241\245\346\235\257-\345\212\250\346\200\201\350\247\204\345\210\222/6.\347\263\226\346\236\234/config.json" "b/data/1.\347\256\227\346\263\225\345\210\235\351\230\266/7.\350\223\235\346\241\245\346\235\257-\345\212\250\346\200\201\350\247\204\345\210\222/6.\347\263\226\346\236\234/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..321f3501b7a7ba485e9caaffcb552ab74441f696 --- /dev/null +++ "b/data/1.\347\256\227\346\263\225\345\210\235\351\230\266/7.\350\223\235\346\241\245\346\235\257-\345\212\250\346\200\201\350\247\204\345\210\222/6.\347\263\226\346\236\234/config.json" @@ -0,0 +1,9 @@ +{ + "node_id": "algorithm-30a892869c304cd483969480fe88b106", + "keywords": [ + "蓝桥杯", + "糖果" + ], + "children": [], + "export": [] +} \ No newline at end of file diff --git "a/data/1.\347\256\227\346\263\225\345\210\235\351\230\266/7.\350\223\235\346\241\245\346\235\257-\345\212\250\346\200\201\350\247\204\345\210\222/6.\347\263\226\346\236\234/desc.md" "b/data/1.\347\256\227\346\263\225\345\210\235\351\230\266/7.\350\223\235\346\241\245\346\235\257-\345\212\250\346\200\201\350\247\204\345\210\222/6.\347\263\226\346\236\234/desc.md" new file mode 100644 index 0000000000000000000000000000000000000000..67ba70922e6fec6ce58ff26dfa79d115d1e8f69a --- /dev/null +++ "b/data/1.\347\256\227\346\263\225\345\210\235\351\230\266/7.\350\223\235\346\241\245\346\235\257-\345\212\250\346\200\201\350\247\204\345\210\222/6.\347\263\226\346\236\234/desc.md" @@ -0,0 +1,25 @@ +糖果店的老板一共有M种口味的糖果出售。为了方便描述,我们将M种口味编号1~M。 +小明希望能品尝到所有口味的糖果。遗憾的是老板并不单独出售糖果,而是K颗一包整包出售。 +幸好糖果包装上注明了其中K颗糖果的口味,所以小明可以在买之前就知道每包内的糖果口味。 +给定包糖果,请你计算小明最少买几包,就可以品尝到所有口味的糖果。 +#### 输入 +第一行包含三个整数N、M 和K。 +接下来N 行每行K 这整数$T_1,T_2,…,T_K$,代表一包糖果的口味。 +1<=N<=100,1<=M<=20,1<=K<=20,1<=$T_i$<=M。 +#### 输出 +一个整数表示答案。如果小明无法品尝所有口味,输出-1。 + +#### 样例输入 +``` +6 5 3 +1 1 2 +1 2 3 +1 1 3 +2 3 5 +5 4 2 +5 1 2 +``` +#### 样例输出 +``` +2 +``` \ No newline at end of file diff --git "a/data/1.\347\256\227\346\263\225\345\210\235\351\230\266/7.\350\223\235\346\241\245\346\235\257-\345\212\250\346\200\201\350\247\204\345\210\222/6.\347\263\226\346\236\234/solution.cpp" "b/data/1.\347\256\227\346\263\225\345\210\235\351\230\266/7.\350\223\235\346\241\245\346\235\257-\345\212\250\346\200\201\350\247\204\345\210\222/6.\347\263\226\346\236\234/solution.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..dd24ec510574f7a8702fd3214e1fd2e150340d81 --- /dev/null +++ "b/data/1.\347\256\227\346\263\225\345\210\235\351\230\266/7.\350\223\235\346\241\245\346\235\257-\345\212\250\346\200\201\350\247\204\345\210\222/6.\347\263\226\346\236\234/solution.cpp" @@ -0,0 +1,37 @@ +#include +using namespace std; +int dp[1 << 20]; //dp[v]表示口味为v时所需要的最少糖果包数 +int ST[100]; //100包糖果 +int main() +{ + int n, m, k; + cin >> n >> m >> k; //输入n包糖果,m个口味,每包k个口味 + int tot = (1 << m) - 1; //表示所有m种口味各种组合方式 + memset(dp, -1, sizeof dp); //初始化dp全部为-1 + for (int i = 0; i < n; i++) //依次处理n包糖果 + { + int st = 0; + for (int j = 0; j < k; j++) //依次处理每颗糖果口味 + { + int x; + cin >> x; + st |= (1 << x - 1); //把第i包的第j颗糖果加入该包的口味st中 + } + dp[st] = 1; + ST[i] = st; + } + for (int i = 0; i <= tot; i++) //遍历所有口味组合方式 + { + if (dp[i] != -1) //表示已存在得到该口味的最少糖果包数量 + { + for (int j = 0; j < n; j++) //检查给定的n包糖果 + { + int st = ST[j]; + if (dp[i | st] == -1 || dp[i | st] > dp[i] + 1) //状态转移 + dp[i | st] = dp[i] + 1; + } + } + } + cout << dp[tot]; //得到所有口味tot的最少糖果包数 + return 0; +} diff --git "a/data/1.\347\256\227\346\263\225\345\210\235\351\230\266/7.\350\223\235\346\241\245\346\235\257-\345\212\250\346\200\201\350\247\204\345\210\222/6.\347\263\226\346\236\234/solution.java" "b/data/1.\347\256\227\346\263\225\345\210\235\351\230\266/7.\350\223\235\346\241\245\346\235\257-\345\212\250\346\200\201\350\247\204\345\210\222/6.\347\263\226\346\236\234/solution.java" new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git "a/data/1.\347\256\227\346\263\225\345\210\235\351\230\266/7.\350\223\235\346\241\245\346\235\257-\345\212\250\346\200\201\350\247\204\345\210\222/6.\347\263\226\346\236\234/solution.json" "b/data/1.\347\256\227\346\263\225\345\210\235\351\230\266/7.\350\223\235\346\241\245\346\235\257-\345\212\250\346\200\201\350\247\204\345\210\222/6.\347\263\226\346\236\234/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..0c85f4f43dcb2b09a87cd9ae4b75c3e8d7122685 --- /dev/null +++ "b/data/1.\347\256\227\346\263\225\345\210\235\351\230\266/7.\350\223\235\346\241\245\346\235\257-\345\212\250\346\200\201\350\247\204\345\210\222/6.\347\263\226\346\236\234/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "CSDN.net", + "source": "solution.md", + "exercise_id": "3c7880dd590c4f7cbe182c2c00af0e5a" +} \ No newline at end of file diff --git "a/data/1.\347\256\227\346\263\225\345\210\235\351\230\266/7.\350\223\235\346\241\245\346\235\257-\345\212\250\346\200\201\350\247\204\345\210\222/6.\347\263\226\346\236\234/solution.md" "b/data/1.\347\256\227\346\263\225\345\210\235\351\230\266/7.\350\223\235\346\241\245\346\235\257-\345\212\250\346\200\201\350\247\204\345\210\222/6.\347\263\226\346\236\234/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..cb0f5e56b6076cc30327acc42887c4625a1d2f81 --- /dev/null +++ "b/data/1.\347\256\227\346\263\225\345\210\235\351\230\266/7.\350\223\235\346\241\245\346\235\257-\345\212\250\346\200\201\350\247\204\345\210\222/6.\347\263\226\346\236\234/solution.md" @@ -0,0 +1,238 @@ +# 糖果 + +糖果店的老板一共有M种口味的糖果出售。为了方便描述,我们将M种口味编号1~M。 +小明希望能品尝到所有口味的糖果。遗憾的是老板并不单独出售糖果,而是K颗一包整包出售。 +幸好糖果包装上注明了其中K颗糖果的口味,所以小明可以在买之前就知道每包内的糖果口味。 +给定包糖果,请你计算小明最少买几包,就可以品尝到所有口味的糖果。 + +**输入** + +第一行包含三个整数N、M 和K。 +接下来N 行每行K 这整数$T_1,T_2,…,T_K$,代表一包糖果的口味。 +1<=N<=100,1<=M<=20,1<=K<=20,1<=$T_i$<=M。 + +**输出** + +一个整数表示答案。如果小明无法品尝所有口味,输出-1。 + + +**样例输入** + +``` +6 5 3 +1 1 2 +1 2 3 +1 1 3 +2 3 5 +5 4 2 +5 1 2 +``` + +**样例输出** + +``` +2 +``` + +以下错误的一项是? + +## aop + +### before + +```cpp +#include +using namespace std; +``` + +### after + +```cpp + +``` + +## 答案 + +```cpp +const int N = 105, M = (1 << 20) + 10; +int n, m, k, x, a[N], dp[M]; + +int main() +{ + ios::sync_with_stdio(false); + cin >> n >> m >> k; + memset(dp, -1, sizeof(dp)); + for (int i = 1; i <= n; i++) + { + int s = 0; + for (int j = 1; j <= k; j++) + { + cin >> x; + s |= (1 << (x - 1)); + } + dp[s] = 1; + a[i] = s; + } + for (int i = 1; i <= n; i++) + { + for (int j = 0; j < (1 << m); j++) + { + if (dp[j] == -1) + continue; + int to = j | a[i]; + if (dp[to] != -1) + { + dp[to] = min(dp[to], dp[j]); + } + else + { + dp[to] = dp[j] + 1; + } + } + } + printf("%d\n", dp[(1 << m) - 1]); + return 0; +} +``` + +## 选项 + +### A + +```cpp +int n, m, k; +int dp[1 << 20]; +vector a; + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + cin >> n >> m >> k; + for (int i = 0; i < (1 << m); i++) + { + dp[i] = 9999; + } + for (int i = 0; i < n; i++) + { + int s = 0; + for (int i = 0; i < k; i++) + { + int temp; + cin >> temp; + s |= 1 << (temp - 1); + } + a.push_back(s); + } + dp[0] = 0; + for (int i = 0; i < n; i++) + { + for (int j = 0; j < (1 << m); j++) + { + if (dp[j] == 9999 || (j | a[i]) == j) + continue; + dp[j | a[i]] = min(dp[j] + 1, dp[j | a[i]]); + } + } + if (dp[(1 << m) - 1] == 9999) + cout << -1; + else + cout << dp[(1 << m) - 1]; + return 0; +} +``` + +### B + +```cpp +const int maxn = (1 << 20) + 52; +const int inf = 9; +int a[505], n, m, k, dp[2][maxn]; +int min(int a, int b, int c) +{ + return min(min(a, b), c); +} +int main() +{ + scanf("%d%d%d", &n, &m, &k); + int e = 1 << m; + + for (int i = 1; i <= n; i++) + { + a[i] = 0; + for (int j = 0; j < k; j++) + { + int temp; + scanf("%d", &temp); + a[i] = a[i] | 1 << (temp - 1); + } + } + + for (int i = 0; i < maxn; i++) + { + dp[0][i] = dp[1][i] = inf; + } + dp[0][0] = 0; + + bool pos = 1; + for (int i = 1; i <= n; i++, pos = !pos) + { + for (int j = 0; j < e; j++) + { + dp[pos][j] = dp[!pos][j]; + } + for (int j = 0; j < e; j++) + { + dp[pos][j | a[i]] = min(dp[pos][j | a[i]], dp[!pos][j | a[i]], dp[!pos][j] + 1); + } + } + + if (dp[!pos][e - 1] == inf) + { + printf("-1\n"); + } + else + printf("%d\n", dp[!pos][e - 1]); + return 0; +} +``` + +### C + +```cpp +int dp[1 << 20]; +int ST[100]; +int main() +{ + int n, m, k; + cin >> n >> m >> k; + int tot = (1 << m) - 1; + memset(dp, -1, sizeof dp); + for (int i = 0; i < n; i++) + { + int st = 0; + for (int j = 0; j < k; j++) + { + int x; + cin >> x; + st |= (1 << x - 1); + } + dp[st] = 1; + ST[i] = st; + } + for (int i = 0; i <= tot; i++) + { + if (dp[i] != -1) + { + for (int j = 0; j < n; j++) + { + int st = ST[j]; + if (dp[i | st] == -1 || dp[i | st] > dp[i] + 1) + dp[i | st] = dp[i] + 1; + } + } + } + cout << dp[tot]; + return 0; +} +``` diff --git "a/data_backup/3.\350\223\235\346\241\245\346\235\257/37.\347\263\226\346\236\234/solution.md" "b/data_backup/3.\350\223\235\346\241\245\346\235\257/37.\347\263\226\346\236\234/solution.md" index 5be829e9fa30fed01b9e052b23537639a870c8cb..cb0f5e56b6076cc30327acc42887c4625a1d2f81 100644 --- "a/data_backup/3.\350\223\235\346\241\245\346\235\257/37.\347\263\226\346\236\234/solution.md" +++ "b/data_backup/3.\350\223\235\346\241\245\346\235\257/37.\347\263\226\346\236\234/solution.md" @@ -34,12 +34,15 @@ 2 ``` +以下错误的一项是? + ## aop ### before ```cpp - +#include +using namespace std; ``` ### after @@ -51,7 +54,45 @@ ## 答案 ```cpp - +const int N = 105, M = (1 << 20) + 10; +int n, m, k, x, a[N], dp[M]; + +int main() +{ + ios::sync_with_stdio(false); + cin >> n >> m >> k; + memset(dp, -1, sizeof(dp)); + for (int i = 1; i <= n; i++) + { + int s = 0; + for (int j = 1; j <= k; j++) + { + cin >> x; + s |= (1 << (x - 1)); + } + dp[s] = 1; + a[i] = s; + } + for (int i = 1; i <= n; i++) + { + for (int j = 0; j < (1 << m); j++) + { + if (dp[j] == -1) + continue; + int to = j | a[i]; + if (dp[to] != -1) + { + dp[to] = min(dp[to], dp[j]); + } + else + { + dp[to] = dp[j] + 1; + } + } + } + printf("%d\n", dp[(1 << m) - 1]); + return 0; +} ``` ## 选项 @@ -59,17 +100,139 @@ ### A ```cpp - +int n, m, k; +int dp[1 << 20]; +vector a; + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + cin >> n >> m >> k; + for (int i = 0; i < (1 << m); i++) + { + dp[i] = 9999; + } + for (int i = 0; i < n; i++) + { + int s = 0; + for (int i = 0; i < k; i++) + { + int temp; + cin >> temp; + s |= 1 << (temp - 1); + } + a.push_back(s); + } + dp[0] = 0; + for (int i = 0; i < n; i++) + { + for (int j = 0; j < (1 << m); j++) + { + if (dp[j] == 9999 || (j | a[i]) == j) + continue; + dp[j | a[i]] = min(dp[j] + 1, dp[j | a[i]]); + } + } + if (dp[(1 << m) - 1] == 9999) + cout << -1; + else + cout << dp[(1 << m) - 1]; + return 0; +} ``` ### B ```cpp - +const int maxn = (1 << 20) + 52; +const int inf = 9; +int a[505], n, m, k, dp[2][maxn]; +int min(int a, int b, int c) +{ + return min(min(a, b), c); +} +int main() +{ + scanf("%d%d%d", &n, &m, &k); + int e = 1 << m; + + for (int i = 1; i <= n; i++) + { + a[i] = 0; + for (int j = 0; j < k; j++) + { + int temp; + scanf("%d", &temp); + a[i] = a[i] | 1 << (temp - 1); + } + } + + for (int i = 0; i < maxn; i++) + { + dp[0][i] = dp[1][i] = inf; + } + dp[0][0] = 0; + + bool pos = 1; + for (int i = 1; i <= n; i++, pos = !pos) + { + for (int j = 0; j < e; j++) + { + dp[pos][j] = dp[!pos][j]; + } + for (int j = 0; j < e; j++) + { + dp[pos][j | a[i]] = min(dp[pos][j | a[i]], dp[!pos][j | a[i]], dp[!pos][j] + 1); + } + } + + if (dp[!pos][e - 1] == inf) + { + printf("-1\n"); + } + else + printf("%d\n", dp[!pos][e - 1]); + return 0; +} ``` ### C ```cpp - +int dp[1 << 20]; +int ST[100]; +int main() +{ + int n, m, k; + cin >> n >> m >> k; + int tot = (1 << m) - 1; + memset(dp, -1, sizeof dp); + for (int i = 0; i < n; i++) + { + int st = 0; + for (int j = 0; j < k; j++) + { + int x; + cin >> x; + st |= (1 << x - 1); + } + dp[st] = 1; + ST[i] = st; + } + for (int i = 0; i <= tot; i++) + { + if (dp[i] != -1) + { + for (int j = 0; j < n; j++) + { + int st = ST[j]; + if (dp[i | st] == -1 || dp[i | st] > dp[i] + 1) + dp[i | st] = dp[i] + 1; + } + } + } + cout << dp[tot]; + return 0; +} ``` diff --git a/leetcode_helper.py b/leetcode_helper.py index cdbd45782e3b19cca8aaab5f7c8293e845aa5c00..1301b1b5036c98969f24c3b928d523ddff946383 100644 --- a/leetcode_helper.py +++ b/leetcode_helper.py @@ -457,8 +457,8 @@ def add_color_for_special_exercises(): print(solution_md_path) with open(solution_md_path, 'r', encoding='utf-8') as f: solution_md_data = f.read() - if solution_md_data.find('错误') == -1: - solution_md_data = solution_md_data.replace('错误', '错误') + if solution_md_data.find('错误') == -1: + solution_md_data = solution_md_data.replace('错误', '错误') with open(solution_md_path, 'w', encoding='utf-8') as f: f.write(solution_md_data)