切割回文 OpenJ_Bailian - 4122
阿福最近对回文串产生了非常浓厚的兴趣。
如果一个字符串从左往右看和从右往左看完全相同的话,那么就认为这个串是一个回文串。例如,“abcaacba”是一个回文串,“abcaaba”则不是一个回文串。
阿福现在强迫症发作,看到什么字符串都想要把它变成回文的。阿福可以通过切割字符串,使得切割完之后得到的子串都是回文的。
现在阿福想知道他最少切割多少次就可以达到目的。例如,对于字符串“abaacca”,最少切割一次,就可以得到“aba”和“acca”这两个回文子串。
Input
输入的第一行是一个整数 T (T <= 20) ,表示一共有 T 组数据。
接下来的 T 行,每一行都包含了一个长度不超过的 1000 的字符串,且字符串只包含了小写字母。
Output
对于每组数据,输出一行。该行包含一个整数,表示阿福最少切割的次数,使得切割完得到的子串都是回文的。
Examples
Sample Input
3
abaacca
abcd
abcba
Sample Output
1
3
0
Hint
题意:
注意切割回文是指切割完之后使得所有字符串都是回文串.
题解:
定义状态
d
p
[
i
]
:
前
i
个
字
符
的
最
小
切
割
回
文
dp[i]:前i个字符的最小切割回文
dp[i]:前i个字符的最小切割回文
inf表示不可完成, 初始化inf, dp[1] = 0
状态转移
d
p
[
i
]
=
m
i
n
(
d
p
[
j
−
1
]
+
1
)
(
1
<
=
j
<
=
i
且
S
[
i
.
.
j
]
是
回
文
串
)
dp[i] = min(dp[j-1]+1) (1<=j<=i且S[i..j]是回文串)
dp[i]=min(dp[j−1]+1)(1<=j<=i且S[i..j]是回文串)
经验小结:
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <stdlib.h>
#include <vector>
#include <queue>
#include <cmath>
#include <stack>
#include <map>
#include <set>
using namespace std;
#define ms(x, n) memset(x,n,sizeof(x));
typedef long long LL;
const int inf = 1<<30;
const LL maxn = 1010;
char s[maxn];
int dp[maxn]; //子串S[1-i]的最小切割回文
bool judge(int l, int r){
while(l <= r){
if(s[l] != s[r]) return false;
++l, --r;
}
return true;
}
int main()
{
int T;
scanf("%d",&T);
while(T--){
scanf(" %s",s+1);
int len = strlen(s+1);
for(int i = 0; i <= len; ++i) dp[i] = inf;
for(int i = 1; i <= len; ++i)
for(int j = 1; j <= i; ++j)
if(judge(j, i)){
if(j==1) dp[i] = 0; //S[1-i]的大回文
else dp[i] = min(dp[i], dp[j-1]+1);
}
printf("%d\n",dp[len]);
}
return 0;
}