POJ 1159 - Palindrome (DP 添加最少字符使s为回文串)

本文探讨了如何通过最少次数的字符插入将任意字符串转换为回文串的问题,并提供了两种解决思路:动态规划方法和利用最长公共子串长度计算。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Palindrome

Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 52290 Accepted: 18026

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



题意:

输入字符串s,求最少添加多少个字符,使得s是一个回文串。


思路:

一:

DP dp[i][j] 表示s[i...j]变为回文串最少需要添加的字符数,

dp[i][j] = min({if s[i]==s[j]: dp[i-1][j-1]}, dp[i+1][j]+1, dp[i][j-1]+1);


二:

设原序列S的逆序列为S'

最少需要补充的字母数 = 原序列S的长度 —  S和S'的最长公共子串长度


由于内存比较少,所以需要优化一下,因为对于当前i的值,来至于i和i+1,所以dp[2][maxn]就够了



#include <cstdio>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#include <string>
#include <map>
#include <cmath>
#include <queue>
#include <set>

using namespace std;

//#define WIN
#ifdef WIN
typedef __int64 LL;
#define iform "%I64d"
#define oform "%I64d\n"
#define oform1 "%I64d"
#else
typedef long long LL;
#define iform "%lld"
#define oform "%lld\n"
#define oform1 "%lld"
#endif

#define S64I(a) scanf(iform, &(a))
#define P64I(a) printf(oform, (a))
#define P64I1(a) printf(oform1, (a))
#define REP(i, n) for(int (i)=0; (i)<n; (i)++)
#define REP1(i, n) for(int (i)=1; (i)<=(n); (i)++)
#define FOR(i, s, t) for(int (i)=(s); (i)<=(t); (i)++)

const int INF = 0x3f3f3f3f;
const double eps = 10e-9;
const double PI = (4.0*atan(1.0));

const int maxn = 5000 + 20;
char str[maxn];
int dp[2][maxn];

int main() {
    int n;

    while(scanf("%d", &n) != EOF) {
        scanf("%s", str);
        memset(dp, 0, sizeof(dp));
        int ans = INF;
        for(int i=n-1; i>=0; i--) {
            for(int j=i; j<n; j++) {
                int c = i % 2;
                if(i == j) {
                    dp[c][j] = 0;
                    continue;
                }
                int t = dp[c^1][j] + 1;
                if(str[i] == str[j]) t = min(t, dp[1^c][j-1]);
                t = min(t, dp[c][j-1] + 1);
                dp[c][j] = t;
            }
        }
        printf("%d\n", dp[0][n-1]);
    }

    return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值