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. 分析: 可 ...
随机推荐
- SQL创建表格——手写代码
打开phpstudy,打开Navicat for MySQL,进入要创建表格的数据库,点击上方“查询”按钮,“创建查询”,即可输入代码进行创建. 例: create table class( clas ...
- 吴裕雄 Bootstrap 前端框架开发——Bootstrap 字体图标(Glyphicons):glyphicon glyphicon-file
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name ...
- SQL注入汇总(手注,盲注,报错注入,宽字节,二次编码,http头部){10.22、23 第二十四 二十五天}
首先什么是SQL注入: 所谓SQL注入,就是通过把SQL命令插入到Web表单提交或输入域名或页面请求的查询字符串,最终达到欺骗服务器执行恶意的SQL命令. SQL注入有什么危害? 危害:数据泄露.脱库 ...
- 实训23 功能FC的建立与调用
第4章:实训23 功能的生成与条用 功能简称FC 是用户编写的没有自己存储区的逻辑块 . 功能主要用来执行条用一次就可以完成的操作. 类似于C语言中的 函数 步骤一 单击确定 以后 出现了 在下面图框 ...
- 32位CPU和64位CPU 区别
操作系统只是硬件和应用软件中间的一个平台. 32位操作系统针对的32位的CPU设计. 64位操作系统针对的64位的CPU设计.操作系统只是硬件和应用软件中间的一个平台. 32位操作系统针对的32位的C ...
- Set Request Header
第1步 webpack package.json 的 scripts 里的 dev 要新增 --host 0.0.0.0 这样在浏览器里用ip地址代替localhost就能运行了 第2步 在pc浏览器 ...
- tomcat启动报错The JRE could not be found.Edit the server and change the JRE location
解决: 在Windows->Preferences->Server->Runtime Environments 选择Tomcat->Edit,在jre中选择相应的jdk版本,完 ...
- 怎么更改Rstudio中的默认目录
方法一. 每次启动Rstudio之后,执行代码 setwd("F:/R/R_data") 默认目录就会修改为双引号内的位置路径. 方法二. 对Rstudio进行设置一次即可. ①点 ...
- tomcat以服务形式开机启动
第一步 将tomcat配置为服务模式 cp $TOMCAT_HOME/bin/catalina.sh /etc/init.d/tomcat #将tocmt启动文件复制到/etc/init.d下 vim ...
- ORIGIN(起源属性)路由起源骗术
ORIGIN(起源属性)配置: ①:抓取感兴趣流量——prefix.access ②:创建route-map 流量地图——permit 10 ③:匹配感兴趣流量——match ④:设置起源属性——se ...