P1481 inferno password (LIS deformation)

Title Description (topic Link: https://www.luogu.org/problem/P1481 )

Zonda just walked into his examination room, it ......

Flower: Dangdang Dangdang ~ ~ even is the charm of the Queen - Flower! ! ^^ (gorgeous appearance, gun salute, flowers)

Zonda: I vomit ...... (killing people's eyes) Say title! otherwise……-_-###

Flower: ...... Hey cold we are to solve the problem is the password inferno (narcissism: they might inferno inside someone will give me a blanket and food insect write a love letter with a password, oh alive, of course, is for me Compare Dora * ^ _ ^ *). Inferno now uses a new type of cryptography. Each is a given password contains only lowercase letters of the English word list, each word contains at least one letter, up to 75 letters. If a table of a word or words consisting of, in addition to other than the last one, each word is followed by a word contained, that is, before the word is a prefix of a word, the word list is called a word chain. For example the following words to form a word chain:

i int integer

But the following words do not make up the word chain:

integer

intern and now you have to do is take out a given word list some words, words make up the longest chain, it is to include a maximum number of words in the word chain. The statistics of the number of words it out, get a password.

Zonda: password is the number of words in the longest word chain included Ah ......

Flower: alive alive, and, looks pretty good to see you, to give you a sample of it:

Input Format

The format of these documents, the behavior of the first word of the table the number of words N (1 <= N <= 2000), each have a word line below, arranged in the order of dictionary, there is no intermediate word repetition blanket! !

Output Format

You have to submit the documents as long as the first line of output password to Come ^^

Sample input and output

Input # 1
5
i
int
integer
intern
internet
Output # 1
4 
Analysis: Actually this is a deformation of the LIS problem , so only need a [i]> a [j ] conditions change you can get the code

#include<bits/stdc++.h>
using namespace std;
string c[2005];
int w[2005],n,ans=0;
int main()
{

    scanf("%d",&n);
    for(int i=1;i<=n;i++)
       cin>>c[i];
    for(int i=1;i<=n;i++)
    {
       for(int j=1;j<i;j++)
       {
          IF (C [I] .find (C [J]) == 0 ) // when the sub-strings find function returns the position of the original string, because it must be prefixed 0 

            W [I] = max (W [I] , W [J] + . 1 ); 
          ANS = max (ANS, W [I]); 
       } 
    } 
    the printf ( " % D " , ANS + . 1 );
     return  0 ; 
 }

 

Guess you like

Origin www.cnblogs.com/cstdio1/p/11353936.html