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 ...
随机推荐
- dataTables中固定表头
dataTables中固定表头 加入 bAutowidth:false, <style> #dayReveiveMoney_payment_list_table_wrapper .dat ...
- 【计算几何】FZU Problem 2270 Two Triangles
http://acm.fzu.edu.cn/problem.php?pid=2270 [题意] 给定6到10个点,从中选出6个不同的点组成两个三角形,使其中一个三角形可以通过另一个三角形平移和旋转得到 ...
- 新版VS-code如何自动换行?
文件 -> 首选项 -> 设置 -> 编辑器 找到 // 控制折行方式.可以选择: - "off" (禁用折行), - "on" (视区折行 ...
- CERC 2014 (动态树+主席树)
CERC 2014 Pork barrel Problem : n个点m条边有边权的无向图,有q个询问,每次询问权值在[L,R]内的边组成的最小生成树的权值和,强制在线. n <= 1000, ...
- Android视图组成View
视图组成View 创建时间: 2013-9-13 10:51 更新时间: 2013-9-13 11:04
- 理解 mysql行锁和表锁
在调用存储过程中,就会涉及到表锁,行锁这一概念:所谓区别:有索引的时候就是行锁,没有索引的时候就是表索. innodb 的行锁是在有索引的情况下,没有索引的表是锁定全表的. 表锁演示(无索引) Ses ...
- Milking Time---poj3616
Description Bessie is such a hard-working cow. In fact, she is so focused on maximizing her producti ...
- P1003 铺地毯(noip 2011)
洛谷——P1003 铺地毯 题目描述 为了准备一个独特的颁奖典礼,组织者在会场的一片矩形区域(可看做是平面直角坐标系的第一象限)铺上一些矩形地毯.一共有 n 张地毯,编号从 1 到n .现在将这些地毯 ...
- Spring MVC页面重定向实例
以下内容引用自http://wiki.jikexueyuan.com/project/spring/mvc-framework/spring-page-redirection-example.html ...
- 表皮囊肿?wtf
https://baike.baidu.com/item/%E8%A1%A8%E7%9A%AE%E5%9B%8A%E8%82%BF/7852024?fr=aladdin