/*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)