【UVA】11732 - strcmp() Anyone?
一開始不知道这样的一维建树方法。
每次一层用一个链指向下一层最左边的结点,之后每一层用一个链表串联全部的结点,这样就建树成功了。
| 14328524 | 11732 |
option=com_onlinejudge&Itemid=8&page=show_problem&problem=2832" style="font-size:13.3333330154419px; margin:0px; padding:0px; color:rgb(153,0,0); text-decoration:none">strcmp() Anyone? |
Accepted | C++ | 0.826 | 2014-10-09 13:13:14 |
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long LL;
typedef pair<int,int> pill;
const int nodemaxn = 4000 * 1000 + 100;
struct Trie{
int head[nodemaxn]; //左儿子链
int next[nodemaxn]; //右兄弟链
int ch[nodemaxn]; //这个结点代表的字符
LL tot[nodemaxn]; //有几个单词经过了这个结点
int sz; //结点个数
LL ret; //比較的次数
void clear(){
head[0] = 0; next[0] = 0; tot[0] = 0; sz = 1; ret = 0;
}
void insert(char *str){
int L = strlen(str);
int u = 0,v,found;
tot[0]++;
for(int i = 0; i <= L; i ++){
found = 0;
for(v = head[u]; v != 0 ; v = next[v]){ //到下一层去找
if(ch[v] == str[i]){
found = 1;
break;
}
}
if(!found){ //假设没有找到
v = sz ++;
tot[v] = 0;
ch[v] = str[i];
next[v] = head[u];
head[u] = v;
head[v] = 0;
}
u = v;
tot[u] ++;
}
}
void dfs(int u,int depth){
if(head[u] == 0){
ret += tot[u] * (tot[u] - 1) * depth;
}
else{
LL sum = 0;
for(int v = head[u] ; v != 0; v = next[v])
sum += tot[v] * (tot[u] - tot[v]);
ret += sum / 2 * (2 * depth + 1);
for(int v = head[u] ; v != 0; v = next[v])
dfs(v,depth + 1);
}
}
}trie;
const int maxn = 4000 + 10;
int main(){
int n,Case = 1;
while(scanf("%d",&n) && n){
trie.clear();
for(int i = 0; i < n; i++){
char str[maxn];
scanf("%s",str);
trie.insert(str);
}
trie.dfs(0,0);
printf("Case %d: %lld\n",Case++,trie.ret);
}
}
【UVA】11732 - strcmp() Anyone?的更多相关文章
- CJOJ 1070 【Uva】嵌套矩形(动态规划 图论)
CJOJ 1070 [Uva]嵌套矩形(动态规划 图论) Description 有 n 个矩形,每个矩形可以用两个整数 a, b 描述,表示它的长和宽.矩形 X(a, b) 可以嵌套在矩形 Y(c, ...
- CJOJ 1071 【Uva】硬币问题(动态规划)
CJOJ 1071 [Uva]硬币问题(动态规划) Description 有n种硬币,面值分别为v1, v2, ..., vn,每种都有无限多.给定非负整数S,可以选用多少个硬币,使得面值之和恰好为 ...
- 题解 【Uva】硬币问题
[Uva]硬币问题 Description 有n种硬币,面值分别为v1, v2, ..., vn,每种都有无限多.给定非负整数S,可以选用多少个硬币,使得面值之和恰好为S?输出硬币数目的最小值和最大值 ...
- 【UVa】11270 Tiling Dominoes
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...
- 【UVA】【10828】随机程序
数学期望/高斯消元/马尔可夫过程 刘汝佳老师白书上的例题- -b 本体不满足拓扑关系,但马尔可夫过程是可以高斯消元解的…… 用「高斯·约当消元」更方便! //UVA 10828 #include< ...
- 【UVA】【11762】Race to 1(得到1)
数学期望/马尔可夫过程 DP/记忆化搜索 刘汝佳老师白书上的例题…… //UVA 11762 #include<vector> #include<cstdio> #includ ...
- 【UVA】【11427】玩纸牌
数学期望 也是刘汝佳老师白书上的例题……感觉思路很神奇啊 //UVA 11427 #include<cmath> #include<cstdio> #include<cs ...
- 【UVA】【11021】麻球繁衍
数序期望 刘汝佳老师的白书上的例题……参见白书 //UVA 11021 #include<cmath> #include<cstdio> #define rep(i,n) fo ...
- 【UVa】Headmaster's Headache(状压dp)
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...
随机推荐
- apache2.4配置虚拟主机
step1 启用 httpd-vhosts.conf 找到E:/apache/Apache24/conf 中httpd.conf 文件,取消注释下面这句话 step2 在 httpd-vhosts.c ...
- Python 学习之urllib模块---用于发送网络请求,获取数据
1.urllib urllib是Python标准库的一部分,包含urllib.request,urllib.error,urllib.parse,urlli.robotparser四个子模块. (1) ...
- mac+eclipse+svn+maven经验总结(转)
1.Eclipse for Mac:http://mirrors.ustc.edu.cn/eclipse/technology/epp/downloads/release/indigo/SR2/ecl ...
- 简单学c——前言
1.学C语言需要什么基础吗? 零基础. 2.什么是C语言? C语言是一种编程语言. 3.什么是编程语言? 编程语言是用来定义计算机程序的形式语言,是一种被标准化的交流技巧,用来向计算机发出指令. ...
- jquery添加元素
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <head> ...
- php调用linux命令
php有以下接口可提供执行外部函数: system() exec() popen() 但要使用上面几个函数,首先,要配置php.ini配置文件.修改配置文件如下: safe_mode = off; 改 ...
- Qt 窗体的模态与非模态(setWindowFlags(Qt::WindowStaysOnTopHint);比较有用,还有Qt::WA_DeleteOnClose)
概念 模态对话框(Modal Dialog)与非模态对话框(Modeless Dialog)的概念不是Qt所独有的,在各种不同的平台下都存在.又有叫法是称为模式对话框,无模式对话框等. 1. 模态窗体 ...
- LeetCode 面试:Add Two Numbers
1 题目 You are given two linked lists representing two non-negative numbers. The digits are stored in ...
- hadoop-2.0.0-mr1-cdh4.2.0源码编译总结
准备编译hadoop-2.0.0-mr1-cdh4.2.0的同学们要谨慎了.首先看一下这篇文章: Hadoop作业提交多种方案 http://www.blogjava.net/dragonHadoop ...
- 14.8.3 Physical Row Structure of InnoDB Tables InnoDB 表的物理行结构
14.8.3 Physical Row Structure of InnoDB Tables InnoDB 表的物理行结构 一个InnoDB 表的物理行结构取决于在创建表指定的行格式 默认, Inno ...