左儿子右兄弟Trie UVA 11732 strcmp() Anyone?
题目地址:
题意:
问strcmp函数的==语句运行了几次。
分析:
大白上的题目。
听说要用左儿子右兄弟的Trie。比較省空间。顺便学了下。
一边insert一边统计。
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; #define repf(i,a,b) for(int i=(a);i<=(b);i++)
typedef long long ll; const int N = 0;
const int MAXNODE = 4000010; int n, cas;
ll ans;
char str[4001]; struct STrie {
int son[MAXNODE];
int bro[MAXNODE];
int val[MAXNODE];
char ch[MAXNODE];
int sz; STrie() { sz = 1; ch[0] = val[0] = bro[0] = son[0] = 0; }
void init() { sz = 1; ch[0] = val[0] = bro[0] = son[0] = 0; }
// inline int idx(char c) { return c - 'a'; } void insert(char *s) {
int len = strlen(s), u = 0, p;
repf (i, 0, len) {
// check the brother of u
for (p = son[u]; p; p = bro[p]) {
if (ch[p] == s[i])
break;
}
// cannot find out than insert
if (!p) {
p = sz++;
ch[p] = s[i];
bro[p] = son[u];
son[p] = 0;
val[p] = 0;
son[u] = p;
}
ans += (val[u] - val[p]) * (2 * i + 1);
if (len == i) {
ans += val[p] * (2 * i + 2);
val[p]++;
}
val[u]++;
u = p;
}
}
} trie; int main() {
// ios_base::sync_with_stdio(0);
while (~scanf("%d", &n) && n) {
trie.init();
ans = 0;
repf (i, 0, n - 1) {
scanf("%s", str);
trie.insert(str);
}
printf("Case %d: %lld\n", ++cas, ans);
}
return 0;
}
// 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;
}
左儿子右兄弟Trie UVA 11732 strcmp() Anyone?的更多相关文章
- UVA11732 "strcmp()" Anyone?【左儿子右兄弟Trie】
LINK1 LINK2 题目大意 给你一些字符串,并定义了一个函数(具体见题面) 问你把任意两个字符串放到函数里面得到的值的和是多少 思路 该怎么统计答案呢? 每次考虑当前插入的串和所有已经插入过的串 ...
- UVa 11732 "strcmp()" Anyone? (左儿子右兄弟前缀树Trie)
题意:给定strcmp函数,输入n个字符串,让你用给定的strcmp函数判断字符比较了多少次. 析:题意不理解的可以阅读原题https://uva.onlinejudge.org/index.php? ...
- Trie UVA 11732 "strcmp()" Anyone?
题目传送门 题意:询问所有字符串的比较次数和(注意for循环内的比较也算) 分析:将所有字符串插入到字典树上,然后结点信息记录有几个字符串,那么每走到一个结点就能知道比较到此时需要的次数.学习到链表存 ...
- 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?(Trie)
UVA 11732 - strcmp() Anyone? 题目链接 题意:给定一些字符串,要求两两比較,须要比較的总次数(注意.假设一个字符同样.实际上要还要和'\0'比一次,相当比2次) 思路:建T ...
- Vijos p1518 河流 转二叉树左儿子又兄弟
左儿子又兄弟的转发一定要掌握啊,竞赛必用,主要是降低编程复杂度,省时间.个人觉得状压DP也是为了降低编程复杂度. 方程就不说了,程序应该能看得懂,用的记忆化搜索,方便理解. #include<c ...
- 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 ...
随机推荐
- net3:DropDownList的动态绑定
原文发布时间为:2008-07-29 -- 来源于本人的百度文章 [由搬家工具导入] using System.Data;using System.Configuration;using System ...
- Select函数实现
int select(int nfds, fd_set *restrict readfds, fd_set *restrict writefds, fd_set *restrict errorfds, ...
- Android 4.4 不休眠+不锁屏+默认中文+去除导航栏
1.不休眠 frameworks/base/packages/SettingsProvider/res/values/defaults.xml 里面60000改成-1,就是不进入休眠. 这个文件还保存 ...
- UPC 2219: A^X mod P
题形:另类快速幂 题意: f(x) = K, x = 1 f(x) = (a*f(x-1) + b)%m , x > 1 Now, Your task is to calculate ( A^( ...
- 本地hosts文件
(1)什么是Hosts文件? Hosts是一个没有扩展名的系统文件,可以用记事本等工具打开,其作用就是将一些常用的网址域名与其对应的IP地址建立一个关联“数据库”,当用户在浏览器中输入一个需要登录的网 ...
- Codeforces635C XOR Equation【数学】
题目链接: http://codeforces.com/contest/635/problem/C 题意: 给定两个数的和s及异或x,求两个数的可能情况. 分析: 我们有公式a+b=a& b∗ ...
- 洛谷——P2196 挖地雷
题目背景 NOIp1996提高组第三题 题目描述 在一个地图上有N个地窖(N<=20),每个地窖中埋有一定数量的地雷.同时,给出地窖之间的连接路径.当地窖及其连接的数据给出之后,某人可以从任一处 ...
- 布斯(Steve Jobs)在斯坦福大学的演讲稿,中英文对照版
2005年6月14日,苹果CEO史蒂夫·乔布斯(Steve Jobs)在他的母校斯坦福大学的毕业典礼发表了著名的演讲,关于这段演讲,你会看到N多人的推荐(比如同样喜欢在大学演讲的李开复先生).此前曾经 ...
- 管理SQL Server监控
http://blog.csdn.net/DBA_Huangzj/article/category/1133081 http://www.cnblogs.com/bhtfg538/archive/20 ...
- asp.net Excel导入&导出
1.Excel数据导入到数据库中: //该方法实现从Excel中导出数据到DataSet中,其中filepath为Excel文件的绝对路径,sheetname为表示那个Excel表: p ...