题目大意:
给你一个字符串,问最少增加几个字符使得这个字符串变为回文串。
 
=======================================================================================
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<queue>
#include<vector>
#include<map>
using namespace std;
typedef long long LL;
const int INF = 1e9+7;
const int MAXN = 255;
int dp[MAXN][MAXN];
char str[MAXN];
int DFS(int L,int R)
{
    if(dp[L][R] != -1) return dp[L][R];
    if(L >= R) return dp[L][R] = 0;

    dp[L][R] = INF;
    for(int i=L; i<=R; i++)
    {
        if(str[L] == str[i])
            dp[L][R] = min(dp[L][R], R - i + DFS(L+1,i-1));
        dp[L][R] = min(dp[L][R], DFS(L+1,i)+R-i+1);
    }
    return dp[L][R];
}

int main()
{
    int T, cas = 1, n;

    scanf("%d", &T);
    while(T --)
    {
        memset(dp, -1, sizeof(dp));
        scanf("%s", str);
        printf("Case %d: %d\n",cas++, DFS(0, strlen(str)-1));
    }

    return 0;
}

 

相关文章:

  • 2022-02-26
  • 2021-07-22
  • 2021-10-01
  • 2021-11-07
  • 2022-12-23
  • 2021-10-06
  • 2021-10-29
  • 2021-08-15
猜你喜欢
  • 2022-01-20
  • 2021-12-25
  • 2021-08-08
  • 2021-11-21
  • 2021-10-09
  • 2021-05-24
  • 2021-07-14
相关资源
相似解决方案