题目大意:

    给你一个字符串,问有多少种方法删除字符,使得剩下的字符是回文串。
有几个规定:
1.空串不是回文串
2.剩下的字符位置不同也被视为不同的回文串。如:AA有三种回文串 A, A, AA
================================================================================================
 
#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;
char str[MAXN];
LL dp[MAXN][MAXN];
LL DFS(int L,int R)
{
    if(L > R) return 0;
    if(dp[L][R]) return dp[L][R];
    if(L == R) return dp[L][R] = 1;

    dp[L][R] = DFS(L+1,R) + DFS(L,R-1) - DFS(L+1,R-1);
    if(str[L] == str[R])
        dp[L][R] += DFS(L+1, R-1) + 1;
    return dp[L][R];
}

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

    return 0;
}

 

相关文章:

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