您的位置:首页 > 其它

LightOJ 1033 - Generating Palindromes 【区间DP】

2015-06-10 15:56 441 查看
题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1033

题意:至少添加几个字符,能使得给定的串变为回文串。

解法:枚举起点终点,进行DP;

代码:

[code]#include <stdio.h>
#include <ctime>
#include <math.h>
#include <limits.h>
#include <complex>
#include <string>
#include <functional>
#include <iterator>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <list>
#include <bitset>
#include <sstream>
#include <iomanip>
#include <fstream>
#include <iostream>
#include <ctime>
#include <cmath>
#include <cstring>
#include <cstdio>
#include <time.h>
#include <ctype.h>
#include <string.h>
#include <assert.h>

using namespace std;

int t;
char s[1010];
int dp[1010][1010]; 

int main()
{
    scanf("%d",&t);
    for (int ca = 1; ca <= t;ca++)
    {
        memset(dp,0,sizeof(dp));
        scanf("%s",s+1);
        int n = strlen(s+1);

        for (int i = n; i >= 1; i--)
            for (int j = i+1; j <= n; j++)
            {
                if (s[i] == s[j])
                    dp[i][j] = dp[i + 1][j - 1];
                else
                    dp[i][j] = min(dp[i+1][j],dp[i][j-1]) + 1;
            }
        printf("Case %d: %d\n",ca,dp[1]
);
    }
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: