UVA 11732 链表+字典树
因为字符集比较大,所以就不能用简单字典树,在字典树里面,用链表进行存储。这个倒是不难,练了下手
统计的时候还是有点难搞,因为要算所有的两两比较的次数之和,对分叉处进行计算,注意细节
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int N = *+;
struct Trie
{
int head[N];
int next[N];
char ch[N];
int tot[N];
int sz;
long long ans;
void clear()
{
sz=;
head[]=next[]=tot[]=;
ans=;
}
void insert(const char* s)
{
int u=,v,len;
len=strlen(s);
tot[]++;
for (int i=; i<=len; i++)
{
bool found=false;
for (v=head[u]; v!=; v=next[v])
{
if (ch[v]==s[i])
{
found=true;
break;
}
}
if (!found)
{
v=sz++;
ch[v]=s[i];
tot[v]=;
next[v]=head[u];
head[u]=v;
head[v]=;
}
u=v;
tot[u]++;
}
} void dfs(int depth, int u) //计算总数
{
if(head[u] == )
ans += tot[u] * (tot[u] - ) * depth;
else
{
int sum = ;
for(int v = head[u]; v != ; v = next[v])
sum += tot[v] * (tot[u] - tot[v]);
ans += sum / * ( * depth + );
for(int v = head[u]; v != ; v = next[v])
dfs(depth+, v);
}
} long long count()
{
ans = ;
dfs(, );
return ans;
}
} T;
char str[];
int main()
{
int n; int kase=;
while (scanf("%d",&n) && n)
{
T.clear();
for (int i=; i<=n; i++)
{
scanf("%s",str);
T.insert(str);
}
T.count();
printf("Case %d: %lld\n",++kase,T.ans);
}
return ;
}
UVA 11732 链表+字典树的更多相关文章
- UVA 11732 strcmp() Anyone? (压缩版字典树)
题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...
- UVA - 11488 字典树
题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...
- UVA 11488 Hyper Prefix Sets (字典树)
题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...
- HDU 1251 统计难题(字典树 裸题 链表做法)
Problem Description Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本身也是自己 ...
- UVA - 12333 字典树+大数
思路:用字典树将前40个数字记录下来,模拟大数加法即可. AC代码 #include <cstdio> #include <cmath> #include <algori ...
- UVA Phone List (字典树)(查询是否有前缀或自身是其他的前缀)
Phone List Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16341 Accepted: 5228 Descr ...
- UVA - 12333 Revenge of Fibonacci 高精度加法 + 字典树
题目:给定一个长度为40的数字,问其是否在前100000项fibonacci数的前缀 因为是前缀,容易想到字典树,同时因为数字的长度只有40,所以我们只要把fib数的前40位加入字典树即可.这里主要讨 ...
- uva 11488 - Hyper Prefix Sets(字典树)
H Hyper Prefix Sets Prefix goodness of a set string is length of longest common prefix*number of str ...
- UVa 12333 - Revenge of Fibonacci manweifc(模拟加法竖式 & 字典树)
题意: 给定n个(n<=40)数字, 求100000个以内有没有前面n个数字符合给定的数字的fibonacci项, 如果有, 给出最小的fibonacci项, 如果没有, 输出-1. 分析: 可 ...
随机推荐
- bean的autowire属性及其生命周期
一:sutowire属性 1.no:默认值,禁用自动装配: 2.byName:根据属性名称自动装配: 3.byType:根据属性类型自动装配: 4.constructor:通过构造方法自动装配,不推荐 ...
- 【剑指Offer】面试题28. 对称的二叉树
题目 请实现一个函数,用来判断一棵二叉树是不是对称的.如果一棵二叉树和它的镜像一样,那么它是对称的. 例如,二叉树 [1,2,2,3,4,4,3] 是对称的. 1 / \ 2 2 ...
- mqtt已断开连接(32109)
在rabbitmq下使用mqtt协议时,如果服务质量(qos)设置为2,在发布服务时会出现[已断开连接 (32109) - java.io.EOFException]的报错. 出现该报错的其他情况还有 ...
- P 1038 统计同成绩学生
转跳点:
- 19 03 13 关于 scrapy 框架的 对环球网的整体爬取(存储于 mongodb 数据库里)
关于 spinder 在这个框架里面 和不用数据库 相同 # -*- coding: utf-8 -*- import scrapy from yang_guan.items import ...
- BZOJ 4853 [Jsoi2016]飞机调度
题解: 我严重怀疑语文水平(自己的和出题人的) 把航线按照拓扑关系建立DAG 然后最小路径覆盖 为什么两条首尾相接航线之间不用维护???? #include<iostream> #incl ...
- SQL decode 函数的用法
decode 函数基本语法: decode(字段|表达式,条件1,结果1,条件2,结果2,...,条件n,结果n,缺省值): --缺省值可以省略 表示如果 字段|表达式 等于 条件1 时,DECODE ...
- IDEA 分屏显示
效果: 步骤: 对着某个标签页单击右键,选择Split Vertically或者Split Horizontally即可.
- sping MVC 定时任务的设置
项目中用到了定时任务,写一篇随笔记录一下. 首先在Spring的配置文件ApplicationContext.xml文件的beans标签中添加 xmlns:task="http://www. ...
- Linux 下 zip 文件解压乱码如何解决
作者:Latm Ake链接:https://www.zhihu.com/question/20523036/answer/35225920来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业 ...