| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
错了N次,看了网上牛人的解法,终于AC了(动态规划+滚动数组)#include <iostream>
#include <vector>
#include <string>
#include <cmath>
#include <algorithm>
#include <functional>
#include <list>
#include <map>
#include <set>
using namespace std;
char str[5001];
short DP[2][5001],len,i,j;
int main()
{
scanf("%d\n%s",&len,str+1);
for(i = 1;i <= len;++i)
for(j = 1;j <= len;++j)
{
if(str[i] == str[len-j+1])
DP[i%2][j] = DP[(i-1)%2][j-1] + 1;
else
DP[i%2][j] = DP[(i-1)%2][j]>DP[i%2][j-1] ? DP[(i-1)%2][j] : DP[i%2][j-1];
}
printf("%d\n",len-DP[len%2][len]);
system("pause");
return 0;
};
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator