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查询.在本篇中,除了对前面的知识做个简单的总结,还会介绍 ...
随机推荐
- 朱丽叶—Cuda+OSG
#include <cuda_runtime.h> #include <osg/Image> ; typedef struct cuComplex { float r; flo ...
- C#正则分组实例
static void Main(string[] args) { string str = "大家家家家家家家明天天天天天天天天玩得得得得得得得开心"; Regex reg = ...
- LeetCode OJ 59. Spiral Matrix II
Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...
- 基于多线程多用户的FTP服务器与客户端功能实现
项目介绍: 用户加密认证 允许同时多用户登录 每个用户有自己的家目录 ,且只能访问自己的家目录 对用户进行磁盘配额,每个用户的可用空间不同 允许用户在ftp server上随意切换目录 允许用户查看当 ...
- SpringMVC利用Hibernate validator做字段验证
1.添加Hiberbate validator相关的jar包 2.字需要验证的formbean 上添加验证的注解,内置注解有: dBean Validation 中内置的 constraint @Nu ...
- B - ACboy needs your help(动态规划,分组背包)
B - ACboy needs your help Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & ...
- C# 调用C++ DLL 的类型转换
//C#调用C++的DLL搜集整理的所有数据类型转换方式,可能会有重复或者多种方案,自己多测试 //c++:HANDLE(void *) ---- c#:System.IntPtr //c++:Byt ...
- 对SNS网站现状和未来的一些想法——以我对人人网的体验为例
现在对人人网越来越没有兴趣了,上面的照片.状态也越来越少了,反而是朋友圈里大家比较活跃. 我觉得在网上发内容的,至少是希望得到大家关注的,可是为什么人人越来越被大家嫌弃了呢? 人人上的消息越来越被淹没 ...
- Python 正则表达式学习笔记
本文介绍了Python对于正则表达式的支持,包括正则表达式基础以及Python正则表达式标准库的完整介绍及使用示例.本文的内容不包括如何编写高效的正则表达式.如何优化正则表达式,这些主题请查看其他教程 ...
- Java 微信登录授权后获取微信用户信息昵称乱码问题解决
String getUserInfoUrl = "https://api.weixin.qq.com/sns/userinfo?access_token="+access_toke ...