#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. NodeJS路由(server.js + router.js)

    手动路由... server.js 创建http服务器,解析pathname,传递给router处理. var http = require("http"); var url = ...

  2. Unity3D笔记十 游戏元素

    一.地形 1.1 树元素 1.2 草元素 二.光源 2.1 点光源 点光源(Point Light):好像包围在一个类似球形的物体中,读者可将球形理解为点光源的照射范围,就像家里的灯泡可以照亮整个屋子 ...

  3. wampserver3 集成环境 启动Apache失败

    前提:安装完成后,原先是能够启动服务,但是按照网上教程修改conf文件后就不能启动Apache, 方法: 1.查看Apache错误日志(无奈的是看不懂) 2.在cmd命令行中查看,(打开cmd,输入: ...

  4. Animate CC 2017

    一.Animate CC 2017 下载及安装 http://www.ddooo.com/softdown/86853.htm

  5. javascript飞机大战-----006创建敌机

    先写一个敌机类 /* 创建敌机: */ function Enemy(blood,speed,imgs){ //敌机left this.left = 0; //敌机top this.top = 0; ...

  6. MVC之AJAX异步提交表单

    第一种用法: 在MVC中,依然可以使用ajax校验,跟在WebForm中的使用时一样的,唯一的区别就是将以前的URL路劲改为访问控制器下的行为 前台 <html> <head> ...

  7. Database Sharding Challenges DATABASE SHARDING

    w分布式查询.数据聚合.跨碎片join是可且应避免的.自增主键管理.基于-会话/事务/语句-选择碎片.通过-主键/模块/碎片索引-碎片化数据 http://www.agildata.com/datab ...

  8. 深究AngularJS——自定义服务详解(factory、service、provider)

    前言 3种创建自定义服务的方式.  Factory Service Provider 大家应该知道,AngularJS是后台人员在工作之余发明的,他主要应用了后台早就存在的分层思想.所以我们得了解下分 ...

  9. 转!!mysql 字段 is not null 和 字段 !=null

      今天在查询数据时,查到包含一条某个时间startTime(该字段默认为null ) 为null的记录,想把它过滤,加了 startTime != null 的条件,结果记录都没了,应该用条件 is ...

  10. Python开发【项目】:博客后台

    概述 通过自己写的博客后台代码.思路,来与武sir的代码进行一个差异化的比较,记录之间的差距,改善以后写代码的思路 博客后台这个项目,对之前Django学习的各个知识点都有涉及到,非常重要 用户登录验 ...