uva11732 Trie转化
有40001 个单词每个单词长度不超过1000,每个两个单词之间都要比较求要比较次数
int strcmp(char *s,char *t){
int i;
for(i = 0; s[i]==t[i]; ++i)
if(s[i]=='\0')
return 0;
return s[i]-t[i];
}
如果我们按照这样的去比较会超时,采用字典树,用孩子兄弟法求
#include <iostream>
#include <string.h>
#include <cstdio>
using namespace std;
const int maxn = *+;
struct Trie{
int head[maxn];
int next[maxn];
char ch[maxn];
int tot[maxn];
int sz;
long long ans;
void clear(){
sz=;
head[]=next[]=tot[]=;
}
int idx(char c){ return c-'a';}
void insert(char *s){
int n= strlen(s);
int u=,v;
tot[]++;
for(int i=; i<=n; ++i){ bool found = false;
for( v = head[u]; v; v=next[v])
if(ch[v]==s[i]){ found=true; break; }
if(found==false){
v= sz++;
tot[v]=;
head[v]=;
next[v]=head[u];
head[u]=v;
ch[v]=s[i];
}
u=v;
++tot[u];
}
}
void dfs(int u,int len){
if(head[u]==){
ans+=tot[u]*(tot[u]-)*len;
}else{
int sum=;
for(int v= head[u]; v; v=next[v])
sum+=tot[v]*(tot[u]-tot[v]);
ans+= sum/*(*len+);
for(int v= head[u]; v; v=next[v])
dfs(v,len+);
} }
long long cut(){
ans=;
dfs(,);
return ans;
} }A;
char str[];
int main()
{
int n;
int cas=;
while(scanf("%d",&n)==&&n){
A.clear();
for(int i=; i<n; ++i){
scanf("%s",str);
A.insert(str);
}
printf("Case %d: %lld\n",cas++,A.cut());
}
return ;
}
uva11732 Trie转化的更多相关文章
- UVA11732(Trie树)
鸣谢https://blog.csdn.net/Baoli1008/article/details/4441936,基本都是抄的代码 #pragma GCC optimize(2) #include ...
- 从Trie树(字典树)谈到后缀树
转:http://blog.csdn.net/v_july_v/article/details/6897097 引言 常关注本blog的读者朋友想必看过此篇文章:从B树.B+树.B*树谈到R 树,这次 ...
- [算法]从Trie树(字典树)谈到后缀树
我是好文章的搬运工,原文来自博客园,博主July_,地址:http://www.cnblogs.com/v-July-v/archive/2011/10/22/2316412.html 从Trie树( ...
- UVa11732 "strcmp()" Anyone?(Trie树+孩子兄弟表示法)
我的做法是先建字典树,统计每个结点出现次数和相同字符串个数,每个结点对答案的贡献就是2*C(次数,2),然后再分别讨论相同字符串和不同字符串对答案的贡献. 另外这题主要就是Trie树的孩子兄弟表示法: ...
- Uva11732(trie)
题意:给你n个字符串 用strcmp()两两比较 ,求字符比较的总次数 分析: 数据量很大我们考虑用孩子兄弟表示法来表示字典树 #include <cstdio> #include < ...
- UVA11732 "strcmp()" Anyone?【左儿子右兄弟Trie】
LINK1 LINK2 题目大意 给你一些字符串,并定义了一个函数(具体见题面) 问你把任意两个字符串放到函数里面得到的值的和是多少 思路 该怎么统计答案呢? 每次考虑当前插入的串和所有已经插入过的串 ...
- [bzoj4567][Scoi2016]背单词-Trie+贪心+模型转化
Brief Description 给你N个互不相同的字符串,记\(S_i\)为第i个字符串,现在要求你指定N个串的出现顺序,我们用\(V_i\)表示第i个字符串是第几个出现的,则V为1到N的一个排列 ...
- Trie(字典树)解析及其在编程竞赛中的典型应用举例
摘要: 本文主要讲解了Trie的基本思想和原理,实现了几种常见的Trie构造方法,着重讲解Trie在编程竞赛中的一些典型应用. 什么是Trie? 如何构建一个Trie? Trie在编程竞赛中的典型应用 ...
- Poj The xor-longest Path 经典题 Trie求n个数中任意两个异或最大值
Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 5646 Accepted: 1226 Description In an ...
随机推荐
- VS添加命令行参数main(int argc, char** argv)
- Android中Parcelable和Serializable接口用法
1. Parcelable接口 Interface for classes whose instances can be written to and restored from a Parcel. ...
- php学习九:存取器&魔术方法
当声明属性的时候用到了private,那么的话只能本类中才能访问此属性,其他地方无法使用,这个时候需要给外界提供一些方法来使外界访问 方法一:存取器: 可以提供一个public的方法来使外界对类里面的 ...
- 【linux】Crontab 定时任务 使用实例
1 使用putty 登录linux 服务器 2 输入以下命令.查看已有的定时任务 crontab -l 3 输入 以下命令,进入定时任务文件 crontab -e 4 键盘 选择 i 键 进行输 ...
- Android 菜单动态变化【添加或去除】
<menu xmlns:android="http://schemas.android.com/apk/res/android"> <group android: ...
- Python 中的线程-进程2
原文:https://www.cnblogs.com/i-honey/p/7823587.html Python中实现多线程需要使用到 threading 库,其中每一个 Thread类 的实例控制一 ...
- Appium中长按按钮操作
在一次项目中,appium要对某个按钮进行长按操作(大于2s),类似拍微信小视频,参考网上长按视频会报错 action1 = TouchActions(self.driver) el = self.d ...
- Android DatepickerDialog(日期选择器)的使用
效果图如下: 日期和时间选择对话框,首先是要获得当前时间,这里用 java类中的Calendar来获得日期和时间(也可以用Date,但是不提倡,Date部分方法已经注释为过时), Calendar是一 ...
- mysql动态sql 整理多个字段
原始表: 整理后的表: 方案一(动态sql): BEGIN #Routine body goes here... DECLARE v1 ); DECLARE v2 ); #DECLARE v3 VAR ...
- 设备加速传感器(accelerometer) ---- HTML5+
模块:Accelerometer Accelerometer模块管理设备加速度传感器,用于获取设备加速度信息,包括x(屏幕水平方向).y(垂直屏幕水平方向).z(垂直屏幕平面方向)三个方向的加速度信息 ...