Uva 11732 strcmp()函数
题目链接: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()函数的更多相关文章
- UVa 11732 strcmp()函数(左孩子右兄弟表示法)
#include<iostream> #include<algorithm> #include<string> #include<cstring> #i ...
- 左儿子右兄弟Trie UVA 11732 strcmp() Anyone?
题目地址: option=com_onlinejudge&Itemid=8&category=117&page=show_problem&problem=2832&qu ...
- UVA 11732 - strcmp() Anyone?(Trie)
UVA 11732 - strcmp() Anyone? 题目链接 题意:给定一些字符串,要求两两比較,须要比較的总次数(注意.假设一个字符同样.实际上要还要和'\0'比一次,相当比2次) 思路:建T ...
- UVa 11732 "strcmp()" Anyone? (左儿子右兄弟前缀树Trie)
题意:给定strcmp函数,输入n个字符串,让你用给定的strcmp函数判断字符比较了多少次. 析:题意不理解的可以阅读原题https://uva.onlinejudge.org/index.php? ...
- UVA - 11732 "strcmp()" Anyone? (trie)
https://vjudge.net/problem/UVA-11732 题意 给定n个字符串,问用strcmp函数比较这些字符串共用多少次比较. strcmp函数的实现 int strcmp(cha ...
- UVA 11732 - strcmp() Anyone? 字典树
传送门:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&a ...
- Uva 11732 strcmp() Anyone?
strcmp() Anyone? Time Limit: 2000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu [Subm ...
- UVA 11732 strcmp() Anyone? (压缩版字典树)
题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...
- UVA 11732 strcmp() Anyone?(Trie的性质)
strcmp() Anyone? strcmp() is a library function in C/C++ which compares two strings. It takes two st ...
随机推荐
- Alibaba Java Coding Guidelines 安装(eclipse)并简单使用
源码地址:https://github.com/alibaba/p3c 1.安装 第1步.help >> Instakll New Software... 第2步.在Work with中输 ...
- ECharts基本设置
theme = { // 全图默认背景 // backgroundColor: ‘rgba(0,0,0,0)’, // 默认色板 color: ['#ff7f50','#87cefa','#da70d ...
- chrome浏览器解决 跨域调试问题
1.关闭chrome浏览器(全部) 我们可以通过使用chrome命令行启动参数来改变chrome浏览器的设置,具体的启动参数说明参考这篇介绍.https://code.google.com/p/xia ...
- RTT设备与驱动之PIN设备
单片机的PIN有2个基本功能:GPIO和AFIO,其中gpio的常用功能: 1 输入:上拉.下拉.模拟.浮动 2 输出:上拉.下拉.推挽.开漏 3 中断:上升沿.下降沿.双沿.高电平.低电平触发 RT ...
- maya2012安装失败如何卸载重装
AUTODESK系列软件着实令人头疼,安装失败之后不能完全卸载!!!(比如maya,cad,3dsmax等).有时手动删除注册表重装之后还是会出现各种问题,每个版本的C++Runtime和.NET f ...
- C# list.toArray list与数组的转换
void Start () { List<int> list = new List<int>(); list.Add(); list.Add(); list.Add(); li ...
- PlayMaker Get Parent 拿到父物体
这里是拿到自己的父物体,然后存储到Parent这个GameObject变量里. 然后在Parent那个位置生成一个Coin,也就是在父物体那生成一个Coin.
- swing线程机制
在介绍swing线程机制之前,先介绍一些背景概念. 背景概念 同步与异步: 同步是指程序在发起请求后开始处理事件并等待处理的结果或等待请求执行完毕,在此之前程序被阻塞(block)直到请求完成 ...
- 60、Docker 学习笔记(CentOS 7.1)
#基本概念 -x86_64-minimal.tar.gz | docker import - centos:v7.mini``` 然后查看导入的镜像: ##上传镜像 >用户可以通过 docker ...
- The function getUserId must be used with a prefix when a default namespace is not specified 解决办法
The function getUserId must be used with a prefix when a default namespace is not specified 解决方法: 1. ...