Partitioning by Palindromes
作者:互联网
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define itn int
#define retrun return
#define _memset(a) memset((a), 0, sizeof((a)))
int dp[1010];
char s[1010];
bool judge(int l, int r) {
while (l < r) {
if (s[l] != s[r]) return 0;
l++;
r--;
}
return 1;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int t;
cin >> t;
while (t--) {
cin >> (s + 1);
int len = strlen(s + 1);
for (int i = 1; i <= len; i++) {
dp[i] = i;
for (int j = 0; j <= i; j++) {
if (judge(j + 1, i)) dp[i] = min(dp[j] + 1, dp[i]);
}
}
cout << dp[len] << endl;
}
//system("pause");
return 0;
}
标签:Partitioning,Palindromes,return,int,memset,cin,long,define 来源: https://blog.csdn.net/qq_61908339/article/details/121939269