Topcoder SRM 698 Div1 250 RepeatString(dp)
程序员文章站
2022-05-21 11:18:10
题意 ~~[题目链接]~~这怎么发链接啊。。。。。 Sol 枚举一个断点,然后类似于LIS一样dp一波 这个边界条件有点迷啊。。fst了两遍。。。 cpp include using namespace std; const int MAXN = 1e5 + 10, INF = 1e9 + 7; i ......
题意
[题目链接]这怎么发链接啊。。。。。
sol
枚举一个断点,然后类似于lis一样dp一波
这个边界条件有点迷啊。。fst了两遍。。。
#include<bits/stdc++.h> using namespace std; const int maxn = 1e5 + 10, inf = 1e9 + 7; inline int read() { char c = getchar(); int x = 0, f = 1; while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();} while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar(); return x * f; } int n, f[101][101]; class repeatstring{ public: int solve(int pos, string s) { string a, b; a += '.'; b += '*'; memset(f, 0x3f, sizeof(f)); for(int i = 0; i < pos; i++) a += s[i]; for(int i = pos; i < n; i++) b += s[i]; int la = a.length() - 1, lb = b.length() - 1; f[0][0] = 0; for(int i = 0; i <= la; i++) f[i][0] = i; for(int i = 0; i <= lb; i++) f[0][i] = 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 minimalmodify(string s) { n = s.length(); int ans = n; for(int i = 0; i < n; i++) ans = min(ans, solve(i, s)); return ans; } }; int main() { string s; cin >> s; cout << repeatstring().minimalmodify(s); } /* pkafkbeabccfjejkdgkaatcedaocgmecaapakfvbfgefr */