zoukankan      html  css  js  c++  java
  • 1044

    题目大意:
    给你一个字符串,问这个字符串最少有多少个回文串。
    区间DP直接搞
     
     
     
    #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 = 1055;
    int dp[MAXN][MAXN];
    bool P[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] = 1;
    
        dp[L][R] = INF;
        if(P[L][R]) dp[L][R] = 1;
        for(int i=L; i<=R; i++)
        {
            if(P[L][i])
                dp[L][R] = min(dp[L][R], 1 + DFS(i+1,R));
        }
        return dp[L][R];
    }
    
    bool ok(int L,int R)
    {
        for(int i=L,j=R; i<=j; i++, j--)
        {
            if(str[i] != str[j])
                return false;
        }
        return true;
    }
    
    
    int main()
    {
        int T, cas = 1;
        scanf("%d", &T);
        while(T --)
        {
            memset(dp, -1, sizeof(dp));
            scanf("%s", str);
            int len = strlen(str) - 1;
            for(int i=0; i<=len; i++)
            for(int j=i; j<=len; j++)
                P[i][j] = ok(i, j);
    
            printf("Case %d: %d
    ",cas ++, DFS(0, len) );
        }
    
        return 0;
    }
  • 相关阅读:
    deepin 系统更新命令
    安装mongdb
    读model所得
    上周某一天
    在项目中直接执行里面的文件
    数据库(六)
    数据库(五)
    数据库(四)
    数据库(三)
    数据库(二)
  • 原文地址:https://www.cnblogs.com/chenchengxun/p/4914772.html
Copyright © 2011-2022 走看看