将字符串转化为回文串,并记录方案
#include <iostream> #include <stdio.h> #include <algorithm> #include <string> #include <cmath> #include <string.h> #define R(x) x = read() #define For(i, j, n) for (int i = j; i <= n; ++i) using namespace std; inline int read() { int x = 0, f = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); } return x * f; } const int N = 1005; char s[N], s2[N]; int f[N][N],n; void solve() { int lens=strlen(1+s); n=strlen(1+s); for (int length = 2; length <= n; ++length) { for (int i = 1; i <= n - length + 1; ++i) { int j = i + length - 1; if (s[i] == s[j]) { f[i][j] = f[i + 1][j - 1]; } else { f[i][j] = min(f[i + 1][j], f[i][j - 1]) + 1; } } } cout << f[1][n] << endl; } string t1, t2; void build() { int i = 1, j = n; while (i < j) { cout << i << " " << j << endl; if (s[i] == s[j]) { // cout << "1" << " " << s[i] << endl; t1.push_back(s[i]); ++i; --j; } else if (f[i][j] == f[i + 1][j] + 1) { //cout << "2" << " " << s[i] << endl; t1.push_back(s[i]); ++i; } else { // cout << "3" << " " << s[j] << endl; t1.push_back(s[j]); --j; } cout << i << " " << j << endl; } if (i == j) { t1.push_back(s[i]); } int l = t1.size() - 1; if(i > j) l++; cout << t1 << endl; string t2 = t1.substr(0, l); reverse(t2.begin(), t2.end()); t1 += t2; } int main() { scanf("%s", 1+s); solve(); build(); cout << t1 << endl; return 0; }
如果最后i==j,说明目标串长度是奇数;否则为偶数
本文作者:Gold_stein
本文链接:https://www.cnblogs.com/smartljy/p/18113620
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】