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)内 ...
随机推荐
- 文件类似性推断 -- SimHash
近期调研了一下simhash算法,它主要用在谷歌网页去重中.网上有非常多原理性的介绍. 既然能够用来推断文件的相似性,就想知道效果怎么样.simhash的准确度是否依赖于分词算法?是否和simhash ...
- oracle 时间戳TIMESTAMP
//数据库 UPDATETIMESTAMP TIMESTAMP(6) //dto /** 更新时间戳 **/ private String updatetimestamp; //dao //插入操作 ...
- 英语影视台词---八、the shawshank redemption
英语影视台词---八.the shawshank redemption 一.总结 一句话总结:肖申克的救赎 1.It's funny. On the outside, I was an honest ...
- linux 终端提示符
默认的当路径一长就难看得出奇. 我的设置: export PS1="|\W$>\[\e[0m\]" 最后效果就是|目录名$> 参考:https://www.cnblog ...
- linux 下的文件搜索、可执行文件搜索
1. whereis 与 which 速度快,只是模糊查询,例如查询 $ whereis mysql,则会将mysql, mysql.ini, mysql*所在的目录都找出来: whereis 查看的 ...
- js中cookie的使用 以及缺点
什么是Cookie Cookie意为“甜饼”,是由W3C组织提出,最早由Netscape社区发展的一种机制.目前Cookie已经成为标准,所有的主流浏览器如IE.Netscape.Firefox. ...
- 简易的CSS下拉菜单 - 1
<!DOCTYPE html> <html> <head> <style> body{ margin:0; } .dropmenu{ backgroun ...
- django steps EASY WAY
django 2.0 python 3.6 =========django steps EASY WAY=========== reference: https://djangoforbeginner ...
- POJ 1064 Cable master 【二分答案】
和杭电那一题一样,只不过G++交不能通过,C++能过 wa了好多好多好多次----------------------------------------- #include<iostream& ...
- rsyslog 存储到 mysql
输出Host1/2的系统日志, 记录到mysql服务器数据库中, 并发布loganalyzer 结构关系如下图: 思路: 通过远程连接mysql, 使得rsyslog的log记录能够写入到mysql中 ...