UVa 11732 strcmp()函数(左孩子右兄弟表示法)
#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()函数(左孩子右兄弟表示法)的更多相关文章
- Uva 11732 strcmp()函数
题目链接:https://vjudge.net/contest/158125#problem/A 题意: 系统中,strcmp函数是这样执行的,给定 n 个字符串,求两两比较时,strcmp函数要比较 ...
- UVA - 11732 "strcmp()" Anyone?左兄弟右儿子trie
input n 2<=n<=4000 s1 s2 ... sn 1<=len(si)<=1000 output 输出用strcmp()两两比较si,sj(i!=j)要比较的次数 ...
- UVA 11732 - strcmp() Anyone? 字典树
传送门:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&a ...
- 左儿子右兄弟Trie UVA 11732 strcmp() Anyone?
题目地址: option=com_onlinejudge&Itemid=8&category=117&page=show_problem&problem=2832&qu ...
- UVa 11732 "strcmp()" Anyone? (左儿子右兄弟前缀树Trie)
题意:给定strcmp函数,输入n个字符串,让你用给定的strcmp函数判断字符比较了多少次. 析:题意不理解的可以阅读原题https://uva.onlinejudge.org/index.php? ...
- Uva 11732 strcmp() Anyone?
strcmp() Anyone? Time Limit: 2000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu [Subm ...
- UVA 11732 - strcmp() Anyone?(Trie)
UVA 11732 - strcmp() Anyone? 题目链接 题意:给定一些字符串,要求两两比較,须要比較的总次数(注意.假设一个字符同样.实际上要还要和'\0'比一次,相当比2次) 思路:建T ...
- UVA - 11732 "strcmp()" Anyone? (trie)
https://vjudge.net/problem/UVA-11732 题意 给定n个字符串,问用strcmp函数比较这些字符串共用多少次比较. strcmp函数的实现 int strcmp(cha ...
- UVA 11732 strcmp() Anyone?(Trie的性质)
strcmp() Anyone? strcmp() is a library function in C/C++ which compares two strings. It takes two st ...
随机推荐
- 【BZOJ2599】[IOI2011]Race 树的点分治
[BZOJ2599][IOI2011]Race Description 给一棵树,每条边有权.求一条简单路径,权值和等于K,且边的数量最小.N <= 200000, K <= 100000 ...
- DGbroker故障切换示例
1.主库故障 SQL> startup ORACLE instance started. Total System Global Area bytes Fixed Size bytes Vari ...
- ST-LINK使用注意
利用ST-LINK下载程序注意事项: 1.接线 按照上面图对着自己的开发板连接相应的引脚就可以了. 2.keil5配置 线连接完之后,要对自己的工程进行相关的 配置才能正确进行下载. 首先选择ST-L ...
- Floyd算法并输出路径
hdu1224 Free DIY Tour Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Ot ...
- C++ 引用的辨别
对于习惯使用C进行开发的朋友们,在看到c++中出现的&符号,可能会犯迷糊,因为在C语言中这个符号表示了取地址符,但是在C++中它却有着不同的用途,掌握C++的&符号,是提高代码执行效率 ...
- 170609、Nginx配置文件详细说明
在此记录下Nginx服务器nginx.conf的配置文件说明, 部分注释收集与网络. #运行用户 user www-data; #启动进程,通常设置成和cpu的数量相等 worker_processe ...
- __FILE__ 与 $_SERVER['SCRIPT_FILENAME']的区别
二者都表明了本文件的绝对路径,区别在于,$_SERVER['SCRIPT_FILENAME']指向当前执行脚本的绝对路径:__FILE__指向当前文件的绝对路径:也就是写在哪个文件里就是哪里. 例子: ...
- pouch架构源码分析
// daemon/daemon.go 1.func NewDaemon(cfg config.Config) *Daemon 调用containerStore, err := meta.NewSto ...
- Windows环境下手动更新boot2docker.iso
GitHub连不上导致自动更新失败. https://github.com/boot2docker/boot2docker/releases 替换了DockerToolbox安装目录和系统盘用户目录\ ...
- C++模拟Http/Https访问web站点
一.概述 1.Http与Https的区别与联系 在OSI参考模型中Http与Https均属于应用层协议.Http即Hypertext Transfer Protocol,超文本传输协议:而Https为 ...