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

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

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. pushState与replaceState区别

    history.pushState(state, title, url) 将当前URL和history.state加入到history中,并用新的state和URL替换当前.不会造成页面刷新. sta ...

  2. php Imagick库readImage()报Postscript delegate failed 解决方法(失效)

    需要安装 ghostscript http://www.ghostscript.com/download/gsdnld.html

  3. string与char* 互相转换以及周边问题

    先插一个小知识点 string str = "abc" str += 'd'; cout<<str<<endl;  //"abcd"   ...

  4. 目前最流行的网页自动运行EXE文件

    大家对木马都不陌生了,它可能要算是计算机病毒史上最厉害的了,相信会使木马的人千千万万,但是有很多人苦于怎么把木马发给对方,现在随着计算机的普及,在网络上我相信很少有人会再轻易的接收对方的文件了,所以网 ...

  5. struts2中的标签“# ”,“%{ }”,“%{# }”

    理解值栈(ValueStack)与上下文(StackContext):            Struts2中有值堆栈和堆栈上下文的概念,你用 <s:debug />可以看出. 值栈中的对 ...

  6. WPF的模版

    此例子来自<深入浅出WPF>,刘铁猛. VS2010 源码1:不使用Bingding 源码2:使用Binding 下面展示一些代码: 主窗体XAML代码: <Window x:Cla ...

  7. Contest20140710 eagleeggs

    eagleeggs|eagleeggs.in|eagleeggs.out 题目描述: 共有N个硬度相同的鹰蛋,硬度是一个整数(并且已知其不大于H),表示这个蛋从天上掉下来不摔碎的最大高度.为了找出这个 ...

  8. 【POJ1275】Cashier Employment

    题目: Description A supermarket in Tehran is open 24 hours a day every day and needs a number of cashi ...

  9. Qt中连接到同一signal的多个slots的执行顺序问题(4.6以后按连接顺序执行)

    起源 前些天忘记在哪儿讨论过这个问题,今天在csdn又看到有网友问这个问题,而其他网友却无一例外的给出了“无序”这个答案. Manual Qt的问题,当manual中有明确文字说明时,我们应该以Qt的 ...

  10. 3.1日 重温JVM相关信息

    1.JDK.JRE.JVM的关系: JDK是java开发的必备工具箱,JDK其中有一部分是JRE,JRE是JAVA运行环境,JVM则是JRE最核心的部分. 2.JVM的组成: JVM由4大部分组成:C ...