**回文串

Generating Palindromes
Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu

Description

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

6

abcd

aaaa

abc

aab

abababaabababa

pqrsabcdpqrs

Sample Output

Case 1: 3

Case 2: 0

Case 3: 2

Case 4: 1

Case 5: 0

Case 6: 9

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int main()
{
int n,k=0;
char s[110],b[110];
int map[110][110];
while(~scanf("%d",&n))
{
while(n--)
{
scanf("%s",s+1);
int l=strlen(s+1);
//初始化 
for(int i=0;i<=l;i++)
map[i][0]=0;
for(int j=0;j<=l;j++)
map[0][j]=0;
for(int i=1;i<=l;i++)
b[i]=s[l-i+1];//逆序存储
int t=-1;
for(int i=1;i<=l;i++)
{
	for(int j=1;j<=l;j++)
	{
		map[i][j]=0;
		map[i][j]=max(map[i][j],map[i-1][j]);
		map[i][j]=max(map[i][j],map[i][j-1]);
		if(s[i]==b[j])
		map[i][j]=map[i-1][j-1]+1;
		t=max(t,map[i][j]);//最多相同的数 
	}
	}	
	printf("Case %d: %d\n",++k,l-t);
}	
}
return 0;	
} 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值