AC Diary-Solitaire game Codevs 1051

Source: Internet
Author: User

1051 Solitaire Games

time limit: 1 sspace limit: 128000 KBtitle level: Diamonds DiamondTitle Description Description

Given the n words, the order has been sorted by length. If a word i is a prefix for a word j, I->j is counted as a solitaire (two identical words cannot be counted as solitaire).

Your task is: To find the longest dragon for the words entered.

Enter a description Input Description

The first behavior N (1<=n<=105). The following n lines, one word per line (made up of lowercase), have been sorted by length. (<50 per word length)

Output description Output Description

Only one number, for the length of the longest dragon.

Sample input Sample Input

5

I

A

Int

Able

Inter

Sample output Sample Output

3

Data range and Tips Data Size & Hint

1<=n<=105

Ideas:

Use a hash table to save all the words.

and compare it with a hash table.

Then the linear structure compares whether the complexity of the prefix is n^2

However n<=100000

Time-outs steady.

So, how to optimize it?

Consider transforming the linear comparison into a tree

Establish a root node

We're looking for the word prefix from the root node.

If there is no prefix for this word

The word is grown on the root node as a leaf node.

If you find the

Then jump into this leaf node

Continue to look for prefixes from this leaf node

Until it is not found, and then the word as a new leaf node grows on the current node

This will optimize a lot of

Easy AC

Come on, on the code:

#include <cstdio>#include<string>#include<cstring>#include<iostream>#defineMoD 10000007using namespacestd;structNode {int  from, To,dis,next;};structNode edge[100001];intn,len[100001],hash[100001][Wuyi],dp[100001];inthead[100001],num,ans=0;stringword[100001];inlinevoidEdge_add (int  from,intTo ) {num++; Edge[num].to=to ; Edge[num]. from= from; Edge[num].next=head[ from]; head[ from]=num;}voidDfsintNowintPosintBe ) {ans=Max (Now,ans);  for(intI=head[be];i;i=Edge[i].next) {        if(len[edge[i].to]<Len[pos]) {            if(hash[edge[i].to][len[edge[i].to]-1]==hash[pos][len[edge[i].to]-1]) {DFS ( now+1, pos,edge[i].to); return ; }}} edge_add (Be,pos);}intMain () {len[0]=1; scanf ("%d",&N);  for(intI=1; i<=n;i++) {Dp[i]=1; CIN>>Word[i]; Len[i]=word[i].length ();  for(intj=0; j<len[i];j++) {Hash[i][j]=word[i][j]-'a'+1; if(j) hash[i][j]+= (hash[i][j-1]* in)%MoD; HASH[I][J]%=MoD; } DFS (1I0); } cout<<ans<<Endl; return 0;}

AC Diary-Solitaire game Codevs 1051

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.