题目链接:https://vjudge.net/contest/158125#problem/A

题意:

系统中,strcmp函数是这样执行的,给定 n 个字符串,求两两比较时,strcmp函数要比较多少次?

如:

t  h  a  n  \n       t  h  e  r  e  \n

t  h  a  t  \n        t  h  e  \n

2  2  2  1            2  2  2  1

直接两两比较是超时的。

可以这样建立一个字典树:

可以发现一直要比较到交叉处,和这个交叉点的深度有关(2*depth+1)。

1、这个字典序每一个交叉点都是要计算的,所以采用邻接表的形式存图;

2、每个结点都要计算他的叶子结点的个数,当分叉的时候,那么说明这里有两个(多个)不同的单词,从中要选择两个单词,计算有多少种搭配,比较次数就是这些搭配*(2*depth+1);

3、递归找下一个结点。

 #include <bits/stdc++.h>

 using namespace std;

 const int maxnode =  *  + ;
const int sigma_size = ; struct Trie
{
int head[maxnode];
int next[maxnode];
char ch[maxnode];
int tot[maxnode];
int sz;
long long ans;
void clear()
{
sz = ;
tot[] = head[] = next[] = ;
} void insert(const char *s)
{
int u = , v, n = strlen(s);
tot[]++;
for(int i = ; i <= n; i++)
{
// 找字符a[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);
}
} long long count()
{
ans = ;
dfs(,);
return ans;
} } trie; char word[]; int main()
{
int n;
int kase = ;
while(scanf("%d",&n),n)
{
trie.clear();
for(int i=; i<n; i++)
{
scanf("%s",word);
trie.insert(word);
}
printf("Case %d: %lld\n",kase++,trie.count());
} return ;
}

Uva 11732 strcmp()函数的更多相关文章

  1. UVa 11732 strcmp()函数(左孩子右兄弟表示法)

    #include<iostream> #include<algorithm> #include<string> #include<cstring> #i ...

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

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

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

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

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

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

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

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

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

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

  7. Uva 11732 strcmp() Anyone?

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

  8. UVA 11732 strcmp() Anyone? (压缩版字典树)

    题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...

  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. asp.net模板页实现类似jquery中document.ready

    模板页先判断是否有方法DocumentReady,有的话就调用 1.模板页 <script type="text/javascript" language="jav ...

  2. jenkins出现的问题

    1.增加服务器时 要修改时,,需要设置 2:出现这个问题是 执行了npm install node-sass

  3. JS数组去重总结

    方法一: 双层循环,外层循环元素,内层循环时比较值 如果有相同的值则跳过,不相同则push进数组 Array.prototype.distinct = function(){ var arr = th ...

  4. DOM, DOCUMENT, BOM, WINDOW 有什么区别?

    DOM: DOM 全称是 Document Object Model,也就是文档对象模型. DOM 就是针对 HTML 和 XML 提供的一个API.什么意思?就是说为了能以编程的方法操作这个 HTM ...

  5. axios处理http请求

    axios中文文档 https://github.com/mzabriskie/axios#using-applicationx-www-form-urlencoded-format  axios文档 ...

  6. 牛客网Java刷题知识点之File对象常用功能:获取文件名称、获取文件路径、获取文件大小、获取文件修改时间、创建与删除、判断、重命名、查看系统根目录、容量获取、获取某个目录下内容、过滤器

    不多说,直接上干货! 获取文件名称.获取文件路径.获取文件大小.获取文件修改时间 FileMethodDemo.java package zhouls.bigdata.DataFeatureSelec ...

  7. JS arguments转array

    JS arguments转array? Array.prototype.slice.call(arguments)

  8. Java集合框架概述

    集合框架指的是容器类.Java中大量持有对象的方式有数组和容器类两种方式.数组相较于容器类的优点在于:①随机访问效率高:由于是连续的存储空间,可以计算地址直接访问 ②类型确定:数组在创建时即可确定元素 ...

  9. HDU 1045——Fire Net——————【最大匹配、构图、邻接矩阵做法】

    Fire Net Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Sta ...

  10. android 官网访问不了

    网上搜到的解决方案,亲测有用.记下来,以备遗忘. 使用管理员权限,修改C:\Windows\System32\Drivers\etc\hosts文件,加入以下内容 173.194.127.7 deve ...