1 #define _CRT_SECURE_NO_WARNINGS 2 #include <stdio.h> 3 #include <math.h> 4 #include <algorithm> 5 #include <stdlib.h> 6 #include <vector> 7 #include <map> 8 #include <queue> 9 #include <string> 10 #include <iostream> 11 #include <ctype.h> 12 #include <string.h> 13 #include <set> 14 #include <stack> 15 #include<functional> 16 using namespace std; 17 #define size 1001 18 #define maxn 1<<30 19 int dp[size]; 20 char a[size]; 21 /*dp[i]表示的是将前面i个字符切割回文字串时候需要切多少刀 22 然后将字符串从j处切开,字符串变成了 23 1..j j+1..i 24 能切割的条件是j+1 到i是回文的 25 */ 26 bool isSymmetry(int s, int e){ 27 while (a[s] == a[e]&&s<=e){ 28 s++, e--; 29 } 30 if (s <= e) return false; 31 return true; 32 } 33 34 int main(){ 35 int T; 36 cin >> T; 37 while (T--){ 38 scanf("%s", a + 1); 39 int len = strlen(a+1); 40 for (int i = 1; i <= len; i++){ 41 dp[i] = isSymmetry(1, i) ? 0 : i - 1; 42 for (int j = 1; j < i; j++) 43 { 44 if (isSymmetry(j + 1, i)) dp[i] = min(dp[j] + 1, dp[i]); 45 } 46 } 47 cout << dp[len] << endl; 48 } 49 system("pause"); 50 return 0; 51 }