删除最少字符,得到回文串
来源:互联网 发布:mac os 卸载程序 编辑:程序博客网 时间:2024/06/05 10:53
给定一个字符串s,你可以从中删除一些字符,使得剩下的串是一个回文串。如何删除才能使得回文串最长呢?
输出需要删除的字符个数。
tips:最长公共自序列的变形,如果能想到这点的话,解决起来就不会太困难。
#include<iostream>#include<string>#include<algorithm>using namespace std;const int MAX = 1001;int MaxLen[MAX][MAX]; //最长公共子序列,动态规划求法int maxLen(string s1, string s2){ int length1 = s1.size(); int length2 = s2.size(); for (int i = 0; i < length1; ++i) MaxLen[i][0] = 0; for (int i = 0; i < length2; ++i) MaxLen[0][i] = 0; for (int i = 1; i <= length1; ++i) { for (int j = 1; j <= length2; ++j) { if (s1[i-1] == s2[j-1]){ MaxLen[i][j] = MaxLen[i-1][j - 1] + 1; } else { MaxLen[i][j] = max(MaxLen[i - 1][j], MaxLen[i][j - 1]); } } } return MaxLen[length1][length2];} int main(){ string s; while (cin >> s){ int length = s.size(); if (length == 1){ cout << 1 << endl; continue; } //利用回文串的特点 string s2 = s; reverse(s2.begin(),s2.end()); int max_length = maxLen(s, s2); cout << length - max_length << endl; } return 0;}
0 0
- 删除最少字符,得到回文串
- 删除最少字符 使字符串成为回文串
- 插入最少字符成为回文串
- 插入最少字符使原串变成回文串
- 最少需要删除多少个字符才能使字符串S变为回文串
- 面试题94:删除最少字符,使字符串为回文串
- 给定字符串,删除最少的字符,使剩下的字符串成为回文串
- 腾讯实习题---删除最少字符成为回文
- 交换最少次数得到回文
- STL学习-字符串回文判断以及实现删除字符串中重复字符最少字符
- 贪心两题(活动安排问题+删除最少字符得到目标字符)
- 区间dp-添加最少字符成为回文串
- 源字符串插入最少字符生成回文串
- dp问题 添加最少的字符构成回文串
- 插入最少的字符使字符串成为回文串
- Uva11584 最少回文串
- 判断字符串是否可以通过交换相邻字符得到回文字符串,如果可以输出最少交换次数
- 插入最少的字符使字符串成为回文串 <最长公共子序列 + 回文>
- 剑指Offer(5)______用两个栈实现队列
- Ubuntu16.04下安装pillow
- Spring JPA-Hibernate报错:com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an er解决方案
- pandas基本操作函数
- Ubuntu14.04+CUDA8.0+Anaconda2+Python2.7下编译Caffe(详细教程)
- 删除最少字符,得到回文串
- 队列queue中的常用函数 c++ 容器
- unix网络编程时关于字节序的问题
- 学习Hadoop笔记之一
- CSS基础
- 1.MVC架构概述
- 合并排序的两个链表
- STM32 PWM捕获
- 【PTA 5-10 树的遍历 (25分)】+ 二叉树