关闭

POJ 1159

标签: poj
147人阅读 评论(0) 收藏 举报
分类:

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

题意:
给出一串字符串字母,问最少添加多少字母能把这个字符串变成回文串,相信对于回文串,大家都了解,所以就不赘述了*

思路:
也是看了大神的博客才做出来的,最主要的就是最少次数 = 字符串长度 - 字符串倒叙与 字符串正序的最长公共字串长度*

代码:


import java.util.Scanner;

public class Main{
    private static int num = 0;
    private static char[] array_a = new char[5005];
    private static char[] array_b = new char[5005];
    private static int[][] cc = new int[2][5005];
    public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    num = scanner.nextInt();
    String inputLine = scanner.next();
    array_a = inputLine.toCharArray();
    int data = 0;
    for (int i = inputLine.length() - 1; i >= 0; i--) {
        array_b[data++] = array_a[i];
        cc[0][i] = 0;
    }
    for (int j = 0; j < num; j++) {
        for (int i = 0; i < num; i++) {
        if (array_a[i] == array_b[j]) {
            cc[1][i + 1] = cc[0][i] + 1;
        } else {
            cc[1][i + 1] = cc[1][i] > cc[0][i + 1] ? cc[1][i]:cc[0][i + 1];
        }
        }
        for (int i = 1; i <= num; i++) {
        cc[0][i] = cc[1][i];
        }
    }
    System.out.println(num - cc[1][num]);
    scanner.close();
    }
}
0
0
查看评论

POJ1159-Palindrome

转载请注明出处:優YoU http://user.qzone.qq.com/289065406/blog/1300587979  设原序列S的逆序列为S' ,则这道题目的关键在于, 最少需要补充的字母数 = 原序列S的长度 —  S和S'的最长公共子...
  • lyy289065406
  • lyy289065406
  • 2011-07-31 12:18
  • 7054

OpenJudge noi 1159Maze(poj 2157)

Description Acm, a treasure-explorer, is exploring again. This time he is in a special maze, in which there are some doors (at most 5 doors, repres...
  • Loi_black
  • Loi_black
  • 2016-10-15 19:06
  • 490

杭电ACM1159

又是一道非常经典的动态规划问题:最长公共子序列。 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1159 题目大意:给出两个字符串,求出最长公共子序列的长度。 解题思路:本题求给出的两个字符串的最大公共子序列。首先要区分公共子字符串和公共子序列,...
  • Runner__1
  • Runner__1
  • 2015-10-30 14:03
  • 844

【杭电oj1159】Common Subsequence

Common Subsequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 34234&...
  • better_space
  • better_space
  • 2016-08-12 20:53
  • 159

HDU-1159-最长公共序列模板。。。

简单但总忘只能记录了。。。#include<cstdio> #include<cstring> #include<algorithm> #include<iostream> using namespace std; char s1[600],s2[60...
  • acblacktea
  • acblacktea
  • 2016-03-17 19:48
  • 190

指针逆序输出数组

输入代码: /* *Copyright (c)2014,烟台大学计算机与控制工程学院 *All rights reserved. *文件名称:sum123.cpp *作 者:林海云 *完成日期:2014年12月16日 *版 本 号:v2.0 * *问题描述:用指针逆序输出数组...
  • linhaiyun_ytdx
  • linhaiyun_ytdx
  • 2014-12-16 21:39
  • 1339

poj 1159

<br />PalindromeTime Limit: 3000MS Memory Limit: 65536KTotal Submissions: 36084 Accepted: 12184<br />Descrip...
  • fp_hzq
  • fp_hzq
  • 2011-05-05 10:39
  • 375

POJ 1159

原题网址:http://poj.org/problem?id=1159 题意:给一个字符串,求最少插入多少个字符可以使之变成回文串 题目分类:区间dp 状态转移方程:dp[i][j]=dp[i+1][j-1] (str[i]==str[j) dp[i][j]=mi...
  • u010621873
  • u010621873
  • 2014-08-03 17:32
  • 208

hdu1159(最长公共子序列)

坑的一笔,我脑残了,一直wa,原来的代码是从0开始遍历,这样if (a[i]==b[j])dp[i][j]=dp[i-1][[j-1]就会数组下标越界,所以经过修改才正确. 附AC代码: #include #include char a[505]; char b[505]; int dp[5...
  • fengsigaoju
  • fengsigaoju
  • 2016-02-23 22:04
  • 289

hdu1159 LCS模板题

LCS最长公共子序列模板题。。
  • guodongxiaren
  • guodongxiaren
  • 2014-08-06 22:41
  • 1394
    个人资料
    • 访问:112170次
    • 积分:1387
    • 等级:
    • 排名:千里之外
    • 原创:41篇
    • 转载:3篇
    • 译文:0篇
    • 评论:32条
    博客专栏
    最新评论