UVa 11732-strcmp () anyone?

Source: Internet
Author: User

Title: Give you some words, give you a string comparison function, all words are parity, what is the number of comparisons.

Analysis: Strings, Terry.

First of all. Look at the data size, assuming normal discovery clues, meeting tle and MLE.

As a result, the normal dictionary tree depth is 1000, and there may be a lot of space wasted.

So, using a tree compression algorithm (left brother, right child). can improve execution efficiency.

And then. You can do it in the dictionary tree. Statistics, you can make a contribution to the statistics, or the edge of the achievement of statistics.

Here I choose the edge of the method of statistical methods (online a large number of practices, are the first achievements and then statistics, search solution)

Each time you insert a word. It is only compared to the word inserted earlier, and the number of words is the same as the number of each letter.

The number of times each letter in a word is compared. Is the number of words that the root node of the letter contains.

The word comparison functions such as the following:

int strcmp (char *s, char *t) {    int i;    for (i=0; s[i]==t[i]; i++)        if (s[i]== ')            return 0;    return S[i]-t[i];}
Due to the comparison function, the following points should be noted when calculating:

1. Two words of the same length of L are more expensive than 2l-1. The final extrapolation of the S-end;

2. The comparison length of the word should be strlen (str) +1, The Terminator is also a relatively more ring;

3. Assume that two words are the same, then compute an end inference more than once. +1 more times.

Note: Use ll, otherwise the data will overflow.

#include <iostream> #include <cstdlib> #include <cstring> #include <cstdio>using namespace std;    typedef long LONG Ll;char words[1010];/* Trie define */#define NODESIZE 4444444//number of nodes typedef struct NODE1 {Char    Value;int size;int Count; Node1* lchild;node1* Rchild;}  Tnode;  Tnode Dict[nodesize];        Class Trie {private:ll count;          int size;    tnode* Root;           Public:trie () {initial ();}              void Initial () {memset (dict, 0, sizeof (dict));        Size=0;count=0ll;root=newnode (0);          } tnode* NewNode (char c) {Dict[size].value = C;return &dict[size + +];}             void Insert (char* word, int L) {tnode* now = Root->rchild,*save = root;            int same = 1;  for (int i = 0; I <= L; + + i) {if (!now) {save->rchild = NewNode (Word[i]); now = Save->rchild;now->count = 1;same = 0;} else {if (i) count + = Now->count;count + = nOw->count ++;while (now->lchild && now->value! = word[i]) now = now->lchild;if (now->value! = Wor D[i]) {now->lchild = NewNode (Word[i]); now = Now->lchild;same = 0;}}            Save = Now;now = save->rchild;            } if (same) save->size + +;        Count + = save->size; } LL query () {return count;}}  Trie /* Trie END */int main () {int case = 1,n;while (scanf ("%d", &n)! = EOF) {if (! N) break;trie.initial (); for (int i = 0; i < N; + + i) {scanf ("%s", words); Trie.insert (Words,strlen (words));} printf ("Case%d:%lld\n", Case ++,trie.query ());} return 0;}

Copyright notice: This article Bo Master original article. Blog, not reproduced without consent.

UVa 11732-strcmp () anyone?

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.