BZOJ 1174: [Balkan2007]Toponyms
Time Limit: 10 Sec Memory Limit: 128 MB
Submit: 735 Solved: 102
[Submit][Status][Discuss]
Description
给你一个字符集合,你从其中找出一些字符串出来. 希望你找出来的这些字符串的最长公共前缀*字符串的总个数最大化.
Input
第一行给出数字N.N在[2,1000000] 下面N行描述这些字符串,长度不超过20000 。保证输入文件不超过10MB
Output
a single line with an integer representing the maximal level of complexity Lc(T).
Sample Input
Jora de Sus
Orhei
Jora de Mijloc
Joreni
Jora de Jos
Japca
Orheiul Vechi
Sample Output
HINT
这题有毒啊,,
思路和算法没什么好说的,
就是建一棵字典树,统计好深度和个数,然后乘起来,取最大值
但是!!!
本蒟蒻一开始写的无限RE
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int n,sz=,ans;
string ch;
int a[][],f[];
void insert()
{
int l=ch.length(),now=;
for(int i=;i<l;i++)
{
int t;
if(ch[i]==' ')t=;
else if(ch[i]<='Z')t=ch[i]-'A';
else t=ch[i]-'a'+;
if(a[now][t])now=a[now][t];
else now=a[now][t]=++sz;
f[now]++;
ans=max(ans,f[now]*(i+));
}
}
int main()
{
scanf("%d",&n);
for(int i=;i<=n;i++)
{
getline(cin,ch);
insert();
}
printf("%d",ans);
return ;
}
RE
后来听别人说这题卡内存,
于是我换成了前向星储存,
然后,
无限TLE,
加了各种常数优化还是无限TLE
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
const int MAXN=;
inline void read(int &n)
{
char c='+';bool flag=;n=;
while(c<''||c>''){c=getchar();if(c=='-')flag=;}
while(c>=''&&c<='')n=n*+c-,c=getchar();
}
struct E
{
int u,v,nxt;
}edge[MAXN];
int head[MAXN];
int num=;
struct node
{
int bh;
int num;
int val;
node(){num=;val=;}
}trie[MAXN];
int tot=;
char a[MAXN];
bool vis[MAXN];
inline void add_edge(int x,int y)
{
edge[num].u=x;
edge[num].v=y;
edge[num].nxt=head[x];
head[x]=num++;
}
long long int ans=;
inline void insert(char *a)
{
int l=strlen(a);int now=;
for(register int i=;i<l;i++)
{
bool flag=;
int debug=a[i];
for(register int j=head[now];j!=-;j=edge[j].nxt)
{
if(trie[edge[j].v].val==a[i])
trie[edge[j].v].num++,
flag=,
now=edge[j].v;
}
if(flag==)
{
trie[++tot].bh=tot;
trie[tot].num=;
trie[tot].val=a[i];
add_edge(now,tot);
now=tot;
}
ans=max(ans,(long long )(i+)*trie[now].num);
}
}
int main()
{
memset(head,-,sizeof(head));
int n;
read(n);
for(int i=;i<=n;i++)
{
memset(a,,sizeof(a));int hh=;
char ch=getchar();bool flag2=;
while(ch=='\n') ch=getchar();
while(ch!='\n')
a[hh++]=ch,ch=getchar();
insert(a);
}
printf("%lld",ans);
return ;
}
TLE
然后只好参考别的大神的代码.......
编译速度就秒杀我的代码。。。
#include <cstdio>
#include <cstring>
#include <algorithm>
#define N 5000010
using namespace std;
int tot = , head[N] , to[N] , next[N] , cnt , si[N];
char val[N];
void add(int x , int y , char c)
{
to[++cnt] = y , val[cnt] = c , next[cnt] = head[x] , head[x] = cnt;
}
int main()
{
int n , i , j , k , t , p;
char ch;
long long ans = ;
scanf("%d" , &n);
for(i = ; i <= n ; i ++ )
{
ch = getchar();
while(ch == '\n') ch = getchar();
for(j = t = ; ch != '\n' ; j ++ , ch = getchar())
{
for(p = , k = head[t] ; k ; k = next[k])
{
if(val[k] == ch)
{
p = to[k];
break;
}
}
if(!p) add(t , p = ++tot , ch);
t = p , si[t] ++ , ans = max(ans , (long long)j * si[t]);
}
}
printf("%lld\n" , ans);
return ;
}
BZOJ 1174: [Balkan2007]Toponyms的更多相关文章
- BZOJ 1174 [Balkan2007]Toponyms(Trie)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1174 [题目大意] 选出一些字符串,使得字符串的最长公共前缀*字符串的总个数最大化 [ ...
- BZOJ1174: [Balkan2007]Toponyms
1174: [Balkan2007]Toponyms Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 117 Solved: 16[Submit][S ...
- BZOJ 1176: [Balkan2007]Mokia
1176: [Balkan2007]Mokia Time Limit: 30 Sec Memory Limit: 162 MBSubmit: 2012 Solved: 896[Submit][St ...
- BZOJ 1176: [Balkan2007]Mokia( CDQ分治 + 树状数组 )
考虑cdq分治, 对于[l, r)递归[l, m), [m, r); 然后计算[l, m)的操作对[m, r)中询问的影响就可以了. 具体就是差分答案+排序+离散化然后树状数组维护.操作数为M的话时间 ...
- BZOJ 1176([Balkan2007]Mokia-CDQ分治-分治询问)
1176: [Balkan2007]Mokia Time Limit: 30 Sec Memory Limit: 162 MB Submit: 185 Solved: 94 [ Submit] ...
- 【BZOJ1174】: [Balkan2007]Toponyms
→原题← ↑这样子我就不复制题面啦~ 就是一题很裸的字典树而已,不过空间卡的很死,直接开个数组 tr[N][52] 什么之类的一定会RE的(惨痛的教训) 当字典树空间不够而时间限制又比较宽松时,我们可 ...
- BZOJ 1176[Balkan2007]Mokia(CDQ分治)
1176: [Balkan2007]Mokia Time Limit: 30 Sec Memory Limit: 162 MBSubmit: 3381 Solved: 1520[Submit][S ...
- BZOJ 1176 [Balkan2007]Mokia ——CDQ分治
[题目分析] 同BZOJ2683,只需要提前处理s对结果的影响即可. CDQ的思路还是很清晰的. 排序解决一维, 分治时间, 树状数组解决一维. 复杂度是两个log [代码] #include < ...
- BZOJ 1176: [Balkan2007]Mokia [CDQ分治]
题意: 有一个n * n的棋盘,每个格子内有一个数,初始的时候全部为0.现在要求维护两种操作: 1)Add:将格子(x, y)内的数加上A. 2)Query:询问矩阵(x0, y0, x1, y1)内 ...
随机推荐
- 什么是域名的TTL值? ——一条域名解析记录在DNS缓存服务器中的存留时间
什么是域名的TTL值? 转自:http://hizip.net/index.php/archives/20/TTL(Time-To-Live),就是一条域名解析记录在DNS服务器中的存留时间.当各地的 ...
- 安装Oracle RAC 11g
1.Oracle Enterprise Linux 和 iSCSI 上构建 Oracle RAC 11g 集群 2.Oracle RAC 的所有共享磁盘存储将基于 iSCSI,iSCSI 使用在第三个 ...
- js捕获页面回车事件
1.javascript版 document.onkeyup = function (e) { if (window.event)//如果window.event对象存在,就以此事件对象为准 e = ...
- HOOK劫持自己
#include <stdio.h> #include <stdlib.h> #include <Windows.h> #include "detours ...
- xBIM 基础03 基本模型操作
系列目录 [已更新最新开发文章,点击查看详细] 本篇将使用基本的代码示例来表示如何使用xBIM.我们将介绍持久存储的四个基本功能,即 CRUD(创建,检索,更新和删除).以下示例通常适用于IF ...
- BZOJ 2190 欧拉函数
思路: 递推出来欧拉函数 搞个前缀和 sum[n-1]*2+3就是答案 假设仪仗队是从零开始的 视线能看见的地方就是gcd(x,y)=1的地方 倒过来一样 刨掉(1,1) 就是ans*2+1 再加一下 ...
- POJ 3299 模拟
水题,但是WA了一屏--- swap的时候忘了把读入的数字也swap了---------..[尴尬] // by SiriusRen #include <cmath> #include & ...
- 51nod-1134 最长递增子序列,用线段树将N^2的dp降到NlogN
题目链接 给出长度为N的数组,找出这个数组的最长递增子序列.(递增子序列是指,子序列的元素是递增的) 例如:5 1 6 8 2 4 5 10,最长递增子序列是1 2 4 5 10. Input 第1行 ...
- PKI和加密,散列算法
Day 11-PKI和加密,散列算法 PKI(Public Key Infrastructure公钥基础设施) 1 PKI(Public Key Infrastructure公钥基础设施)回顾学习 - ...
- java 实现多线程 3种方式
java实现多线程可以有以下三种方式: (1)继承Thread 类,重写其run()方法: (2)实现Runnable接口,实现其run() 方法: (3) 实现Callable 接口,重写call( ...