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 ...
随机推荐
- GO 语言周报【七月第 1 期】
TIOBE 七月排名 Go 进入前十 TIOBE 七月头条:Go 语言达到历史最高并进入前十.对于 Go 语言来说,这是一个里程碑时刻,我们可以更大胆地想象,它下一步的发展会达到怎样的高度.Go 是否 ...
- BZOJ 2763: [JLOI2011]飞行路线 【SPFA】
Alice和Bob现在要乘飞机旅行,他们选择了一家相对便宜的航空公司.该航空公司一共在n个城市设有业务,设这些城市分别标记为0到n-1,一共有m种航线,每种航线连接两个城市,并且航线有一定的价格.Al ...
- 【BZOJ4475】子集选取(计数)
题意: 思路: #include<cstdio> #include<cstdlib> #include<iostream> #include<algorith ...
- LightOJ1094 - Farthest Nodes in a Tree(树的直径)
http://lightoj.com/volume_showproblem.php?problem=1094 Given a tree (a connected graph with no cycle ...
- Java连接MySQL报错:CommunicationsException: Communications link failure
现象: 报错:Exception in thread "main" com.mysql.cj.jdbc.exceptions.CommunicationsException: Co ...
- react 使用 eslint 的三种代码检查方案总结,多了解点--让代码更完美....
1.介绍 ESLint 是一个可扩展,每条规则独立,被设计为完全可配置的lint工具. 可以用来检测代码,避免低级错误 可以用来规范代码的开发风格,统一代码习惯. 2.为什么使用 ESLint ? 统 ...
- [Bash] Create nested folder in Bash
We can create a single folder by doing: mkdir onefolder If we want to create nested folder we need t ...
- http://vdceye.com/ 全新页面上线
vdceye manager home page
- 微信小程序之 Tabbar(底部选项卡)
1.项目目录 2.在app.json里填写:tab个数范围2-5个 app.json { "pages": [ "pages/index/index", &qu ...
- SQL - 创建一个学生表,要求有主键约束和非空约束
CREATE TABLE [dbo].[Student] ( [ID] [int] NOT NULL, [Name] [nchar](10) NOT NULL, [Age] [int] NOT NUL ...