UVa 10739 String to Palindrome (DP)

Source: Internet
Author: User
Tags cas min time limit

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/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.