BZOJ4567: [Scoi2016] Back Word

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 Output2It 's really exploded recently ... first the crossdress of the trie tree, and then the key to re-build, then the problem into a tree with a tree numbered 1 to n arrangement, so that the number is increased by depth, and Sigma (V[x]-v[fa]) the smallest. It is not difficult to get the conclusion that we must have a subtree number complete and then go to the other subtree number (with the neighboring Exchange method is not difficult to prove). So each subtrees tree independent calculation, and then simply greedy a bit on the line.
#include <cstdio> #include <cctype> #include <queue> #include <cstring> #include <algorithm > #define REP (i,s,t) for (int. i=s;i<=t;i++) #define DWN (I,S,T) for (int. i=s;i>=t;i--) #define REN for (int i=first[x ];i;i=next[i]) using namespace std;const int Maxn=100010;const int maxnode=600010;typedef long Long ll;int ch[maxnode][26 ],val[maxnode],tot;void Insert (char* s) {int J=0,n=strlen (s), C;dwn (i,n-1,0) {c=s[i]-' a '; if (!ch[j][c]) Ch[j][c]=++tot ; j=ch[j][c];} Val[j]=1;} int first[maxn],next[maxn],to[maxn],ct=1,e;void Addedge (int u,int v) {to[++e]=v;next[e]=first[u];first[u]=e;} void build (int x,int fa) {if (val[x]) Addedge (FA,++CT), Fa=ct;rep (c,0,25) if (Ch[x][c]) build (CH[X][C],FA);} int cur,a[maxn],son[maxn],s[maxn];int cmp (int x,int y) {return s[x]<s[y];} void GetSize (int x) {s[x]=1;int Cnt=0;ren getsize (To[i]), S[x]+=s[to[i]];ren son[++cnt]=to[i];sort (son+1,son+cnt+1, CMP); Cnt=0;ren to[i]=son[++cnt];} ll dfs (int x,int fa) {a[x]=++cur;ll Res=a[x]-a[fa];ren Res+=dfs (to[i],x); reTurn res;} Char Str[maxn];int main () {int n;scanf ("%d", &n), Rep (i,1,n) scanf ("%s", str), insert (str); build (0,1); GetSize (1); printf ("%lld\n", Dfs); return 0;}

  

BZOJ4567: [Scoi2016] Back Word

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.