标签:des style blog io 使用 ar for strong sp
| Time Limit: 3000MS | Memory Limit: 65536K | |
| Total Submissions: 53414 | Accepted: 18449 |
Description
Input
Output
Sample Input
5 Ab3bd
Sample Output
2
Source
使用short的5000*5000的数组
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
short dp[5100][5100] ;
char str1[5100] , str2[5100] ;
int main()
{
int i , j , n ;
while(scanf("%d", &n) !=EOF)
{
scanf("%s", str1);
for(i = 0 ; i < n ; i++)
str2[n-1-i] = str1[i] ;
str2[i] = '\0' ;
for(i = 1 ; i <= n ; i++)
for(j = 1 ; j <= n ; j++)
{
if( str1[i-1] == str2[j-1] )
dp[i][j] = dp[i-1][j-1]+1 ;
else
dp[i][j] = max( dp[i-1][j],dp[i][j-1] );
}
printf("%d\n", n-dp[n][n]);
}
return 0;
}
使用滚动数组
滚动数组:使用两行数组,模拟大的二维数组
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int dp[2][5100] ;
char str1[5100] , str2[5100] ;
int main()
{
int i , j , n , k ;
while(scanf("%d", &n) !=EOF)
{
scanf("%s", str1);
for(i = 0 ; i < n ; i++)
str2[n-1-i] = str1[i] ;
str2[i] = '\0' ;
k = 0 ;
for(i = 1 ; i <= n ; i++)
{
k = 1 - k ;
for(j = 1 ; j <= n ; j++)
{
if( str1[i-1] == str2[j-1] )
dp[k][j] = dp[1-k][j-1]+1 ;
else
dp[k][j] = max( dp[1-k][j],dp[k][j-1] );
}
}
printf("%d\n", n-dp[k][n]);
}
return 0;
}
poj1159--Palindrome(dp:最长公共子序列变形 + 滚动数组)
标签:des style blog io 使用 ar for strong sp
原文地址:http://blog.csdn.net/winddreams/article/details/40154867