Vijos1028. Demon Family Password

Source: Internet
Author: User

Topic Overview

The son of the wind just walked into his examination room, just ...

Flower: When I was a charm queen--flower!! ^^ (gorgeous appearance, salute, flowers)

Son of the Wind: I vomit ... (Kill the person's eyes) quick, say the question! otherwise ...-_-###

Flower:...... Ah ~ ~ So Cold ~ ~ We are now going to solve the demon clan password problem (narcissistic: Bad Demon clan inside will also someone use the password to me and the vegetable worm Write love letter, OH alive, of course, give me more than pull * ^_^ *). The Demon clan now uses a new cryptography system. Each password is a given list of English words that contain only lowercase letters, and each word contains at least 1 letters, up to 75 letters. If in a table consisting of one word or multiple words, except the last one, each word is contained by a word that follows, that is, the previous word is the prefix of the second word, and the word list is a chain of words. For example, the following words form a chain of words:

I

Int

Integer

But the following words do not form a word chain:

Integer

Intern

Now all you have to do is take some words out of a given list of words and make up the longest chain of words that contains the most words. Count the number of its words and get the password.

Back to the column page: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

The son of the Wind: the password is the longest word chain includes the number of words ah ...

Input

The first behavior word list in the number of words N (1<=n<=2000), each of the following lines have a word, in the dictionary order, there is no duplicate words in the middle.

Output

Output password on first line

Ideas for solving problems

This problem is the longest not descending subsequence. such as a sequence {3, 5, 6, 2, 8}, the longest not descending subsequence is {3, 5, 6, 8}.

So how do you find this sequence? This problem can be converted to, f (i) = max{f (j)} + 1. (where F (j) is the longest not descending subsequence in the first J number)

We need an array length[] to record the value of the longest not descending subsequence of the 1~n element.

For the first element, length[i] = max{Length[j]} + 1 (0 <= J < i), and the subsequence (WORDS[J) that is words[i] is the array that holds the word chain.

Problems encountered

Rare so smooth, no problems encountered, even O (n^2) code can also AC.

Source

#include <iostream> #include <fstream> #include <string> bool CompareString (const STD::STRI  
    ng& str1, const std::string& str2) {size_t i = 0, j = 0;  
            for (; i < Str1.size () && J < Str2.size (); + + I, + + j) {if (str1[i)!= Str2[j]) {  
        Break  
return (i = = str1.size () | | | j = = str2.size ());  
    int main () {using Std::cin;  
    Std::ifstream cin;  
     
    Cin.open ("Input.txt");  
    const int SIZE = 2000;  
    int n = 0;  
     
    std::string Words[size];  
    Input cin >> N;  
    for (int i = 0; i < n; + + i) {cin >> words[i];  
    }//processing int length[size] = {0};  
        for (int i = 0; i < n; + + i) {length[i] = 1; for (int j = 0; J < i; + + j) {if (CompareString (Words[i], words[j]) && Length[j] <= length  
               [i]) { Length[i] = length[j] + 1;  
    }}//Output int maxLength = 0;  
        for (int i = 0; i < n; + + i) {if (Length[i] > maxLength) {maxLength = Length[i];  
     
    } std::cout << maxLength << Std::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.