UVA - 11488 Hyper Prefix Sets(trie树)
1、给n个只含0、1的串,求出这些串中前缀的最大和。
例1:
0000
0001
10101
010
结果:6(第1、2串共有000,3+3=6)
例2:
01010010101010101010
11010010101010101010
结果:20(第1串的长度为20)
2、用trie树(字典树)来做,插入的时候统计前缀出现次数,并更新最大前缀和(前缀出现次数*前缀长度)。
3、
#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std; const int MAX=;
int ans;//最大前缀和 struct Trie
{
Trie *next[MAX];
int num[MAX];//此前缀出现次数
}; void createTrie(char *str,Trie *root)
{
int temp;
int len = strlen(str);
Trie *p = root, *q;
for(int i=; i<len; ++i)
{
int id = str[i]-'';
if(p->next[id] == NULL)
{
q = new Trie;
for(int j=; j<MAX; ++j)
{
q->next[j] = NULL;
q->num[j]=;
}
p->next[id] = q; }
++p->num[id];//前缀出现次数+1
temp=p->num[id]*(i+);
if(temp>ans)
ans=temp;//更新最大前缀和
p = p->next[id];
}
} int main()
{
char str[];
int T,n,i; scanf("%d",&T);
while(T--)
{
Trie *root=new Trie;
for(i=; i<MAX; i++)
{
root->next[i]=NULL;
root->num[i]=;
} ans=;//最大前缀和初始化
scanf("%d",&n);
for(i=; i<n; ++i)
{
scanf("%s",str);
createTrie(str,root);//插入到字典树
}
printf("%d\n",ans);
}
return ;
}
UVA - 11488 Hyper Prefix Sets(trie树)的更多相关文章
- UVA 11488 Hyper Prefix Sets (Trie)
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...
- UVA 11488 Hyper Prefix Sets (字典树)
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- 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 11488 Hyper Prefix Sets (字典树)
题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...
- uva 11488 Hyper Prefix Sets(狂水)
题意: 获得集合中最长前缀长度*有该前缀个数的最大值 Prefix goodness of a set string is length of longest common prefix*number ...
- UVa 11488 - Hyper Prefix Sets
找 前缀长度*符合该前缀的字符串数 的最大值 顺便练了一下字典树的模板 #include <iostream> #include <cstdio> #include <c ...
- HDU 11488 Hyper Prefix Sets (字符串-Trie树)
H Hyper Prefix Sets Prefix goodness of a set string is length of longest common prefix*number of str ...
- Hyper Prefix Sets
uva11488:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&am ...
- UVa11488-Hyper Prefix Sets(trie树)
H Hyper Prefix Sets Prefix goodness of a set string is length of longest common prefix*number of str ...
随机推荐
- Idea中配置svn时报 Can't use Subversion command line client: svn.Errors found while svn working copies detection.
https://www.cnblogs.com/wqh17/p/6881483.html
- bzoj 1500 [NOI 2005] 维修数列
题目大意不多说了 貌似每个苦逼的acmer都要做一下这个splay树的模版题目吧 还是有很多操作的,估计够以后当模版了.... #include <cstdio> #include < ...
- SQL SERVER 2012 第四章 连接 JOIN语句的早期语法结构 & 联合UNION
1/内部连接的早期语法结构 INNER JOIN SELECT * FROM Person.Person JOIN HumanResources.Employee ON Person.Person.I ...
- tomcat启动提示java.lang.UnsatisfiedLinkError: D:\soft\devTool\apache-tomcat-7.0.57\bin\tcnative-1.dll: C
https://blog.csdn.net/a274360781/article/details/52411984
- 安装mysql时出现应用程序无法正常启动(0xc000007b)、初始化失败以及密码忘记怎样重置?
https://blog.csdn.net/zztingfeng/article/details/80155624
- 《springMVC》学习笔记
1.SpringMVC框架 1.1 MVC在b/s系统下的应用 用户发送request请求到Controller Controller请求模型(Model)进行处理 Model将处理结果返回到Cont ...
- 【Mongodb教程 第十二课 】PHP mongodb 的使用
mongodb 不用过多的介绍了,NOSQL的一种,是一个面向文档的数据库,以其方便灵活的数据结构,对于开发者来说是比较友好的,同时查询的速度也是比较快的,现在好多网站 开始使用mongodb ,具体 ...
- centos7 rpm 安装MySQL5.6
系统centos7,安装MySQL,出错,百度了一大堆,几乎都是通过yum安装的MySQL,我想说的是,通过yum自动安装的话系统自己会帮你做很多事情,相关联的包也会下下来,相当于替你省去了很多事情, ...
- Cocos从入门到精通--《创建第一个项目:HelloWorld》
上节课我们解说了cocos2-x v3.7版本号的下载安装,也展示了使用CocosStudio编译不同平台运行程序的方法,大家是不是对新版本号的Cocos引擎充满期待?今天我们就创建一个project ...
- 错误 1 无法将程序集“NBear.Data.dll”复制到文件“D:\newbpm\bpm\SureBpm\Bin\NBear.Data.dll”。无法将“D:\newbpm\bpm\SureSoft.WebServiceBaseLib\bin\Debug\NBear.Data.dll”添加到网站。 无法添加文件“Bin\NBear.Data.dll”。 拒绝访问。 D:\..
错误 1 无法将程序集“NBear.Data.dll”复制到文件“D:\newbpm\bpm\SureBpm\Bin\NBear.Data.dll”.无法将“D:\newbpm\bpm\SureSof ...