input

n 2<=n<=4000

s1

s2

...

sn

1<=len(si)<=1000

output

输出用strcmp()两两比较si,sj(i!=j)要比较的次数,结果在long long范围内(相同字符比较两次,不相同字符比较一次,包括'\0')

做法:由于字符集太大,要用左兄弟右儿子的trie保存字符,不用每次都开ch[62]个孩子

 1 #include <cstdio>
 2 #include <queue>
 3 #include <cstring>
 4 #include <iostream>
 5 #include <cstdlib>
 6 #include <algorithm>
 7 #include <vector>
 8 #include <map>
 9 #include <set>
10 #include <ctime>
11 #include <cmath>
12 #include <cctype>
13 #define MAX 100000
14 #define LL long long
15 #define mod 20071027
16 struct node
17 {
18     int sz;
19     char val;
20     node*ch[2];            //ch[1]兄弟,ch[0]儿子
21     node()
22     {
23         ch[0]=ch[1]=NULL;
24         sz=0;
25     }
26 };
27 char word[1010];
28 int n,cas=1;
29 long long sum;
30 long long insert(char*s,node*u)
31 {
32     long long sum=0,lastsz=u->sz++;
33     for(;*s||*(s-1);s++)
34     {
35         if(!u->ch[0])            //易错,要先把新建的结点连接到父结点才能往下走,否则新建之后父节点无法再读取到该结点
36         {
37             u->ch[0]=new node;
38             u->ch[0]->val=*s;
39         }
40         for(u=u->ch[0];u->val!=*s;u=u->ch[1])
41         {
42             if(!u->ch[1])
43             {
44                 u->ch[1]=new node;
45                 u->ch[1]->val=*s;
46             }
47         }
48         sum+=lastsz+u->sz;
49         lastsz=u->sz++;
50     }
51     return sum;
52 }
53 /*//这样可以将父节点的ch和新建的结点绑定起来,传过来的是&root
54 long long insert(char*s,node**u)
55 {
56     long long sum=0,lastsz=(*u)->sz++;
57     for(;*s||*(s-1);s++)
58     {
59         printf("%p\n",u);
60         for(u=&((*u)->ch[0]);(*u)&&(*u)->val!=*s;u=&((*u)->ch[1]));
61         if(*u==NULL)
62         {
63             *u=new node;
64             (*u)->ch[0]=(*u)->ch[1]=NULL;
65             (*u)->val=*s;
66         }
67         sum+=lastsz+(*u)->sz;
68         lastsz=(*u)->sz++;
69     }
70     return sum;
71 }*/
72 void freenode(node*u)
73 {
74     if(u==NULL) return;
75     freenode(u->ch[0]);
76     freenode(u->ch[1]);
77     delete u;
78 }
79 int main()
80 {
81     //freopen("/home/user/桌面/in","r",stdin);
82     while(scanf("%d",&n)==1&&n)
83     {
84         sum=0;
85         node *root=new node;
86         while(n--)
87         {
88             scanf("%s",word);
89             sum+=insert(word,root);
90         }
91         printf("Case %d: %lld\n",cas++,sum);
92         freenode(root);
93     }
94     //printf("time=%.3lf",(double)clock()/CLOCKS_PER_SEC);
95     return 0;
96 }
97
98 my Code

my Code

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <iostream>
 4 #include <algorithm>
 5 using namespace std;
 6
 7 #define repf(i,a,b) for(int i=(a);i<=(b);i++)
 8 typedef long long ll;
 9
10 const int N = 0;
11 const int MAXNODE = 4000010;
12
13 int n, cas;
14 ll ans;
15 char str[4001];
16
17 struct STrie {
18     int son[MAXNODE];
19     int bro[MAXNODE];
20     int val[MAXNODE];
21     char ch[MAXNODE];
22     int sz;
23
24     STrie() { sz = 1; ch[0] = val[0] = bro[0] = son[0] = 0; }
25     void init() { sz = 1; ch[0] = val[0] = bro[0] = son[0] = 0; }
26     // inline int idx(char c) { return c - 'a'; }
27
28     void insert(char *s) {
29         int len = strlen(s), u = 0, p;
30         repf (i, 0, len) {
31             // check the brother of u
32             for (p = son[u]; p; p = bro[p]) {
33                 if (ch[p] == s[i])
34                     break;
35             }
36             // cannot find out than insert
37             if (!p) {
38                 p = sz++;
39                 ch[p] = s[i];
40                 bro[p] = son[u];
41                 son[p] = 0;
42                 val[p] = 0;
43                 son[u] = p;
44             }
45             ans += (val[u] - val[p]) * (2 * i + 1);
46             if (len == i) {
47                 ans += val[p] * (2 * i + 2);
48                 val[p]++;
49             }
50             val[u]++;
51             u = p;
52         }
53     }
54 } trie;
55
56 int main() {
57     // ios_base::sync_with_stdio(0);
58     while (~scanf("%d", &n) && n) {
59         trie.init();
60         ans = 0;
61         repf (i, 0, n - 1) {
62             scanf("%s", str);
63             trie.insert(str);
64         }
65         printf("Case %d: %lld\n", ++cas, ans);
66     }
67     return 0;
68 } 

copy Code

转载于:https://www.cnblogs.com/cdyboke/p/5015540.html

UVA - 11732 strcmp() Anyone?左兄弟右儿子trie相关推荐

  1. UVA 11732 - strcmp() Anyone?(Trie)

    UVA 11732 - strcmp() Anyone? 题目链接 题意:给定一些字符串,要求两两比較,须要比較的总次数(注意.假设一个字符同样.实际上要还要和'\0'比一次,相当比2次) 思路:建T ...

  2. Uva 11732 strcmp()函数

    题目链接:https://vjudge.net/contest/158125#problem/A 题意: 系统中,strcmp函数是这样执行的,给定 n 个字符串,求两两比较时,strcmp函数要比较 ...

  3. uva 11732 - strcmp() Anyone? 不错的Trie题

    题解:http://blog.csdn.net/u013480600/article/details/23122503 我的代码一直TLE,,,看了人家的之后,认为1.链式前向星比較好,2.*dept ...

  4. uva 11732 strcmp() Anyone?

    https://vjudge.net/problem/UVA-11732 题意: 给出许多字符串,他们两两按下面的函数比较 输出比较次数 s[i]==t[i]  ,  和  s[i]=='\0'   ...

  5. strcmp() Anyone? UVA - 11732 左孩子右兄弟Trie/计数

    #include<bits/stdc++.h> using namespace std; #define ll long longconst int maxnode = 4000 * 10 ...

  6. java实现家庭关系图_左孩子右兄弟二叉树实现家族家谱

    /* Name: 家谱 Description: 本项目对家谱管理进行简单的模拟,以实现查看祖先和子孙个人信息.插入家族成员.删除家族成员等功能. */ #include #include using ...

  7. 3422. 左孩子右兄弟

    对于一棵多叉树,我们可以通过 "左孩子右兄弟" 表示法,将其转化成一棵二叉树. 如果我们认为每个结点的子结点是无序的,那么得到的二叉树可能不唯一. 换句话说,每个结点可以选任意子结 ...

  8. 辅助类BinaryTreeNodeLeftChildRightSibling(左孩子右兄弟,二叉树结点)

    辅助类BinaryTreeNodeLeftChildRightSibling(左孩子右兄弟,二叉树结点) template<typename T> class BinaryTreeNode ...

  9. 蓝桥杯 左baby右兄弟

    试题: 思路: "左孩子右兄弟"是常见的多叉树转化成二叉树的方法.具体的实现方式是,从第二层最右边的结点开始,将将自己的孩子结点放到左边,左边一位的兄弟放到左边的结点上.对于是多支 ...

最新文章

  1. 计算机科学与技术专业综合二,计算机科学与技术专业综合一第二页
  2. lighttpd配置支持https
  3. Python 基础语法(二)
  4. matlab敏感词输出代码,敏感词设置
  5. 2020牛客暑期多校训练营(第六场)
  6. [linux]Ubuntu12.1下打开terminal的方式
  7. c语言如何关闭线程,如何用C语言实现多线程
  8. u盘pe无人值守linux,从U盘无人值守安装linux操作系统(纯实践笔记
  9. Java 连接LDAP实现验证与查询用户
  10. 数据库事务的四大特性以及隔离级别
  11. Python对zip、tgz、rar压缩包的解压与读取
  12. Python 中 jieba 库
  13. 安装python第三方模块包时,报错 error: Microsoft Visual C++ 14.0 or greater is required. Get it with “Microsoft C
  14. web安全之SQL注入(三)
  15. 金融科技方便生活,分布式架构助力微粒贷“闪电放款”
  16. 工作总结:简明扼要,突出重点
  17. [湖南大学程序设计实训训练作业一]9.二叉树遍历,从前序、中序到后序(二叉树呀,面试必考哦!)
  18. 微观经济学案例分析(四)
  19. GCC and MinGW-w64 for Windows
  20. 2020/04/12 02-HTML和URL提取、豆瓣读书爬虫编写

热门文章

  1. Mysql 开启远程连接
  2. Linux下配置LVM
  3. 数据分析真的能驱动用户快速增长吗?
  4. centos7 搭建Docker Registry
  5. 海尔5D净水洗热水器引领中国制造未来
  6. 企业级 oracle11G r2 DataGuard 安装配置
  7. 《几何与代数导引》例2.7.3
  8. C++ 序列化 serialization 如何将类持久化?
  9. Linux系统中/dev/mtd与/dev/mtdblock的区别
  10. 在c++代码中关闭和启动另外一个pid进程号,共享内存数据使用