Trie/最短的名字
/*
简单trie树的应用,注意在初始化的时候要把cnt也初始化,不然,WA!
下面的四分代码各有特点
*/
//数组型,名字查询。
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn=1000000;
struct tire{
int wd[27];
int cnt;
void init()
{
cnt=0;
memset(wd,0,sizeof(wd));
}
};
tire tree[maxn];
int tot=1,n,m;
void Insert(char* s,int root)
{
for(int i=0;s[i];i++)
{
int k=s[i]-'a';
if(!tree[root].wd[k])
{
tree[tot].init();
tree[root].wd[k]=tot++;
}
root=tree[root].wd[k];
tree[root].cnt++;
}
}
int sum=0;
int query (int root)
{
int sum=0;
for(int i=0;i<=25;i++)
{
if(tree[root].wd[i])
{
int ro=tree[root].wd[i];
sum+=tree[ro].cnt;
if(tree[ro].cnt>1)
sum+=query(ro);
}
}
return sum;
}
char name[1000000+5];
int main ()
{
int T;scanf("%d",&T);
while(T--)
{
int root=0;
tot=1;
tree[root].init();
int n;scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%s",name);
Insert(name, root);
}
printf("%d\n",query(root));
}
return 0;
}
//指针型,DFS查询
#include<cstdio>
#include<cstring>
#include<iostream>
const int maxn=1000000+5;
const int si=26;
int n;
char name[maxn];
struct node
{
int n;
node *chi[si];
void init()
{
n=0;
for(int i=0;i<si;i++)
chi[i]=NULL;
}
};
void Insert(char *s,node *root)
{
for(int i=0;s[i];i++)
{
if(root->chi[s[i]-'a']==NULL)
{
node *t=(node *)malloc(sizeof(node));
t->init();
root->chi[s[i]-'a']=t;
}
root=root->chi[s[i]-'a'];
root->n++;
}
}
int query(node *root)
{
int sum=0;
for(int i=0;i<si;i++)
{
if(root->chi[i]!=NULL)
{
node *t=root->chi[i];
sum+=t->n;
if(t->n>=2)
sum+=query(t);
}
}
return sum;
}
void rease(node *root)
{
for(int i=0;i<si;i++)
{
if(root->chi[i]!=NULL)
rease(root->chi[i]);
}
delete root;
}
int main()
{
int T;scanf("%d",&T);
while(T--)
{
int n=0;
scanf("%d",&n);
node *root=(node *)malloc(sizeof(node));
root->init();
for(int i=0;i<n;i++)
{
scanf("%s",name);
Insert(name, root);
}
printf("%d\n",query(root));
}
}
//数组型,DFS查询
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn=1000000;
struct tire{
int wd[27];
int cnt;
void init()
{
cnt=0;
memset(wd,0,sizeof(wd));
}
};
tire tree[maxn];
int tot=1,n,m;
void Insert(char* s,int root)
{
for(int i=0;s[i];i++)
{
int k=s[i]-'a';
if(!tree[root].wd[k])
{
tree[tot].init();
tree[root].wd[k]=tot++;
}
root=tree[root].wd[k];
tree[root].cnt++;
}
}
int sum=0;
int query (int root)
{
int sum=0;
for(int i=0;i<=25;i++)
{
if(tree[root].wd[i])
{
int ro=tree[root].wd[i];
sum+=tree[ro].cnt;
if(tree[ro].cnt>1)
sum+=query(ro);
}
}
return sum;
}
char name[1000000+5];
int main ()
{
int T;scanf("%d",&T);
while(T--)
{
int root=0;
tot=1;
tree[root].init();
int n;scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%s",name);
Insert(name, root);
}
printf("%d\n",query(root));
}
return 0;
}
//数组型,名字查询,名字在string中保存,虽然可以节约空间,但是由于string只能用cin输入,所以时间慢。
#include<cstdio>
#include<cstring>
#include<string>
#include<iostream>
using namespace std;
const int maxn=1000000;
struct tire
{
int wd[27];
int cnt;
void init()
{
cnt=0;
memset(wd,0,sizeof(wd));
}
};
tire tree[maxn];
int tot=1,n,m;
void Insert(string s,int root)
{
for(int i=0;s[i];i++)
{
int k=s[i]-'a';
if(!tree[root].wd[k])
{
tree[tot].init();
tree[root].wd[k]=tot++;
}
root=tree[root].wd[k];
tree[root].cnt++;
}
}
int query (string s,int root)
{
for(int i=0;s[i];i++)
{
int k=s[i]-'a';
root=tree[root].wd[k];
if(tree[root].cnt==1)
return i+1;
}
return tree[root].cnt+1;
}
string name[1000+5];
int main ()
{
int T;scanf("%d",&T);
while(T--)
{
int root=0;
tot=1;
tree[root].init();
int n;scanf("%d",&n);
for(int i=1;i<=n;i++)
{
cin>>name[i];
Insert( name[i], root);
}
int ans=0;
for(int i=1;i<=n;i++)
{
ans+=query(name[i], root);
}
printf("%d\n",ans);
}
return 0;
}
Trie/最短的名字的更多相关文章
- csuoj 1115: 最短的名字
http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1115 1115: 最短的名字 Time Limit: 5 Sec Memory Limit: 6 ...
- CSU 1115 最短的名字
传送门 Time Limit: 5000MS Memory Limit: 65536KB 64bit IO Format: %lld & %llu Description 在一个奇怪的 ...
- E - 最短的名字
Description 在一个奇怪的村子中,很多人的名字都很长,比如aaaaa, bbb and abababab. 名字这么长,叫全名显然起来很不方便.所以村民之间一般只叫名字的前缀.比如叫'aaa ...
- CSU - 1115 最短的名字(字典树模板题)
Description 在一个奇怪的村子中,很多人的名字都很长,比如aaaaa, bbb and abababab. 名字这么长,叫全名显然起来很不方便.所以村民之间一般只叫名字的前缀.比如叫'aaa ...
- 2012年湖南省程序设计竞赛E题 最短的名字
题目链接:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1115 解题报告:输入n个字符串,让你求出可以用来区别这些字符串的最少的前缀总共有多少个字 ...
- 6天通吃树结构—— 第五天 Trie树
原文:6天通吃树结构-- 第五天 Trie树 很有段时间没写此系列了,今天我们来说Trie树,Trie树的名字有很多,比如字典树,前缀树等等. 一:概念 下面我们有and,as,at,cn,com这些 ...
- 内存空间有限情况下的词频统计 Trie树 前缀树
数据结构与算法专题--第十二题 Trie树 https://mp.weixin.qq.com/s/nndr2AcECuUatXrxd3MgCg
- 湖南省第八届大学生计算机程序设计竞赛(A,B,C,E,F,I,J)
A 三家人 Description 有三户人家共拥有一座花园,每户人家的太太均需帮忙整理花园.A 太太工作了5 天,B 太太则工作了4 天,才将花园整理完毕.C 太太因为正身怀六甲无法加入她们的行列, ...
- LINQ之路 7:子查询、创建策略和数据转换
在前面的系列中,我们已经讨论了LINQ简单查询的大部分特性,了解了LINQ的支持计术和语法形式.至此,我们应该可以创建出大部分相对简单的LINQ查询.在本篇中,除了对前面的知识做个简单的总结,还会介绍 ...
随机推荐
- onSaveInstanceState()和onRestoreInstanceState()方法
Activity的 onSaveInstanceState() 和 onRestoreInstanceState()并不是生命周期方法,它们不同于 onCreate().onPause()等生命周期方 ...
- Struts入门学习(三)---自定义类型转换器
类型转换器是将浏览器传递的参数进行转换为了与服务器端的参数匹配,先举个例子如果我们想往服务器传递日期类型的参数时我们要怎么让浏览器传过去的让服务器明白 我们新建一个类 ConverterTest.ja ...
- UI篇—懒加载
1.懒加载基本 懒加载——也称为延迟加载,即在需要的时候才加载(效率低,占用内存小).所谓懒加载,写的是其get方法. 注意:如果是懒加载的话则一定要注意先判断是否已经有了,如果没有那么再去进行实例化 ...
- C -小晴天老师系列——竖式乘法
C - 小晴天老师系列——竖式乘法 Time Limit: 4000/2000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others ...
- ural 1352. Mersenne Primes
1352. Mersenne Primes Time limit: 1.0 secondMemory limit: 64 MB Definition. If the number 2N−1 is pr ...
- 杭电20题 Human Gene Functions
Problem Description It is well known that a human gene can be considered as a sequence, consisting o ...
- jq的siblings对a标签不起效
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- robotframework代码定位感悟
robotframework代码定位感悟: 在做自动化的时候,有时候还是要考虑真实的操作,考虑人为反应,网络,页面渲染,服务器处理请求的过程及数据是否真实到达响应的位置, 不能一味的按照程序的速度 去 ...
- iOS中的触摸事件,手势识别,摇晃事件等
在iOS中,事件可以划分为以下几类: 1.触摸事件:通过触摸,手势进行触发(手指点击.缩放等) 2.运动事件:通过加速器触发(例如手机晃动) 3.远程控制事件:通过其他远程设备触发(例如耳机控制按钮) ...
- easyui-datagrid加载时的效率低下,解决方案
在360浏览器中 使用easyui datagrid 加载数据是效率低下,有时候会出现卡机. 可以修改easyui源码进行解决.