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查询.在本篇中,除了对前面的知识做个简单的总结,还会介绍 ...
随机推荐
- hdu_5881_Tea(xjb猜)
题目链接:hdu_5881_Tea 题意: 有一壶水, 体积在 L 和 R 之间, 有两个杯子, 你要把水倒到两个杯子里面, 使得杯子水体积几乎相同(体积的差值小于等于1), 并且使得壶里剩下水体积不 ...
- hdu_2717_Catch That Cow_bfs
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=2717 题解:一维简单BFS,详细看代码,0ms. #include<cstdio> #in ...
- Apache Zeppelin
介绍 用于做数据分析和可视化 一.二进制安装 1)下载二进制包 wget http://mirrors.tuna.tsinghua.edu.cn/apache/incubator/zeppelin/0 ...
- 文件断点续传原理与实现—— ESFramework 通信框架4.0 进阶(12)
在ESFramework通信框架 4.0 快速上手(13) -- 文件传送,如此简单一文的详细介绍和ESFramework通信框架 4.0 快速上手(14) -- 聊天系统Demo,增加文件传送功能( ...
- phpstudy 相关配置
在/etc/my.cnf中 添加 expire_logs_days=5 phpstudy add list del
- java虚拟机存储区
方法区和堆区是数据共享区. 栈区:数据不共享.方法参数.局部变量.参与运算的中间结果.返回值等等都在栈区中. 堆区:数据共享.存放对象. 方法区存放类型信息,类型信息包括:字段信息.方法信息.该类型的 ...
- poj2140(奇因数的个数)
#include<stdio.h>int main(){ int n,s=0; scanf("%d",&n); for(int i=1;i<=n;i++) ...
- 1077. [NOIP2010冲刺六] 数列游戏
[题目描述] 小M很喜欢找点游戏自娱自乐.有一天,她在纸上写了一串数字:1,1,2,5,4.接着她擦掉了一个1,结果发现剩下1,2,4都在自己所在的位置上,即1在第1位,2在第2位,4在第4位.她希望 ...
- C#第七天
一 1.继承:我们可能会在一些类中,写一些重要的成员,将这些重复的成员单独的封装到一个类中,作为这些类的父类. Student Teacher Driver ...
- java启动子进程以及进程通信
1.利用进程的管道通信传输流 2.子进程没有控制台,正常测试的时候也是没办法看到子进程的输出的,需要传到主线程 3.测试主进程传参给子进程再传回来 4.父进程启动子进程只要执行runtime.exec ...