博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
light oj 1033 - Generating Palindromes 【LCS】
阅读量:6573 次
发布时间:2019-06-24

本文共 2681 字,大约阅读时间需要 8 分钟。

  
Time Limit: 2 second(s) Memory Limit: 32 MB

By definition palindrome is a string which is not changed when reversed. "MADAM" is a nice example of palindrome. It is an easy job to test whether a given string is a palindrome or not. But it may not be so easy to generate a palindrome.

Here we will make a palindrome generator which will take an input string and return a palindrome. You can easily verify that for a string of length n, no more than (n - 1) characters are required to make it a palindrome. Consider "abcd" and its palindrome "abcdcba" or "abc" and its palindrome "abcba". But life is not so easy for programmers!! We always want optimal cost. And you have to find the minimum number of characters required to make a given string to a palindrome if you are only allowed to insert characters at any position of the string.

Input

Input starts with an integer T (≤ 200), denoting the number of test cases.

Each case contains a string of lowercase letters denoting the string for which we want to generate a palindrome. You may safely assume that the length of the string will be positive and no more than 100.

Output

For each case, print the case number and the minimum number of characters required to make string to a palindrome.

Sample Input

Output for Sample Input

6

abcd

aaaa

abc

aab

abababaabababa

pqrsabcdpqrs

Case 1: 3

Case 2: 0

Case 3: 2

Case 4: 1

Case 5: 0

Case 6: 9

 


PROBLEM SETTER: MD. KAMRUZZAMAN

SPECIAL THANKS: JANE ALAM JAN (MODIFIED DESCRIPTION, DATASET)

题意:

给你一个字符串,问最少添加多少个字符,使原字符串变成回文串

思路:
回文串的性质是倒过来两者一样, 那么有这个性质可知, 设原始字符串为a,原始字符串颠倒过来的字符串为b,  那么求a和b的最长公共子序列, 字符串a的长度减去最长公共自序列的长度不就是最少添加的字符!

#include #include 
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
//#include
//#define LOACL#define space " "using namespace std;//typedef long long LL;//typedef __int64 Int;typedef pair
paii;const int INF = 0x3f3f3f3f;const double ESP = 1e-5;const double PI = acos(-1.0);const int MOD = 1e9 + 7;const int MAXN = 1e2 + 10;int dp[MAXN][MAXN];char str1[MAXN], str2[MAXN];int main(int argc, char* argv[]) { int T; scanf("%d", &T); int Kcase = 0; while (T--) { scanf("%s", str1); int len = strlen(str1); memset(dp, 0, sizeof(dp)); memset(str2, 0, sizeof(str2)); for (int i = len - 1; i >= 0; i--) { str2[len - i - 1] = str1[i]; } for (int i = 1; i <= len; i++) { for (int j = 1; j <= len; j++) { if (str1[i-1] == str2[j-1]) { dp[i][j] = dp[i-1][j-1]+1; } else dp[i][j] = max(dp[i-1][j], dp[i][j-1]); } } printf("Case %d: %d\n", ++Kcase, len - dp[len][len]); } return 0;}

 

 

 

 

转载于:https://www.cnblogs.com/cniwoq/p/6770788.html

你可能感兴趣的文章
网络布线线材用量计算公式
查看>>
查询当前数据库用户会话信息
查看>>
转:模态对话框的支持 (IE,Firefox,Chrome)
查看>>
Jenkins+QTP自动化测试框架
查看>>
《Node.js In Action》笔记之流程控制
查看>>
3518EV200 SDK学习1
查看>>
JavaScript初学者应注意的七个细节
查看>>
1163: 零起点学算法70——Yes,I can!
查看>>
2018-2019-2 网络对抗技术 20165318 Exp1 PC平台逆向破解
查看>>
关于图片或者文件在数据库的存储方式归纳
查看>>
存储过程和SQL语句比较及存储过程在C#中调用方法
查看>>
hihocoder 1014 Trie树
查看>>
ADO.NET笔记——使用DataSet返回数据
查看>>
【机器学习】--关联规则算法从初识到应用
查看>>
MOTO XT702添加开机音乐
查看>>
Python脚本日志系统
查看>>
B0BO TFS 安装指南(转载)
查看>>
gulp常用命令
查看>>
TCP(Socket基础编程)
查看>>
RowSet的使用
查看>>