一開始不知道这样的一维建树方法。

每次一层用一个链指向下一层最左边的结点,之后每一层用一个链表串联全部的结点,这样就建树成功了。

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?的更多相关文章

  1. CJOJ 1070 【Uva】嵌套矩形(动态规划 图论)

    CJOJ 1070 [Uva]嵌套矩形(动态规划 图论) Description 有 n 个矩形,每个矩形可以用两个整数 a, b 描述,表示它的长和宽.矩形 X(a, b) 可以嵌套在矩形 Y(c, ...

  2. CJOJ 1071 【Uva】硬币问题(动态规划)

    CJOJ 1071 [Uva]硬币问题(动态规划) Description 有n种硬币,面值分别为v1, v2, ..., vn,每种都有无限多.给定非负整数S,可以选用多少个硬币,使得面值之和恰好为 ...

  3. 题解 【Uva】硬币问题

    [Uva]硬币问题 Description 有n种硬币,面值分别为v1, v2, ..., vn,每种都有无限多.给定非负整数S,可以选用多少个硬币,使得面值之和恰好为S?输出硬币数目的最小值和最大值 ...

  4. 【UVa】11270 Tiling Dominoes

    http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...

  5. 【UVA】【10828】随机程序

    数学期望/高斯消元/马尔可夫过程 刘汝佳老师白书上的例题- -b 本体不满足拓扑关系,但马尔可夫过程是可以高斯消元解的…… 用「高斯·约当消元」更方便! //UVA 10828 #include< ...

  6. 【UVA】【11762】Race to 1(得到1)

    数学期望/马尔可夫过程 DP/记忆化搜索 刘汝佳老师白书上的例题…… //UVA 11762 #include<vector> #include<cstdio> #includ ...

  7. 【UVA】【11427】玩纸牌

    数学期望 也是刘汝佳老师白书上的例题……感觉思路很神奇啊 //UVA 11427 #include<cmath> #include<cstdio> #include<cs ...

  8. 【UVA】【11021】麻球繁衍

    数序期望 刘汝佳老师的白书上的例题……参见白书 //UVA 11021 #include<cmath> #include<cstdio> #define rep(i,n) fo ...

  9. 【UVa】Headmaster's Headache(状压dp)

    http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...

随机推荐

  1. A transition animation compatible Library.

    Android5.0之后为我们提供了许多炫酷的界面过渡效果,其中共享元素过渡也是很有亮点的一个效果,但这个效果只能在Android5.0之后使用,那今天我们就来将共享元素过渡效果兼容到Android4 ...

  2. angularjs入门整理

    之前发过一篇博文,从mobile angular ui的demo和其官网初识整个angularjs的大体使用,但是没很好学习,只是通过一些技术博文初步认识,陷入很多坑.所以现在在中文官网正式整理下知识 ...

  3. cvThreshold()函数理解

    对图像二值化函数cvThreshold的理解 Threshold 对数组元素进行固定阈值操作 void cvThreshold( const CvArr* src, CvArr* dst, doubl ...

  4. 【python】【转】python中isinstance判断变量类型用法

    来源 http://www.jb51.net/article/15696.htm 在Python中只需要使用内置的函数isinstance,使用起来非常简单,比如下面的例子: 复制代码 代码如下: c ...

  5. VC皮肤库之duilib

    首先是个国产的开源 的,directui 界面库,开放,共享,惠众,共赢,遵循bsd协议,可以免费用于商业项目,目前支持Windows 32 .Window CE.Mobile等平台. Duilib ...

  6. NSURLSession -- 备忘

    NSURLSession NSURLSession是iOS7出的API,在它之前常用的原生网络库是NSURLConnection,但是因为Connection的使用起来不是很方便 所以我们一直倾向于A ...

  7. golang开发android环境搭建_window

    golang开发android环境搭建介绍 一 安装依赖软件: git:版本管理 go:  go开发环境(版本>=1.5),可直接下载window版的go安装包. android studio: ...

  8. Codeforces Round #205 (Div. 2) : C

    感觉像是一个数位dp,高位的1如果不选的话,前面低位的数都可以选:不然只能选择为1的数: 代码: #include<iostream> #include<algorithm> ...

  9. Hibernate 注意命名与数据库关键字的冲突 处理方法

    比如你映射了一个名称为key的属性,这是数据库所不允许的,因为它是数据库的关键字. 因此,你必须为此属性添加一对符号,即键盘上“1”键的左边的按键.

  10. java 中的访问修饰符

    一. public:所有类都可以访问 protected:所有子类和同包下的类都可以访问 缺省:同包类都可以访问 private:类本身才可以访问 注意点:protected修饰类属性时,例如 pac ...