poj 1159 Palindrome(dp)

题目:http://poj.org/problem?id=1159

 1 #include<iostream>
 2 #include<cstring>
 3 #include<cstdio>
 4 using namespace std;
 5 
 6 int mmin(int a,int b)
 7 {
 8     return a>b?b:a;
 9 }
10 short int d[5010][5010];
11 int main()
12 {
13     int n,i,j;
14     char s[5010];
15     memset(d,0,sizeof(d));
16     cin>>n;
17     for(i=1; i<=n; i++)
18     cin>>s[i];
19     for(i=n; i>=1; i--)
20     for(j=i+1; j<=n; j++)
21     if(s[i]==s[j])
22     d[i][j]=d[i+1][j-1];
23     else
24     d[i][j]=mmin(d[i+1][j],d[i][j-1])+1;
25 
26     cout<<d[1][n]<<endl;
27     return 0;
28 }

 

posted @ 2013-10-28 21:32  水门  阅读(149)  评论(0编辑  收藏  举报