UVa 11732 "strcmp()" Anyone? (左儿子右兄弟前缀树Trie)
题意:给定strcmp函数,输入n个字符串,让你用给定的strcmp函数判断字符比较了多少次。
析:题意不理解的可以阅读原题https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2832
字符串很多,又很长,如果按照题目的意思两两比较,肯定会TLE,所以要用前缀树(Trie)来解决,当然只是用简单的前缀树也会TLE的,
我们必须对其进行优化,看了网上大牛们的,知道要用左儿子右兄弟的方法来优化,说实话,以前还没用过,看了好久才明白是什么个意思,
不是很明白的画个图想一想就会明白的,我采用的是边插入边计算的方法,在每一个分枝都进行计算,然后加和,我们要找每个分枝,
除了最后一个都是2*i+1,最后一个再加上2*i+2。
代码如下:
#include <cstdio>
#include <iostream>
#include <cstring> using namespace std;
typedef long long LL;
const int maxn = 4000 * 1000 + 10;
LL ans = 0;
char s[1010]; struct Trie{
int lson[maxn];
int rbro[maxn];
int val[maxn];
char ch[maxn];
int sz;
void clear(){ sz = 1; lson[0] = rbro[0] = val[0] = 0; ans = 0; } void insert(const char *s){
int u = 0, v, n = strlen(s);
for(int i = 0; i <= n; ++i){
for(v = lson[u]; v; v = rbro[v])
if(s[i] == ch[v]) break;//找到结点 if(!v){//新建结点
v = sz++;
ch[v] = s[i];
lson[v] = 0;//左儿子为空
rbro[v] = lson[u];//结点放在首部
val[v] = 0;//初始化
lson[u] = v;//插入结点
}
ans += (val[u]-val[v]) * (2 * i + 1);//(val[u]-val[v])意思是和v不一样的单词数
if(i == n){
ans += val[v] * (2 * i + 2);
++val[v];
}
++val[u]; u = v;
}
}
}; Trie trie; int main(){
int n, kase = 0;
while(scanf("%d", &n), n){
trie.clear();
for(int i = 0; i < n; ++i){
scanf("%s", s);
trie.insert(s);
} printf("Case %d: %lld\n", ++kase, ans);
}
return 0;
}
下面是大牛Rujia Liu的代码,我基本是根据他的代码写的(因为自己确实是写不出来。。。)
// UVa11732 strcmp() Anyone?
// Rujia Liu
#include<cstring>
#include<vector>
using namespace std; const int maxnode = 4000 * 1000 + 10;
const int sigma_size = 26; // 字母表为全体小写字母的Trie
struct Trie {
int head[maxnode]; // head[i]为第i个结点的左儿子编号
int next[maxnode]; // next[i]为第i个结点的右兄弟编号
char ch[maxnode]; // ch[i]为第i个结点上的字符
int tot[maxnode]; // tot[i]为第i个结点为根的子树包含的叶结点总数
int sz; // 结点总数
long long ans; // 答案
void clear() { sz = 1; tot[0] = head[0] = next[0] = 0; } // 初始时只有一个根结点 // 插入字符串s(包括最后的'\0'),沿途更新tot
void insert(const char *s) {
int u = 0, v, n = strlen(s);
tot[0]++;
for(int i = 0; i <= n; i++) {
// 找字符a[i]
bool found = false;
for(v = head[u]; v != 0; v = next[v])
if(ch[v] == s[i]) { // 找到了
found = true;
break;
}
if(!found) {
v = sz++; // 新建结点
tot[v] = 0;
ch[v] = s[i];
next[v] = head[u];
head[u] = v; // 插入到链表的首部
head[v] = 0;
}
u = v;
tot[u]++;
}
} // 统计LCP=u的所有单词两两的比较次数之和
void dfs(int depth, int u) {
if(head[u] == 0) // 叶结点
ans += tot[u] * (tot[u] - 1) * depth;
else {
int sum = 0;
for(int v = head[u]; v != 0; v = next[v])
sum += tot[v] * (tot[u] - tot[v]); // 子树v中选一个串,其他子树中再选一个
ans += sum / 2 * (2 * depth + 1); // 除以2是每种选法统计了两次
for(int v = head[u]; v != 0; v = next[v])
dfs(depth+1, v);
}
} // 统计
long long count() {
ans = 0;
dfs(0, 0);
return ans;
}
}; #include<cstdio>
const int maxl = 1000 + 10; // 每个单词最大长度 int n;
char word[maxl];
Trie trie; int main() {
int kase = 1;
while(scanf("%d", &n) == 1 && n) {
trie.clear();
for(int i = 0; i < n; i++) {
scanf("%s", word);
trie.insert(word);
}
printf("Case %d: %lld\n", kase++, trie.count());
}
return 0;
}
UVa 11732 "strcmp()" Anyone? (左儿子右兄弟前缀树Trie)的更多相关文章
- 左儿子右兄弟Trie UVA 11732 strcmp() Anyone?
题目地址: option=com_onlinejudge&Itemid=8&category=117&page=show_problem&problem=2832&qu ...
- UVA - 11732 "strcmp()" Anyone?左兄弟右儿子trie
input n 2<=n<=4000 s1 s2 ... sn 1<=len(si)<=1000 output 输出用strcmp()两两比较si,sj(i!=j)要比较的次数 ...
- UVA11732 "strcmp()" Anyone?【左儿子右兄弟Trie】
LINK1 LINK2 题目大意 给你一些字符串,并定义了一个函数(具体见题面) 问你把任意两个字符串放到函数里面得到的值的和是多少 思路 该怎么统计答案呢? 每次考虑当前插入的串和所有已经插入过的串 ...
- 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 ...
- Vijos p1518 河流 转二叉树左儿子又兄弟
左儿子又兄弟的转发一定要掌握啊,竞赛必用,主要是降低编程复杂度,省时间.个人觉得状压DP也是为了降低编程复杂度. 方程就不说了,程序应该能看得懂,用的记忆化搜索,方便理解. #include<c ...
- UVA 11732 - strcmp() Anyone?(Trie)
UVA 11732 - strcmp() Anyone? 题目链接 题意:给定一些字符串,要求两两比較,须要比較的总次数(注意.假设一个字符同样.实际上要还要和'\0'比一次,相当比2次) 思路:建T ...
- UVa 11732 strcmp()函数(左孩子右兄弟表示法)
#include<iostream> #include<algorithm> #include<string> #include<cstring> #i ...
- UVA 11732 strcmp() Anyone?(Trie的性质)
strcmp() Anyone? strcmp() is a library function in C/C++ which compares two strings. It takes two st ...
随机推荐
- 1 python 文件处理
1.打开文件open 函数 open函数最常用的使用方法如下:文件句柄 = open('文件路径', '模式',编码方式). encode='' 1.关于文件路径 #文件路径: 主要有两种,一种是使用 ...
- conductor Workflow Metrics
Server Metrics conductor使用spectator收集指标.https://github.com/Netflix/spectator 名称 目的 标签 workflow_serve ...
- java script btoa与atob的
javascript原生的api本来就支持,Base64,但是由于之前的javascript局限性,导致Base64基本中看不中用.当前html5标准正式化之际,Base64将有较大的转型空间,对于H ...
- org.Hs.eg.db包简介(转换NCBI、ensemble等数据库中基因ID,symbol等之间的转换)
1)安装载入 ------------------------------------------- if("org.Hs.eg.db" %in% rownames(install ...
- padding-top和margin-top的区别
学习前端知识,对我们查找页面元素很有帮助,而且自己在原公司时,有参与一个QA系统,自己去设计了这个产品,出了原型图,同时设计了几个页面,希望通过做这个产品提高自己的技术,可是因为离职,所以计划搁浅了, ...
- Failed to execute goal org.mybatis.generator:mybatis-generator-maven-plugin:generate (default-cli) on project : <properties> resource does not exist
使用mybatis-generator自动生成mapper.dao等文件时,报错如下: org.apache.maven.lifecycle.LifecycleExecutionException: ...
- racktables 的介绍及搭建指南
Racktables RackTables称自己为一个“机架空间.IP地址.服务器.交换机.路由器等 的管理框架”.它拥有一个web界面,执行报告和配置,并管理名字服务.RackTables以PHP5 ...
- 139. Word Break (String; DP)
Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...
- Our Journey of Xian Ends
Our Journey of Xian Ends https://nanti.jisuanke.com/t/18521 262144K Life is a journey, and the roa ...
- Metropolis(多源点最短路)
Metropolis https://www.nowcoder.com/acm/contest/203/I 题目描述 魔方国有n座城市,编号为.城市之间通过n-1条无向道路连接,形成一个树形结构. 在 ...