bailian4122
题意 就是切最少刀使得切出来的串都是回文串
我们设置dp[i] 代表 1 - i 表示的字符串切成回文串的代价
那么一个串如果本身就是回文串 那么代价就是 0
否则最坏代价是 len - 1 切成 len 个独块
于是我们枚举 1 - i 每个位置 j 切一刀
只要后面的 j + 1 到 i 是回文串
那么这刀就是可行的 dp[i] = min(dp[i],dp[j]+1)
/*
if you can't see the repay
Why not just work step by step
rubbish is relaxed
to ljq
*/
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <cmath>
#include <map>
#include <stack>
#include <set>
#include <sstream>
#include <vector>
#include <stdlib.h>
#include <algorithm>
using namespace std;
#define dbg(x) cout<<#x<<" = "<< (x)<< endl
#define dbg2(x1,x2) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<endl
#define dbg3(x1,x2,x3) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<" "<<#x3<<" = "<<x3<<endl
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
typedef pair<int,int> pll;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int _inf = 0xc0c0c0c0;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const ll _INF = 0xc0c0c0c0c0c0c0c0;
const ll mod = (int)1e9+7;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll ksm(ll a,ll b,ll mod){int ans=1;while(b){if(b&1) ans=(ans*a)%mod;a=(a*a)%mod;b>>=1;}return ans;}
ll inv2(ll a,ll mod){return ksm(a,mod-2,mod);}
void exgcd(ll a,ll b,ll &x,ll &y,ll &d){if(!b) {d = a;x = 1;y=0;}else{exgcd(b,a%b,y,x,d);y-=x*(a/b);}}//printf("%lld*a + %lld*b = %lld\n", x, y, d);
/*namespace sgt
{
#define mid ((l+r)>>1)
#undef mid
}*/
const int MAX_N = 1025;
int dp[MAX_N];
char str[MAX_N];
bool check(int st,int ed)
{
while(st<=ed)
{
if(str[st]==str[ed]) st++,ed--;
else return false;
}
return true;
}
int main()
{
//ios::sync_with_stdio(false);
//freopen("a.txt","r",stdin);
//freopen("b.txt","w",stdout);
int t;
scanf("%d",&t);
while(t--)
{
scanf("%s",str+1);
int len = strlen(str+1);
for(int i = 1;i<=len;++i)
{
dp[i] = check(1,i)?0:(i-1);
for(int j = 1;j<i;j++)
if(check(j+1,i)) dp[i] = min(dp[i],dp[j]+1);
}
printf("%d\n",dp[len]);
}
//fclose(stdin);
//fclose(stdout);
//cout << "time: " << (long long)clock() * 1000 / CLOCKS_PER_SEC << " ms" << endl;
return 0;
}

该博客探讨了如何用最少的切割次数将一个字符串分割成多个回文串的问题。通过动态规划(dp)方法,计算1到i位置上能形成回文串的最小代价。若字符串本身为回文,则代价为0;否则,最坏情况下需切len-1刀。博主枚举每个位置j,当j+1到i是回文串时,更新dp[i]的值为min(dp[i],dp[j]+1)。"
123622441,7847085,Laravel框架的模块化开发实践与解析,"['PHP', 'Laravel', '框架', '模块化开发']

392

被折叠的 条评论
为什么被折叠?



