杭电1513(Palindrome)

2024-06-09 13:18
文章标签 杭电 palindrome 1513

本文主要是介绍杭电1513(Palindrome),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

点击打开杭电1513

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,用n减去所求的最长公共子序列的长度就是答案


代码:

import java.util.*;
class P1513{static String str;static char[] str1;static char[] str2;static int n;static int[][] dp;public static void main(String[] args){Scanner sc=new Scanner(System.in);while(sc.hasNext()){n=sc.nextInt();str2=new char[n];str1=new char[n];str=sc.next();str1=str.toCharArray();for(int i=0;i<n;i++){str2[i]=str1[n-i-1];}lcs();System.out.println(n-dp[n%2][n]);}}public static void lcs(){int i,j;dp=new int[n+1][n+1];for(i=1;i<=n;i++){for(j=1;j<=n;j++){int x=i%2;int y=1-x;if(str1[i-1]==str2[j-1]){dp[x][j]=dp[y][j-1]+1;}else{dp[x][j]=Math.max(dp[y][j], dp[x][j-1]);}}}}
}










这篇关于杭电1513(Palindrome)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1045280

相关文章

ural 1297. Palindrome dp

1297. Palindrome Time limit: 1.0 second Memory limit: 64 MB The “U.S. Robots” HQ has just received a rather alarming anonymous letter. It states that the agent from the competing «Robots Unli

【UVA】10739 - String to Palindrome(动态规划)

比较水的动态规划 dp[i][j] 将原串 i ~ j 之内的字符转化为回文字符所需要的最小操作次数 其中删除操作和添加操作本质上是一样的。 三个状态转移方程: dp[i][j] = min(dp[i][j] ,dp[i + 1][j]); dp[i][j] = min(dp[i][j] ,dp[i + 1][j - 1]); dp[i][j] = min(dp[i][j] ,dp[

leetCode#125. Valid Palindrome

Description Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. For example, “A man, a plan, a canal: Panama” is a palindrome. “race a car

[LeetCode] 409. Longest Palindrome

题:https://leetcode.com/problems/longest-palindrome/description/ 题目 Given a string which consists of lowercase or uppercase letters, find the length of the longest palindromes that can be built with

[LeetCode] 234. Palindrome Linked List

题:https://leetcode.com/problems/palindrome-linked-list/description/ 题目 Given a singly linked list, determine if it is a palindrome. Example 1: Input: 1->2Output: false Example 2: Input: 1->2->

2024杭电8

1004.cats 的重力拼图 题意: 有一个n*m的矩阵,给出最开始拼图的位置。 可以有四个选择,设置重力的方向,就是拼图会向一个方向竖直掉落到最底。 问任意操作次数后拼图走过的方格数量最大值。 题解: 首先已经在边缘的拼图,只能沿着边走一圈,再判断最开始可以朝哪个方向移动是最大值。 代码: #include<bits/stdc++.h>using namespace s

Count Palindrome in String

string 有多少palindrome substring。exp: 'aba' 返回4 , 'abba' 返回6 public class CountPalindrome {public int countPalindrome(String str){if(str == null || str.length() == 0) return 0;int count = 0;for(int

2024杭电6

1001.造花(简单版) 题意: 菊花图:n-1个节点都连接同一节点的树。 给定一棵树,删掉一个节点和连向这个点的所有边,使剩下两个连通块都构成菊花图,问是否可以做到。 题解: 菊花图只有中心节点的度可以没有限制,其余节点的度都是1。 要删除一个节点,要求剩下两个连通块,那就只能删掉度为2的节点,剩下两个菊花图,菊花图最多一个度不是1的节点。 所以度不是1的节点数最多为5,如图。

杭电 1297 Children’s Queue .

http://acm.hdu.edu.cn/showproblem.php?pid=1297   计算F(n): 一:当最后一个是男孩M时候,前面n-1个随便排出来,只要符合规则就可以,即是F(n-1); 二:当最后一个是女孩F时候,第n-1个肯定是女孩F,这时候又有两种情况:         1)前面n-2个可以按n-2个的时候的规则来,完全可以,即是F(n-2);

2014.1.13 杭电习题 绝对值排序

绝对值排序 Problem Description(问题描述) 输入n(n<=100)个整数,按照绝对值从大到小排序后输出。题目保证对于每一个测试实例,所有的数的绝对值都不相等。 Input(输入) 输入数据有多组,每组占一行,每行的第一个数字为n,接着是n个整数,n=0表示输入数据的结束,不做处理。 Output(输出) 对于每个测试实例,输出排序后的结果,两个数之间用一个