10739-string to Palindrome
Time limit:3.000 seconds
Http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=114&page=show_ problem&problem=1680
Idea: For each interval [I, J]:
If str[i] = = Str[j],dp[i][j] = dp[i + 1][j-1];
If Str[i]!= str[j], dp[i][j] = min (dp[i + 1][j], dp[i] [j-1], dp[i + 1] [j-1]) + 1 (Delete I, delete j, modify I to J (add and delete is the same))
Complete code:
/*0.045s*/
#include <bits/stdc++.h>
using namespace std;
const int N = 1005;
int dp[n][n];
Char Str[n];
int main ()
{
int cas, t = 0, len,i;
scanf ("%d\n", &cas);
while (cas--)
{
gets (str);
len = strlen (str);
memset (DP, 0, sizeof (DP));
for (i = len-2 i >= 0;-i)
{for
(int j = i + 1; j < Len; ++j)
{
if (str[i] = = Str[j]) dp[i] [j] = Dp[i + 1][j-1];///inward
else dp[i][j] = min (min (dp[i + 1][j), dp[i][j-1]), Dp[i + 1][j-1]) + 1;
Delete I, delete j, modify I to J (add and delete are the same)}
printf ("Case%d:%d\n", ++t, dp[0][len-1]);
}
return 0;
}
See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/