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树)的更多相关文章

  1. UVA 11488 Hyper Prefix Sets (Trie)

    http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...

  2. UVA 11488 Hyper Prefix Sets (字典树)

    https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  3. uva 11488 - Hyper Prefix Sets(字典树)

    H Hyper Prefix Sets Prefix goodness of a set string is length of longest common prefix*number of str ...

  4. UVA 11488 Hyper Prefix Sets (字典树)

    题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...

  5. uva 11488 Hyper Prefix Sets(狂水)

    题意: 获得集合中最长前缀长度*有该前缀个数的最大值 Prefix goodness of a set string is length of longest common prefix*number ...

  6. UVa 11488 - Hyper Prefix Sets

    找 前缀长度*符合该前缀的字符串数 的最大值 顺便练了一下字典树的模板 #include <iostream> #include <cstdio> #include <c ...

  7. 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 ...

  8. Hyper Prefix Sets

    uva11488:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&am ...

  9. UVa11488-Hyper Prefix Sets(trie树)

    H Hyper Prefix Sets Prefix goodness of a set string is length of longest common prefix*number of str ...

随机推荐

  1. [luoguP1198][JSOI2008] 最大数(线段树 || 单调栈)

    题目传送门 1.线段树 线段树可以搞. 不过慢的要死1300+ms #include <cstdio> #include <iostream> using namespace ...

  2. Help him--hdu5059(模拟 大坑)

    http://acm.hdu.edu.cn/showproblem.php?pid=5059 直接说可能出现的情况 #include <iostream> #include <cst ...

  3. 109.Convert sorted list to BST

    /* * 109.Convert sorted list to BST * 2016.12.24 by Mingyang * 这里的问题是对于一个链表我们是不能常量时间访问它的中间元素的. * 这时候 ...

  4. WinCE5.0如何安装.NET3.5

    首先去微软官网下载.NET Compact Framework 3.5 Redistributable 点击下载 下载页面 一共有两种安装方式,我们先介绍常规的安装方式 1.设备连接到电脑,然后双击下 ...

  5. How do you check if a variable is an array in JavaScript? [duplicate]

    https://stackoverflow.com/questions/767486/how-do-you-check-if-a-variable-is-an-array-in-javascript ...

  6. VM Workstation的Unity Mode有什么用

    正常情况下,如果我启动了一个VM Workstaion的虚拟机,比如是一个Linux系统,并且没运行任何软件,进入Unity mode之后,我真实系统的左下角会有一个虚拟机的图标 点击这个图标可以打开 ...

  7. 【 D3.js 进阶系列 — 1.0 】 CSV 表格文件的读取

    在入门系列的教程中.我们经常使用 d3.json() 函数来读取 json 格式的文件.json 格式非常强大.但对于普通用户可能不太适合,普通用户更喜欢的是用 Microsoft Excel 或 O ...

  8. Python第五讲

    一.冒泡算法 1.将两个变量的值互换 a1 = 123 a2 = 456 #要想将a1与a2的值进行位置互换需要借助一个中间变量(temp) temp = a1#将a1的值赋值给temp(temp=1 ...

  9. powerdesign导出SQL时自己主动生成凝视

    1.使用脚本的方式 在里面执行 Option Explicit ValidationMode   = True InteractiveMode   =   im_Batch Dim   mdl   ' ...

  10. 基于docker容器搭建fastdfs分布式文件系统

    本次环境的搭建参考了 https://blog.csdn.net/qq_43455410/article/details/84797814, 感谢博主. 主要流程如下: 1. 下载fastdfs镜像 ...