UVa 11732 "strcmp ()" Anyone? (left son right brother prefix tree trie)

Source: Internet
Author: User

Test instructions: Given the strcmp function, enter n strings, allowing you to determine how many times a character is compared with a given strcmp function.

Analysis: Test instructions can read the original problem without understanding Https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem &problem=2832

String a lot, and very long, if according to the title of the meaning of 22 comparison, will certainly be tle, so to use the prefix tree (Trie) to solve, of course, only with a simple prefix tree will be tle,

We must optimize it, read the online Daniel, know how to use the left son right brother of the method to optimize, to tell the truth, not used before, see for a long time to understand what is the meaning of

Not very clear to draw a picture to think will understand, I use the edge of the insertion edge calculation method, in each branch is calculated, and then plus, we want to find each branch,

Except the last one is 2*i+1, the last one plus 2*i+2.

The code is as follows:

#include <cstdio>#include<iostream>#include<cstring>using namespaceStd;typedefLong LongLL;Const intMAXN =4000* ++Ten; LL ans=0;Chars[1010];structtrie{intLSON[MAXN]; intRBRO[MAXN]; intVAL[MAXN]; CharCH[MAXN]; intsz; voidClear () {sz =1; lson[0] = rbro[0] = val[0] =0; Ans =0; } voidInsertConst Char*R) {        intU =0, V, n =strlen (s);  for(inti =0; I <= N; ++i) {             for(v = lson[u]; v; v =Rbro[v])if(S[i] = = Ch[v]) Break;//find the knot.            if(!V) {//new Nodev = sz++; CH[V]=S[i]; LSON[V]=0;//left son is emptyRBRO[V] = Lson[u];//The knot is placed in the header.VAL[V] =0;//InitializeLson[u] = v;//inserting Nodes} ans+ = (Val[u]-val[v]) * (2* i +1);//(Val[u]-val[v]) means the number of words that are not the same as V            if(i = =N) {ans+ = val[v] * (2* i +2); ++Val[v]; }            ++val[u]; U =v; }    }}; Trie Trie;intMain () {intN, Kase =0;  while(SCANF ("%d", &N), N) {trie.clear ();  for(inti =0; I < n; ++i) {scanf ("%s", s);        Trie.insert (s); } printf ("Case %d:%lld\n", ++Kase, ans); }    return 0;}

Here is the code of Daniel Rujia Liu, I basically write according to his code (because I really can't write it ...) )

//UVa11732 strcmp () anyone?//Rujia Liu#include <cstring>#include<vector>using namespacestd;Const intMaxnode =4000* ++Ten;Const intSigma_size = -;//the alphabet is the trie of all lowercase letters.structTrie {intHead[maxnode];//Head[i] Number of the left son of the first node  intNext[maxnode];//Next[i] The right sibling number for the first node  CharCh[maxnode];//Ch[i] is the character on the first node  intTot[maxnode];//Tot[i] The total number of leaf nodes that are contained in the subtree for the first node  intSz//Total node points  Long LongAns//Answer  voidClear () {sz =1; tot[0] = head[0] = next[0] =0; }//Initially, there is only one root node.//Insert string s (including last '% '), update tot along the way  voidInsertConst Char*s) {intU =0, V, n =strlen (s); tot[0]++;  for(inti =0; I <= N; i++) {      //find character A[i]      BOOLFound =false;  for(v = head[u]; v! =0; v =Next[v])if(Ch[v] = = S[i]) {//found theFound =true;  Break; }      if(!found) {v= sz++;//new NodeTOT[V] =0; CH[V]=S[i]; NEXT[V]=Head[u]; Head[u]= V;//Insert the header into the listHEAD[V] =0; } u=v; Tot[u]++; }  }  //The sum of the 22 comparisons of all the words in the statistics lcp=u  voidDfsintDepthintu) {if(Head[u] = =0)//leaf knot PointAns + = tot[u] * (tot[u)-1) *depth; Else {      intsum =0;  for(intv = head[u]; V! =0; v =next[v]) sum+ = tot[v] * (Tot[u]-tot[v]);//subtree v Select a string, and then select one of the other sub-treesAns + = SUM/2* (2* Depth +1);//divided by 2 is counted two times for each method of selection.       for(intv = head[u]; V! =0; v =Next[v]) Dfs (depth+1, V); }  }  //Statistics  Long Longcount () {ans=0; DFS (0,0); returnans; }}; #include<cstdio>Const intMAXL = ++Ten;//Maximum length per wordintN;CharWORD[MAXL]; Trie Trie;intMain () {intKase =1;  while(SCANF ("%d", &n) = =1&&N) {trie.clear ();  for(inti =0; I < n; i++) {scanf ("%s", Word);    Trie.insert (word); } printf ("Case %d:%lld\n", kase++, Trie.count ()); }  return 0;}

UVa 11732 "strcmp ()" Anyone? (left son right brother prefix tree trie)

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.