https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2631
划分字符串,看最少能划分几个回文串动态规划
#include <iostream> #include <algorithm> #include <cstring> #include <cstdio> #include <vector> #include <queue> #include <cstdlib> #include <iomanip> #include <cmath> #include <ctime> #include <map> #include <set> using namespace std; #define lowbit(x) (x&(-x)) #define max(x,y) (x>y?x:y) #define min(x,y) (x<y?x:y) #define MAX 100000000000000000 #define MOD 1000000007 #define pi acos(-1.0) #define ei exp(1) #define PI 3.141592653589793238462 #define ios() ios::sync_with_stdio(false) #define INF 0x3f3f3f3f #define mem(a) (memset(a,0,sizeof(a))) typedef long long ll; int n,dp[1006],t; char s[1006]; int main() { ios(); scanf("%d",&t); while(t--) { scanf("%s",s+1); n=strlen(s+1); for(int i=0;i<=1003;i++) dp[i]=1009; dp[0]=0; for(int i=1;i<=n;i++) { for(int j=1;j<=i;j++) { int ok=1; int mid=(i+j)>>1; for(int k=j;k<=mid;k++) { if(s[k]!=s[i-k+j]) { ok=0; break; } } if(ok) dp[i]=min(dp[i],dp[j-1]+1); } } printf("%d ",dp[n]); } return 0; } /* 6 racecar factcar fastcar aaadbccb aaaaaaaa abbacdefghiklcccliopap */