博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ 1159, Palindrome
阅读量:6500 次
发布时间:2019-06-24

本文共 1769 字,大约阅读时间需要 5 分钟。

Time Limit: 3000MS  Memory Limit: 65536K

Total Submissions: 26714  Accepted: 8915

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

 

Source

IOI 2000


//
 POJ1159.cpp : Defines the entry point for the console application.
//
#include 
<
iostream
>
#include 
<
algorithm
>
using
 
namespace
 std;
int
 main(
int
 argc, 
char
*
 argv[])
{
    
int
 N;
    scanf(
"
%d\n
"
&
N);
    
char
 word[
5001
];
    gets(word);
    
int
 DP[
2
][
5001
];
    memset(DP,
0
,
sizeof
(DP));
    
for
 (
int
 i 
=
 
1
; i 
<=
 N; 
++
i)
    {
        DP[i 
&
 
1
][
0
=
 
0
;
        
for
 (
int
 j 
=
 
1
; j 
<=
 N; 
++
j)
        {
            
if
 (word[i
-
1
]
==
word[N
-
j])DP[i 
&
 
1
][j] 
=
 DP[(i
-
1
&
 
1
][j
-
1
+
 
1
;
            
else
 DP[i 
&
 
1
][j] 
=
 max(DP[(i
-
1
&
 
1
][j], DP[i 
&
 
1
][j
-
1
]);
        }
    }
    
    cout 
<<
 N 
-
 DP[N
&
1
][N] 
<<
 endl;
    
return
 
0
;
}

转载于:https://www.cnblogs.com/asuran/archive/2009/10/14/1583117.html

你可能感兴趣的文章
PHP学习笔记 第八讲 Mysql.简介和创建新的数据库
查看>>
【git】git入门之把自己的项目上传到github
查看>>
js获取鼠标位置
查看>>
2016.8.11 DataTable合并及排除重复方法
查看>>
php 魔术方法 说明
查看>>
Mysql
查看>>
POJ-1860-Currency Exchange
查看>>
跨越企业的“中等收入陷阱”
查看>>
Android 开发者必知的开发资源
查看>>
jackson 常见问题
查看>>
软件工程技术基础-(软件复用技术)
查看>>
给django视图类添加装饰器
查看>>
.vimrc文件
查看>>
DVWA默认用户名密码
查看>>
简述 clearfix 的原理
查看>>
【Project Euler】530 GCD of Divisors 莫比乌斯反演
查看>>
luogu P1280 尼克的任务 序列DP
查看>>
Android 实时文件夹
查看>>
获取文件最后修改时间的VC代码
查看>>
适用于0基础小伙伴的HTML知识点总结 先到先得哟
查看>>