首页 > 其他分享 >Palindrome

Palindrome

时间:2023-04-20 18:03:28浏览次数:43  
标签:5010 palindrome string int Palindrome program characters


Palindrome

Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 72   Accepted Submission(s) : 19


Problem 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


/*
解题思路::
		要求的是加几个字母组成回文字符串,首先将其翻转,存入一新数组中
		用LCS计算其最长公共子序列,再用字符长度减去最长公共子序列即为所求。 

*/
#include<stdio.h>
#include<string.h>
#define max(a,b) a>b?a:b
char a[5010],b[5010];
int p[2][5010];
int main(){
	int i,j,n;
	while(scanf("%d",&n)!=EOF)
	{		
		scanf("%s",a);
		for(i=n-1,j=0;i>=0;i--)
			b[j++]=a[i];
		memset(p,0,sizeof(p));
		for(i=1;i<=n;i++)
			for(j=1;j<=n;j++)
			{
				
				if(a[i-1]==b[j-1])
					p[i%2][j]=p[(i-1)%2][j-1]+1;
				else
					p[i%2][j]=max(p[(i-1)%2][j],p[i%2][j-1]);
			}
			printf("%d\n",n-p[n%2][n]);
	}
	return 0;
}

 

标签:5010,palindrome,string,int,Palindrome,program,characters
From: https://blog.51cto.com/u_16079508/6210101

相关文章

  • Codeforces Round #311 (Div. 2) E. Ann and Half-Palindrome (DP+字典树)
    题目地址:传送门先用dp求出所有的符合要求的半回文串,标记出来。然后构造字典树。然后再dfs一遍求出所有节点的子树和,最后搜一遍就能找出第k个来了。代码如下:#include<iostream>#include<string.h>#include<math.h>#include<queue>#include<algorithm>#include<stdlib......
  • LightOJ - 1044 Palindrome Partitioning(DP)
    题目大意:给你一个字符串,要求你对字符串进行划分,使得划分出来的子串都是回文串,且子串数量达到最小解题思路:用dp[i]表示前i个字符划分成回文串,需要划分成多少个部分接着枚举j,如果[i,j]回文,那么dp[i]=min(dp[i],dp[j-1]+1)#include<cstdio>#include<cstring>#include<al......
  • 洛谷P1217 [USACO1.5]回文质数 Prime Palindromes
    #include<bits/stdc++.h>usingnamespacestd;inta,b;boolzs(intx){if(x%2>0){for(inti=3;i<x;i+=2)if(x%i==0)returnfalse;returntrue;}elsereturnfalse;}boolhws(intx......
  • 【APIO2014】Palindromes
    先说一下自己的SAM做法:看到回文串我们首先考虑对以下字符串建立SAM:正串+特殊字符1+特殊字符2+反串。这样也许能有一点用。晚上睡觉前我考虑的是对于正串的endpos在反串中......
  • CF245H Queries for Number of Palindromes
    对字符串s,多次询问,给你两个数L和R,问在字符串区间l到r的字串中,包含多少回文串。 #include<iostream>#include<queue>#include<cstring>#defineIOSstd::ios::syn......
  • Palindrome Linked List
    SourceGivenasinglylinkedlistofcharacters,writeafunctionthatreturnstrueifthegivenlistispalindrome,elsefalse.题解1-使用辅助栈根据栈的......
  • hdu-1513-Palindrome
    PalindromeTimeLimit:4000/2000MS(Java/Others)    MemoryLimit:65536/32768K(Java/Others)TotalSubmission(s):4052    AcceptedSubmission(s):1377......
  • 【题解】[ABC286C] Rotate and Palindrome 题解
    更好的阅读体验洛谷题目传送门|AT原题传送门思路观察题目可以发现A操作最多只能执行\(n\)次,超过以后字符串又会回到初始状态。首先考虑A操作如何实现,一种办法......
  • C - Rotate and Palindrome
    C-RotateandPalindromehttps://atcoder.jp/contests/abc286/tasks/abc286_c 思路从原始字符串开始,rotate第一次,rotate第二次,...,rotate最后一次对于每种情况......
  • 308. 最廉价的回文串 Cheapest Palindrome(挑战程序设计竞赛)
    地址https://www.papamelon.com/problem/308给定一个长度为m(m≤2000)的小写字母字符串,在给定组成该字符串的n(n≤26)个字符的添加和删除费用,求使原字符串变为......