欢迎您访问365答案网,请分享给你的朋友!
生活常识 学习资料

2022-02-14每日刷题打卡

时间:2023-06-05
一、AcWing 902 最短编辑距离 1、问题描述 2、问题解决

#include #include using namespace std;const int N = 1010;int n, m;char a[N], b[N];int f[N][N];int main(){ scanf("%d%s", &n, a + 1); scanf("%d%s", &m, b + 1); for (int i = 0; i <= m; i ++ ) f[0][i] = i; for (int i = 0; i <= n; i ++ ) f[i][0] = i; for (int i = 1; i <= n; i ++ ) for (int j = 1; j <= m; j ++ ) { f[i][j] = min(f[i - 1][j] + 1, f[i][j - 1] + 1); if (a[i] == b[j]) f[i][j] = min(f[i][j], f[i - 1][j - 1]); else f[i][j] = min(f[i][j], f[i - 1][j - 1] + 1); } printf("%dn", f[n][m]); return 0;}

二、AcWing 899、编辑距离 1、问题描述 2、问题解决

#include #include #include using namespace std;const int N = 15, M = 1010;int n, m;int f[N][N];char str[M][N];int edit_distance(char a[], char b[]){ int la = strlen(a + 1), lb = strlen(b + 1); for (int i = 0; i <= lb; i ++ ) f[0][i] = i; for (int i = 0; i <= la; i ++ ) f[i][0] = i; for (int i = 1; i <= la; i ++ ) for (int j = 1; j <= lb; j ++ ) { f[i][j] = min(f[i - 1][j] + 1, f[i][j - 1] + 1); f[i][j] = min(f[i][j], f[i - 1][j - 1] + (a[i] != b[j])); } return f[la][lb];}int main(){ scanf("%d%d", &n, &m); for (int i = 0; i < n; i ++ ) scanf("%s", str[i] + 1); while (m -- ) { char s[N]; int limit; scanf("%s%d", s + 1, &limit); int res = 0; for (int i = 0; i < n; i ++ ) if (edit_distance(str[i], s) <= limit) res ++ ; printf("%dn", res); } return 0;}

三、蓝桥杯十一届 1、问题描述 2、问题解决

#include #include using namespace std;constexpr size_t MAXN = 5e3;int dp[MAXN][27];int v;signed main() { ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); memset(dp, 0x80, sizeof dp), dp[0][0] = 0; cin >> v; for (int i = 1; i < MAXN; i++) for (int j = 1; j <= 26; j++) for (int k = 0; k < i; k++) dp[i][j] = max(dp[i][j], dp[k][j - 1] + k * (i - k)); int i = 0, j = 0, same = 0, flag = 1; while (flag) { j = 1, i++; for (; j <= 26; j++) { if (dp[i][j] >= v) { flag = 0; break; } } } while (i) { if (j == 1) cout << (char)'a'; else if (dp[i][j - 1] >= v) j--, cout << (char)(j + 'a' - 1), v -= i - 1, same = 1; else cout << (char)(j + 'a' - 1), v -= i - 1 - same, same++; i--; }}

Copyright © 2016-2020 www.365daan.com All Rights Reserved. 365答案网 版权所有 备案号:

部分内容来自互联网,版权归原作者所有,如有冒犯请联系我们,我们将在三个工作时内妥善处理。