UV problem 10029 edit step ladders (progressive step)

Source: Internet
Author: User
Tags dot net
// Edit step ladders (progressive step) // PC/Ultraviolet IDs: 110905/10029, popularity: B, success rate: Low Level: 3 // verdict: accepted // submission date: 2011-09-30 // UV Run Time: 2.108 S // copyright (c) 2011, Qiu. Metaphysis # Yeah dot net // [Problem description] // edit step refers to adding, removing, or changing a letter in Word X, turn it into another word "Y" in the dictionary. // For example, if dig is changed to dog and dog is changed to do. The Edit step ladder is a dictionary-based word sequence W1, W2 ,..., wn, for all I from 1 to n-1, the word WI to W (I + 1) // is a progressive change. //// Provides a dictionary. You need to calculate the longest progressive step. //// [Input] // The input file is a dictionary: each line is a word consisting of lowercase letters. All words are alphabetically arranged. All words can be up to 16 characters in length and contain up to words in the dictionary. //// [Output] // output an integer that represents the maximum number of words contained in the progressive step. //// [Sample input] // cat // Dig // dog // fig // fin // fine // fog // log // wine //// [sample output] // 5 /// [solution] // Lexicographic Order is required for output, if you model a directed acyclic graph, you can obtain a directed acyclic graph. The problem is to find the longest path // path in the directed acyclic graph. This problem can also be attributed to the longest increasing subsequence (LIS) problem. //// The question indicates that an incremental word sequence is required. Because the given input is sorted in order, you only need to consider the relationship between the current word and the read word, if the number of words currently read is W and its length is l, W may only be L-1, l, L + 1 Word Structure // a progressive relationship (when l = 1, it can only constitute a progressive relationship with L, L + 1 words ). Due to the output Lexicographic Order requirements, only the words y whose dictionary // order is less than W and whose length meets the requirements are considered. Therefore, we can start from W to construct a progressive X to meet the length requirements, and find out whether // X is in the read-in word. If so, we can take out its maximum progressive length and Add 1, compared with the current progressive length of W, if it is greater than, it can be // replaced. Otherwise, the next progressive word is constructed. repeat this process and record the maximum progressive length found in it. The preceding algorithm uses the simple dynamic // Planning Idea to calculate Lis. the time complexity is O (n ^ 2 ). //// Appendix: Check whether the running time of many people in the ranking list is 0 s. It is estimated that there is only one input file, as long as there is a // program that can be passed, you can use the binary method to continuously submit and try out the final result. I also tried it, the maximum progressive length of the data dictionary on the tested data on the ultraviolet A is 45. # Include <iostream> # include <string> # include <map> using namespace STD; # define maxn 25000 # define maxlength 16 # define init_length 1map <string, int> dict [maxlength + 1]; // separate words of different lengths to improve the search speed. Int steps [maxn]; // store the maximum progressive length of the corresponding word. Int exist (INT index, string & temp, int step) {Map <string, int >:: iterator it; it = dict [Index]. find (temp); If (it! = Dict [Index]. end () if (steps [(* it ). second] + 1)> step) step = steps [(* it ). second] + 1; return step;} int main (int ac, char * AV []) {string line, temp; int current = 0; int maximum = 0; // initialize the progressive length of all words to 1. For (int c = 0; C <maxn; C ++) steps [c] = init_length; while (CIN> line) {int Index = line. length ()-1; int step = 1; // The build length is L-1, l, l + 1, and the lexicographically less than the current word, // word that forms a progressive relationship with the current word. The longest ascending sequence length is found and compared. If the current word length is 1, you only need to construct a variable word with a length of 1. If (index> 0) {for (INT I = 0; I <= index; I ++) {temp = line; if (I = index | temp [I]> = temp [I + 1]) {temp. erase (temp. begin () + I); step = exist (index-1, temp, step);} temp = line; while (temp [I]> 'A ') {temp [I] --; step = exist (index, temp, step);} temp = line; temp. insert (temp. begin () + I, line [I]); While (temp [I]> 'A') {temp [I] --; step = exist (index + 1, temp, step) ;}} else {temp = line; while (temp [0]> 'A') {temp [0] --; step = exist (index, temp, step) ;}// obtain the maximum value. If (Step> maximum) Maximum = step; // Save the current result. Steps [current] = step; dict [Index]. insert (make_pair <string, int> (line, current); current ++;} cout <maximum <Endl; return 0 ;}

 

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.