bzoj4567 back Words

Source: Internet
Author: User

Descriptionlweb face such as the mountains of English words, plunged into deep meditation, "how can I learn quickly, and then go to play three kill it?" ”。 At this time, the wisdom of Feng teacher from afar, he gave Lweb a plan book and a large cylinder of pickled peppers, his plan book is long like this: ————— ordinal word ————— 1 2......n-2n-1 n ————— then Feng teacher told Lweb, I know you want to learn the word total has n, now we from the Complete the schedule below, for a word with ordinal x (ordinal 1...x-1 is already filled in): 1) If there is a word that is its suffix and is not currently filled into the table, then he needs to eat nxn pepper to learn; 2) when all its suffixes are filled in the table, if at 1 ... The word in X-1 's position is not its suffix, so you can remember it by eating X-pepper; 3) When all of its suffixes are filled into the table, if the position of the 1...x-1 is the word that it is suffix, and all the words that are in its suffix, the number is the maximum Y, then you just Eat X-y A pickled pepper can remember it. Lweb is a spicy and spicy things will go out of the strange children, so please help Lweb, looking for an optimal word filling scheme, so that he remembers the N-word case, eat the least of the pickled peppers. Input inputs an integer n, which indicates the number of words lweb to learn. Next n lines, each line has a word (made up of lowercase letters and guarantees that any word 22 is different) 1≤n≤100000, the sum of the lengths of all characters 1≤|len|≤510000 Output

Lweb the minimum number of pickled peppers to eat

Sample Input2
A
BASample Output2 The title of the question means:

Give you n strings, different permutations have different costs, and the costs are calculated as follows (the position of the string s is x):

1. The string after s has a suffix of s, the cost is n^2;

2. The string in front of S has a suffix of s, and there is no suffix of s after s, at the cost of X-Y (the position of the last suffix that is not equal to s);

3.S has no suffix, the cost is x.

According to the terms of the suffix, we can determine that the problem can be used trie tree. Of course we need to store the string in reverse.

Then we can know that for a string, only its first point (before flipping) is useful to us, for example: ABCD,BCD We only use record a with a side to B.

We make a normal contribution, then mark all the end points, if the father of this point is its prefix, then even a father to his side.

For a character-fetching strategy, consider greed. For one point, we prefer the smallest subtree, from small to large, to enumerate in turn.

#include <iostream>#include<cstdlib>#include<cstdio>#include<cstring>#include<algorithm>#defineREP (I,k,n) for (Long long i=k;i<=n;i++)#defineIn (a) a=read ()#defineMAXN 510010using namespaceStd;inlineLong LongRead () {Long Longx=0, f=1; CharCh=GetChar ();  for(;! IsDigit (CH); ch=GetChar ())if(ch=='-') F=-1;  for(; isdigit (CH); ch=GetChar ()) x=x*Ten+ch-'0'; returnx*F;}Long LongN;Long LongCnt=1;Long LongF[MAXN];Long LongTotal=0, HEAD[MAXN],NXT[MAXN],TO[MAXN];Long Longtree[maxn][ -],ISEND[MAXN];Long LongSIZE[MAXN];Long LongDfn[maxn],ind=0, ans=0;Long Longtop=0;structnode{Long LongSiinch;} Arr[maxn];inlinevoidAdlLong LongALong Longb) { Total++;//cout<<total<< "" <<a<< "" <<b<<endl;to[total]=b; Nxt[total]=Head[a]; Head[a]=Total ; return ;} InlinevoidInsertChar*s) {    Long Longu=1;  for(Long LongI=strlen (s)-1; i>=0; i--){        if(!tree[u][s[i]-'a']) tree[u][s[i]-'a']=++CNT; U=tree[u][s[i]-'a']; }    //cout<<u<<endl;isend[u]=1; return ;} InlinevoidBuildLong LongULong Longf) {    if(Isend[u]) {ADL (F,U); F=u; } REP (I,0, -)        if(Tree[u][i]) build (TREE[U][I],F); return ;} InlinevoidGetSize (Long Longu) {Size[u]=1;  for(Long LongE=head[u];e;e=Nxt[e])        {GetSize (to[e]); Size[u]+=Size[to[e]]; }    return ;} InlineBOOLCMP (node A,node b) {returna.si<b.si;} InlinevoidGetans (Long Longu) {Dfn[u]=++IND; Long Longm=top+1;  for(Long LongE=head[u];e;e=Nxt[e]) {arr[++top].inch=To[e]; Arr[top].si=Size[to[e]]; }    if(top<m)return ; Sort (arr+m,arr+top+1, CMP); REP (i,m,top) {Getans (arr[i].inch); Ans+=dfn[arr[i].inch]-Dfn[u]; //cout<<u<< "" <<arr[i].in<< "" <<ans<<endl;} top=m-1; return ; }intMain () {inch(n); Charc[100010]; REP (i,1, N) {scanf ("%s", c);    Insert (c); } Build (1,1); GetSize (1); Getans (1); cout<<ans;}/*4DADCBABACBA*/

bzoj4567 back Words

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.