zoukankan      html  css  js  c++  java
  • [bzoj1174]Toponyms

    构建一棵trie树,然后每一个点的次数*深度取max即可
    (然而这道题并没有这么简单)
    首先字符串长度为500000,字符类型52种,需要用邻接表存储(用map就别想了)
    然后字符串的读入十分麻烦,需要用getchar来读入,还要判掉多余回车
    最后还有点卡常,要注意常数

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 #define N 5000005
     4 struct ji{
     5     int nex,to,val;
     6 }edge[N];
     7 int V,E,n,sum[N],head[N];
     8 long long ans;
     9 void add(int x,int y,int z){
    10     edge[E].nex=head[x];
    11     edge[E].to=y;
    12     edge[E].val=z;
    13     head[x]=E++;
    14 }
    15 void insert(){
    16     int c=getchar();
    17     while (c=='
    ')c=getchar();
    18     for(int i=1,k=1;c!='
    ';i++,c=getchar()){
    19         int t=k;
    20         for(int j=head[k];j!=-1;j=edge[j].nex)
    21             if (edge[j].val==c)k=edge[j].to;
    22         if (t==k)add(t,k=++V,c);
    23         sum[k]++;
    24         ans=max(ans,1LL*sum[k]*i);
    25     }
    26 }
    27 int main(){
    28     scanf("%d",&n);
    29     V=1;
    30     memset(head,-1,sizeof(head));
    31     for(int i=1;i<=n;i++)insert();
    32     printf("%lld",ans);
    33 }
    View Code
  • 相关阅读:
    LeetCode
    LeetCode
    LeetCode OJ
    LeetCode OJ
    LeetCode OJ
    关于if和else和switch case的用法和程序编码操作过程
    关于java的特点
    关于JAVA的数据类型
    关于java的学习
    力扣482. 密钥字符串 S python--每天一题
  • 原文地址:https://www.cnblogs.com/PYWBKTDA/p/11847145.html
Copyright © 2011-2022 走看看