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 ...
随机推荐
- 在java中获取Map集合中的key和value值
- 一个jboss启动shell脚本
脚本1: #!/bin/sh # paulo@evencom.com.br #JBOSS_HOME JBOSS_HOME="/opt/app/jboss-eap-6.3" JAVA ...
- mysql与时间有关的查询
date(str)函数可以返回str中形如"1997-05-26"格式的日期,str要是合法的日期的表达式,如2008-08-08 22:20:46 时间是可以比较大小的,例如: ...
- 【记录】新建Cordova项目出现ios-deploy找不到的问题
按老流程 Cordova create helloApp Cordova platform add ios 之前一般这种操作之后就能有执行的iOS目录了,像这样 然后 Cordova build ...
- SpringBoot自动配置的源码解析
首先,写源码分析真的很花时间,所以希望大家转的时候也请注明一下,Thanks♪(・ω・)ノ SpringBoot最大的好处就是对于很多框架都默认的配置,让我们开发的时候不必为了大一堆的配置文件头疼,关 ...
- 【HDOJ6318】Swaps and Inversions(树状数组)
题意: 给定一串数组,其中含有一个逆序对则需要花费x,交换相邻两个数需要花费y,输出最小花费. n<=1e5,-1e9<=a[i]<=1e9 思路: #include<cstd ...
- 10.1——pair,map,set,multimap,multiset
map和set只允许相同的键出现一次,而multimap和multiset则允许出现多次. 1. 引言——pair类型: pair需要添加头文件utility头文件 make_pair<v1,v ...
- msp430入门编程42
msp430中C语言的软件工程--事件触发程序结构
- msp430入门编程40
msp430中C语言的软件工程--前后台程序结构
- Xcode warning: code will never be executed.
在xcode编译的时候,提示了code will never be executed这个警告.百度了一下,大体的意思是,该代码永远不会执行的意思. 比如: - (void)setMyStyle:(Ad ...