UVA 10029 Edit Step Ladders (DP)

Source: Internet
Author: User
Tags strcmp

Problem C:edit Step Ladders

An edit "is a transformation" from "one word X to another word y such" x and y are words in the dictionary, and x CA n is transformed to Y by adding, deleting, or changing one letter. So the transformation from dig to dog or from dog to did are edit both. An edit step Ladder is a lexicographically ordered sequence of words W1, w2, ... wn such that the transformation from WI t o wi+1 is a edit step for all I from 1 to n-1.

For a given dictionary, your are to compute the length of the longest edit step ladder.

Input

The input to your program consists of the dictionary-a set of lower case words in lexicographic order-one per line. No word exceeds letters and there are no more than 25000 in the words.

Output

The output consists of a single integer, the number of words in the longest edit step ladder.

Sample Input

Cat
dig
dog
Fig
fin
fine
fog
Log
wine

Sample Output

5

Given a dictionary, if deleted, add or replace can be converted into another word, then these two words for the ladder change, find the longest step changes in the dictionary.

Idea: DP. Lis, with the n^2 timeout ... And then don't want to understand the solution, found that there are N (Logn) methods.

All the words can be transformed out of the case, and then in front of the 2-point search, because the sequence itself is a dictionary order is feasible. So we can get through this.

Code:

 #include <stdio.h> #include <string.h> int max (int a, int b) {return a > B. a:b} int min (int a, I
NT B) {return a < b a:b;} const int N = 25555, M = 20;
Char Word[n][m], str[m];
  
int n, I, J, K, L, dp[25555], ans;
    void Change (char *word, Char *str, char c, int wei) {int i;
    for (i = 0; word[i]; i + +) str[i] = Word[i];
    Str[wei] = c;
  
Str[i] = ' the ';
    } void del (char *word, char *str, int wei) {int i;
    for (i = 0; I < Wei i + +) str[i] = Word[i];
    for (i = wei + 1; word[i]; i + +) str[i-1] = Word[i];
Str[i-1] = ' the ';
    } void Add (char *word, Char *str, char c, int wei) {int i;
    for (i = 0; I < Wei i + +) str[i] = Word[i];
    Str[wei] = c;
    for (i = Wei; word[i]; i + +) Str[i + 1] = Word[i];
Str[i + 1] = ';
    } void Tra (char *word, Char *str, char c, int wei, int flag) {if (flag = 0) Change (Word, str, C, Wei); else if (flag = = 1) del (Word, str,Wei);
else Add (Word, str, C, Wei);
    int find (char *str, int j) {int i = 0;
    J--;
        while (I <= j) {int mid = (i + j)/2;
        if (strcmp (Word[mid], str) = = 0) {return mid;
        else if (strcmp (Word[mid], str) < 0) {i = mid + 1;
    else J = mid-1;
} return-1;
    int main () {while (gets (Word[n])) {n + +;
    Ans = 0;
        for (i = 0; i < n; i + +) {Dp[i] = 1;
                    for (k = 0; k < 3; k + +) {for (j = 0; word[i][j]; j + +) {for (L = 0; L < L + +) {
                    TRA (word[i], str, ' a ' + L, J, K);
                    if (strcmp (word[i], str) < 0) break;
                    int mid = find (str, i);
                If (mid >= 0) dp[i] = max (Dp[i], Dp[mid] + 1);
    ans = max (ans, dp[i]);
    printf ("%d\n", ans);
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.