因为字符集比较大,所以就不能用简单字典树,在字典树里面,用链表进行存储。这个倒是不难,练了下手

统计的时候还是有点难搞,因为要算所有的两两比较的次数之和,对分叉处进行计算,注意细节

#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 链表+字典树的更多相关文章

  1. UVA 11732 strcmp() Anyone? (压缩版字典树)

    题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...

  2. UVA - 11488 字典树

    题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...

  3. UVA 11488 Hyper Prefix Sets (字典树)

    题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...

  4. HDU 1251 统计难题(字典树 裸题 链表做法)

    Problem Description Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本身也是自己 ...

  5. UVA - 12333 字典树+大数

    思路:用字典树将前40个数字记录下来,模拟大数加法即可. AC代码 #include <cstdio> #include <cmath> #include <algori ...

  6. UVA Phone List (字典树)(查询是否有前缀或自身是其他的前缀)

    Phone List Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16341   Accepted: 5228 Descr ...

  7. UVA - 12333 Revenge of Fibonacci 高精度加法 + 字典树

    题目:给定一个长度为40的数字,问其是否在前100000项fibonacci数的前缀 因为是前缀,容易想到字典树,同时因为数字的长度只有40,所以我们只要把fib数的前40位加入字典树即可.这里主要讨 ...

  8. uva 11488 - Hyper Prefix Sets(字典树)

    H Hyper Prefix Sets Prefix goodness of a set string is length of longest common prefix*number of str ...

  9. UVa 12333 - Revenge of Fibonacci manweifc(模拟加法竖式 & 字典树)

    题意: 给定n个(n<=40)数字, 求100000个以内有没有前面n个数字符合给定的数字的fibonacci项, 如果有, 给出最小的fibonacci项, 如果没有, 输出-1. 分析: 可 ...

随机推荐

  1. CentOS7 防火墙设置

    CentOS7 防火墙命令 最近在公司服务器上安装了oracle12c数据库,在用数据库客户端连接的时候,连接不了.最后查找资料的原因是因为oracle的服务端口未开放. 首先还是还是输入以往的开启某 ...

  2. ErrorCode=-2147217900 表已存在.

    ErrorCode=-2147217900 表已存在. 在导出excel时遇到这个问题. 原因是dataTable的TableName中有减号 "-"

  3. TCP/IP 三次握手,四次断开

    TCP/IP 三次握手,四次断开 一.TCP报文格式                     TCP/IP协议的详细信息参看<TCP/IP协议详解>三卷 本. 下面是TCP报文格式图: 图 ...

  4. 洛谷P1002——过河卒

    又是洛谷题,要不是有小姐姐不会,我才不想动脑子.先贴一下题目地址https://www.luogu.org/problem/P1002 再贴一下题目: 我们读一下题目,这可不比学校的**算法题,读完一 ...

  5. UVA - 211 The Domino Effect(多米诺效应)(dfs回溯)

    题意:根据多米诺骨牌的编号的7*8矩阵,每个点可以和相邻的点组成的骨牌对应一个编号,问能形成多少种由编号组成的图. 分析:dfs,组成的图必须有1~28所有编号. #pragma comment(li ...

  6. liunx笔记

    Zolertia IPv6/6LoWPAN Ubidots client Son Han Border Router with Raspberry Pi for LLN with TelosBs Co ...

  7. 安装python包的两种方法

    1.在 anaconda 环境中安装包 selenium conda install selenium 2.python 下安装包 selenium pip install selenium 3.测试 ...

  8. 七十一、SAP中内表的修改,改一行数据,或一行的某个字段

    一.SAP中内表的修改,只能通过工作区来修改,代码如下 二.效果如下

  9. MacType

    #前言 这几天实在是嫌弃Win10垃圾的字体渲染效果--发虚模糊,索性从网上找了个系统字体渲染软件即MacType给系统字体改头换面. #使用效果 这里贴出两个场景的效果对比(单击图片查看具体效果) ...

  10. spring boot配置druid连接池连接mysql

    Spring Boot 集成教程 Spring Boot 介绍 Spring Boot 开发环境搭建(Eclipse) Spring Boot Hello World (restful接口)例子 sp ...