#include<iostream>
#include<algorithm>
#include<string>
#include<cstring>
#include<vector>
using namespace std; const int maxn = * + ;
int n;
long long ans; struct Trie
{
int head[maxn]; //head[i]为第i个结点的左儿子编号
int next[maxn]; //next[i]为第i个结点的右兄弟编号
char ch[maxn]; //第i个结点上的字符
int tot[maxn];
int sz; void init()
{
sz = ;
head[] = tot[] = next[] = ;
} void insert(char *s)
{
int u = , v, n = strlen(s);
tot[]++;
for (int i = ; i <= n; i++)
{
bool found = false;
for (v = head[u]; v != ; v = next[v])
{
if (ch[v] == s[i])
{
found = true;
break;
}
}
if (!found)
{
v = sz++;
tot[v] = ;
ch[v] = s[i];
next[v] = head[u];
head[u] = v;
head[v] = ;
}
u = v;
tot[u]++;
}
} void dfs(int depth, int u)
{
if (head[u] == )
ans += tot[u] * (tot[u] - )*depth;
else
{
int sum = ;
for (int v = head[u]; v != ; v = next[v])
sum += tot[v] * (tot[u] - tot[v]);
ans += sum / * ( * depth + );
for (int v = head[u]; v != ; v = next[v])
dfs(depth + , v);
}
}
}t; int main()
{
//freopen("D:\\txt.txt", "r", stdin);
char str[];
int kase = ;
while (~scanf("%d", &n), n)
{
t.init();
while (n--)
{
scanf("%s", str);
t.insert(str);
}
ans = ;
t.dfs(,);
printf("Case %d: %lld\n", ++kase, ans);
}
return ;
}

UVa 11732 strcmp()函数(左孩子右兄弟表示法)的更多相关文章

  1. Uva 11732 strcmp()函数

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

  2. UVA - 11732 "strcmp()" Anyone?左兄弟右儿子trie

    input n 2<=n<=4000 s1 s2 ... sn 1<=len(si)<=1000 output 输出用strcmp()两两比较si,sj(i!=j)要比较的次数 ...

  3. UVA 11732 - strcmp() Anyone? 字典树

    传送门:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&a ...

  4. 左儿子右兄弟Trie UVA 11732 strcmp() Anyone?

    题目地址: option=com_onlinejudge&Itemid=8&category=117&page=show_problem&problem=2832&qu ...

  5. UVa 11732 "strcmp()" Anyone? (左儿子右兄弟前缀树Trie)

    题意:给定strcmp函数,输入n个字符串,让你用给定的strcmp函数判断字符比较了多少次. 析:题意不理解的可以阅读原题https://uva.onlinejudge.org/index.php? ...

  6. Uva 11732 strcmp() Anyone?

    strcmp() Anyone? Time Limit: 2000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu [Subm ...

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

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

  8. UVA - 11732 "strcmp()" Anyone? (trie)

    https://vjudge.net/problem/UVA-11732 题意 给定n个字符串,问用strcmp函数比较这些字符串共用多少次比较. strcmp函数的实现 int strcmp(cha ...

  9. UVA 11732 strcmp() Anyone?(Trie的性质)

    strcmp() Anyone? strcmp() is a library function in C/C++ which compares two strings. It takes two st ...

随机推荐

  1. chkconfig --add失败的处理方法

    author: headsen  chen datet:2018-08-30   11:57:49 1,在/etc/init.d/下面添加两个文件,并授予 +X 的权限,效果如下: 2,添加到开启自启 ...

  2. OC开发_Storyboard——UITableView

    一.tableView 1.datasource数据源 (1 构造每一个tableVIewCell的方法:cellForRowAtIndexPath,这里的 dequeueReusableCellWi ...

  3. 微信小程序 --- model弹框

    model弹框:在屏幕中间弹出,让你进行选择: 效果: 代码: <button type="primary" bindtap="btnclick"> ...

  4. 170727、MySQL查询性能优化

    MySQL查询性能优化 MySQL查询性能的优化涉及多个方面,其中包括库表结构.建立合理的索引.设计合理的查询.库表结构包括如何设计表之间的关联.表字段的数据类型等.这需要依据具体的场景进行设计.如下 ...

  5. redis缓存数据架构实战

    redis命令参考:http://redisdoc.com/ 与memcache对比 redis安装配置 yum安装 yum -y install redis 源码安装 PS:make报错**问题:* ...

  6. php 函数__autoload与spl_autoload_register理解

    理解自:http://www.cnblogs.com/myluke/archive/2011/06/25/2090119.html __autoload的作用:当我们在一个页面使用其他文件的类方法时候 ...

  7. apidemos编译出错

    编译api 19的(4.4.2)apidemos一直报xml相关资源出错. 把build-toos 22删除,替换成19版本的,就ok了. 真是坑啊. 学习新东西难免遇到坑.

  8. 。。。。。。不带http https : 不报错 spring boot elasticsearch rest

    ......不带http https  : 不报错 先telnet http://onf:8080/getES653/道路桥梁正在“理疗”%20这14条道路纳入市政中修 @GetMapping(&qu ...

  9. Python绘图教程

    1.二维绘图 a. 一维数据集 用 Numpy ndarray 作为数据传入 ply 1. import numpy as np import matplotlib as mpl import mat ...

  10. Network of Schools---poj1236(强连通分量)

    题目链接 题意:学校有一些单向网络,现在需要传一些文件 求:1,求最少需要向几个学校分发文件才能让每个学校都收到, 2,需要添加几条网络才能从任意一个学校分发都可以传遍所有学校. 解题思路(参考大神的 ...