Ultraviolet A 11732 strcmp () anyone? (Trie, right brother, left son)

Source: Internet
Author: User
Ultraviolet A 11732 strcmp () anyone? (Trie, right brother, left son)

ACM

Question address:
Ultraviolet A 11732 strcmp () anyone?

Question:
Question about strcmp Function==The statement is executed several times.

Analysis:
Big white questions.
I heard that I want to use trie from my left son and right brother to save space. I learned it by the way.
Start to record the number of records first, and then traverse the statistics. The result is wrong...
Later I will refer to shoutmon's huge writing method and insert and count.

Code:

/**  Author:      illuz <iilluzen[at]gmail.com>*  File:        uva11732.cpp*  Create Date: 2014-09-24 10:30:15*  Descripton:  trie son brother*/#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>using namespace std;#define repf(i,a,b) for(int i=(a);i<=(b);i++)typedef long long ll;const int N = 0;const int MAXNODE =4000010;int n, cas;ll ans;char str[4001];struct STrie {int son[MAXNODE];int bro[MAXNODE];int val[MAXNODE];char ch[MAXNODE];int sz;STrie() { sz = 1; ch[0] = val[0] = bro[0] = son[0] = 0; }void init() { sz = 1; ch[0] = val[0] = bro[0] = son[0] = 0; }// inline int idx(char c) { return c - 'a'; }void insert(char *s) {int len = strlen(s), u = 0, p;repf (i, 0, len) {// check the brother of ufor (p = son[u]; p; p = bro[p]) {if (ch[p] == s[i])break;}// cannot find out than insertif (!p) {p = sz++;ch[p] = s[i];bro[p] = son[u];son[p] = 0;val[p] = 0;son[u] = p;}ans += (val[u] - val[p]) * (2 * i + 1);if (len == i) {ans += val[p] * (2 * i + 2);val[p]++;}val[u]++;u = p;}}} trie;int main() {// ios_base::sync_with_stdio(0);while (~scanf("%d", &n) && n) {trie.init();ans = 0;repf (i, 0, n - 1) {scanf("%s", str);trie.insert(str);}printf("Case %d: %lld\n", ++cas, ans);}return 0;}



Ultraviolet A 11732 strcmp () anyone? (Trie, right brother, left son)

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.