[CODEVS1051] Solitaire Game

Source: Internet
Author: User

Title 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.

Data range and Tips Data Size & Hint

1<=n<=105

See this problem, although know is to do with stacks, but still no idea, too weak ... we'll find a way. (Face-covering bear

This is probably the case:

We have played solitaire card game is usually A is a b suffix, you can solitaire; but this is the prefix for J, so I->j is a solitaire, so you can sort the input words in dictionary order, then the words with the same prefix will be stacked (sort (ch,ch+n,cmp))

At this point we maintain a stack, first put the first word into the stack, each word and the top of the stack find, if the first word in the No. 0 position can find top, then we put the first word into the stack, continue to read the next word;

If it cannot be found, the top element of the stack pops up, and the first word is the same as the new stack top element, until the stack is empty, and the first word is put into the stack.

In the above process, we can constantly update the value of Max, representing the elements in the stack, that is, how many words can be solitaire with each other, max=max<stack.size ()? Stack.size (): Max;

This is the way of thinking about this problem. The code changed for a half day ....

Code:

#include <iostream>#include<cstdio>#include<stack>#include<cstring>#include<algorithm>using namespaceStd;typedefstructdata{strings;} Data;data ch[1000000];BOOLcmp (data x,data y) {intLen=x.s.length () >y.s.length ()?x.s.length (): Y.s.length ();  for(intI=0; i<len;++i) {if(X.s[i]<y.s[i])return true; Else if(X.s[i]>y.s[i])return false; }    if(X.s.length () <y.s.length ())return true; Else return false;}intMain () {intn,max=1; CIN>>N;  for(intI=0; i<n;++i) Cin>>Ch[i].s; Sort (Ch,ch+n,cmp); /*for (int i=0;i<n;++i) cout<<ch[i].s<< ";*/Stack<data>Mystack; Mystack.push (ch[0]);  for(intI=1; i<n;++i) {data tmp; TMP=Mystack.top (); if(Ch[i].s.find (TMP.S,0)==0)        {            if(Tmp.s.length ()! =ch[i].s.length ()) Mystack.push (Ch[i]); }            Else        {             while(!Mystack.empty ()) {tmp=Mystack.top (); if(Ch[i].s.find (TMP.S,0)==0)                  Break;            Mystack.pop ();        } mystack.push (Ch[i]); } Max=max>mystack.size ()?max:mystack.size (); } cout<<max<<Endl; return 0;}
View Code

[CODEVS1051] Solitaire Game

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.