Partitioning by Palindromes UVA - 11584
2017-08-03 18:54
363 查看
https://vjudge.net/problem/UVA-11584
问分几组(最小)回文串。
dp[i]表示以i结尾的最小组数
先判断以 i 结尾的字符串是否是回文串,然后dp记录
#include <iostream>
#include <cstdio>
#include <string.h>
using namespace std;
char s[1005];
int dp[1005];
bool check(int l,int r){
for( int i = l , j = r ; i < j ; i++,j-- ){
if( s[i] != s[j] ) return false;
}
return true;
}
int main(){
int n;
cin>>n;
while( n-- ){
scanf("%s",s+1);
memset(dp,0,sizeof(dp));
int len = strlen(s+1);
for( int i = 1 ; i <= len ; i++ ){
dp[i] = i ; //初始化为串原长
for( int j = 1 ; j <= i ; j ++ ){
if( check(j,i) )
dp[i] = min( dp[i] , dp[ j - 1 ] + 1 );
}
}
printf("%d\n",dp[len]);
}
return 0;
}
问分几组(最小)回文串。
dp[i]表示以i结尾的最小组数
先判断以 i 结尾的字符串是否是回文串,然后dp记录
#include <iostream>
#include <cstdio>
#include <string.h>
using namespace std;
char s[1005];
int dp[1005];
bool check(int l,int r){
for( int i = l , j = r ; i < j ; i++,j-- ){
if( s[i] != s[j] ) return false;
}
return true;
}
int main(){
int n;
cin>>n;
while( n-- ){
scanf("%s",s+1);
memset(dp,0,sizeof(dp));
int len = strlen(s+1);
for( int i = 1 ; i <= len ; i++ ){
dp[i] = i ; //初始化为串原长
for( int j = 1 ; j <= i ; j ++ ){
if( check(j,i) )
dp[i] = min( dp[i] , dp[ j - 1 ] + 1 );
}
}
printf("%d\n",dp[len]);
}
return 0;
}
相关文章推荐
- uva 11584 Partitioning by Palindromes(dp)
- 区间DP UVA 11584 Partitioning by Palindromes
- UVA - 11584 Partitioning by Palindromes
- UVa 11584 Partitioning by Palindromes【DP】
- uva 11584——Partitioning by Palindromes
- UVA 11584 Partitioning by Palindromes
- uva 11584 Partitioning by Palindromes 线性dp
- UVa11584 - Partitioning by Palindromes(动态规划)
- UVa 11584 - Partitioning by Palindromes
- UVA 11584 Partitioning by Palindromes(切割最小回文)
- Partitioning by Palindromes UVA - 11584 (LIS/DP)
- Uva11584 Partitioning by Palindromes
- Uva 11584 Partitioning by Palindromes(区间dp)
- UVa 11584 Partitioning by Palindromes
- uva11584 Partitioning by Palindromes
- UVA 11584 - Partitioning by Palindromes
- UVA-11584-Partitioning by Palindromes(区间DP 最小回文串数量)
- UVa OJ 11584 - Partitioning by Palindromes
- UVa 11584 - Partitioning by Palindromes 回文串dp
- UVA - 11584 Partitioning by Palindromes