codevs1051 Word solitaire (stack)

Source: Internet
Author: User

/*See the range of n think this can not be DP what because the rules of this solitaire is very simple as long as the prefix is the same can be so that the dictionary order to ensure that the rules must be next to each other to make a stack each time the stack top element to see whether the elements into the stack meet the criteria if it conforms to the stack Otherwise pop up the top of the stack and then compare this in the process of constantly updating the largest ans*/#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<stack>#defineMAXN 100010using namespaceStd;stack<string>C;intn,ans=1;//ans has an initial value of 1 at least one wordstringS[MAXN];intMain () {CIN>>N; inti,j;  for(i=1; i<=n;i++) Cin>>S[i]; Sort (S+1, s+1+N); C.push (s[1]);  for(i=2; i<=n;i++)      {          inttmp;  while(S[i].find (C.top ())! =0|| S[i]==c.top ())//pay attention to the following condition! {c.pop (); if(c.size () = =0) Break; } c.push (S[i]);//either the stack is empty or the condition is met.tmp=c.size (); Ans=Max (ans,tmp); } cout<<ans; return 0;}

codevs1051 Word solitaire (stack)

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.