码迷,mamicode.com
首页 > 其他好文 > 详细

Palindrome

时间:2015-08-10 22:20:54      阅读:169      评论:0      收藏:0      [点我收藏+]

标签:

Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 56312   Accepted: 19468

Description

A palindrome is a symmetrical string, that is, a string read identically from left to right as well as from right to left. You are to write a program which, given a string, determines the minimal number of characters to be inserted into the string in order to obtain a palindrome. 

As an example, by inserting 2 characters, the string "Ab3bd" can be transformed into a palindrome ("dAb3bAd" or "Adb3bdA"). However, inserting fewer than 2 characters does not produce a palindrome. 

Input

Your program is to read from standard input. The first line contains one integer: the length of the input string N, 3 <= N <= 5000. The second line contains one string with length N. The string is formed from uppercase letters from ‘A‘ to ‘Z‘, lowercase letters from ‘a‘ to ‘z‘ and digits from ‘0‘ to ‘9‘. Uppercase and lowercase letters are to be considered distinct.

Output

Your program is to write to standard output. The first line contains one integer, which is the desired minimal number.

Sample Input

5
Ab3bd

Sample Output

2

该题的大意是给你一个字符串,最少增加多少个字符能把它变成回文串,其实不是那么难,只要用该字符串的总长度减去该字符串正向与反向最长的公共子序列的差,即为所求的值。


刚开始定义
<span style="color:#ff0000;">int x[5005][5005];提交时超内存,后来改为short型在(二字节),在poj上提交时过了,在杭电上提交时,还是超内存,把short型改为char型,但是提交是错了,原来char是一字节,最大能存的数是255<img alt="尴尬" src="http://static.blog.csdn.net/xheditor/xheditor_emot/default/awkward.gif" /><img alt="惊讶" src="http://static.blog.csdn.net/xheditor/xheditor_emot/default/ohmy.gif" /><img alt="安静" src="http://static.blog.csdn.net/xheditor/xheditor_emot/default/quiet.gif" /><img alt="再见" src="http://static.blog.csdn.net/xheditor/xheditor_emot/default/bye.gif" /></span>

这个代码只能在poj系统上能过,在hdoj上不能过,因为在杭电上超内存。
#include<stdio.h>
#include<string.h>
#define max(a,b) a>b?a:b
char a[5005],b[5005];
short x[5005][5005];
int main()
{
	int i,j,k,n;
	while(scanf("%d%s",&n,a)!=EOF)
	{
		int len=strlen(a);
		j=0;
		for(i=n-1;i>=0;i--)
		b[j++]=a[i];
		memset(x,0,sizeof(0));
		for(i=1;i<=len;i++)
		{
			for(j=1;j<=len;j++)
			{
				if(a[i-1]==b[j-1])
				x[i][j]=x[i-1][j-1]+1;
				else x[i][j]=max(x[i][j-1],x[i-1][j]);
			}
		}
		//int temp=-'0';
		printf("%d\n",len-x[len][len]);
	}
	return 0;
}


再提供一个代码,在两个系统上都能ac技术分享
#include<stdio.h>
#include<string.h>
#define max(a,b) a>b?a:b
char a[5019],b[5019];
int x[3][5019];
int main()
{
	int i,j,k,n;
	while(scanf("%d%s",&n,a)!=EOF)
	{
		j=0;
		for(i=n-1;i>=0;i--)
		b[j++]=a[i];
		memset(x,0,sizeof(x));
		for(i=1;i<=n;i++)
		{
			for(j=1;j<=n;j++)
			{
				if(a[i-1]==b[j-1])
				x[i%2][j]=x[(i-1)%2][j-1]+1;
				else x[i%2][j]=max(x[i%2][j-1],x[(i-1)%2][j]);
			}
		}
		printf("%d\n",n-x[n%2][n]);
	}
	return 0;
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

Palindrome

标签:

原文地址:http://blog.csdn.net/l15738519366/article/details/47403761

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!