Balkan2007]Toponyms[链式前向星建字典树+getchar()读入优化]


思路容易想,卡空间和时间就吐了
用链式前向星压缩空间,用getchar()一位一位读加快读入


#include <iostream>
#include <cstdio>
#include <stack>
#include <sstream>
#include <limits.h>
#include <vector>
#include <map>
#include <cstring>
#include <deque>
#include <cmath>
#include <iomanip>
#include <queue>
#include <algorithm>
#include <set>
#define Mid ((l + r) >> 1) 
#define Lson rt << 1, l , mid
#define Rson rt << 1|1, mid + 1, r
#define ms(a,al) memset(a,al,sizeof(a))
#define log2(a) log(a)/log(2)
#define _for(i,a,b) for( int i = (a); i < (b); ++i)
#define _rep(i,a,b) for( int i = (a); i <= (b); ++i)
#define for_(i,a,b) for( int i = (a); i >= (b); -- i)
#define rep_(i,a,b) for( int i = (a); i > (b); -- i)
#define lowbit(x) ((-x) & x)
#define IOS std::ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define INF 0x3f3f3f3f
#define LLF 0x3f3f3f3f3f3f3f3f
#define hash Hash
#define next Next
#define pb push_back
#define f first
#define s second
using namespace std;
const int N = 5e6 + 10, MOD = 998244353;
const long double eps = 1e-5;
const int p = 2333;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> PII;
typedef pair<ll,ll> PLL;
typedef pair<double,double> PDD;
template<typename T> void read(T &x)
{
    x = 0;char ch = getchar();ll f = 1;
    while(!isdigit(ch)){if(ch == '-')f*=-1;ch=getchar();}
    while(isdigit(ch)){x = x*10+ch-48;ch=getchar();}x*=f;
}
template<typename T, typename... Args> void read(T &first, Args& ... args) 
{
    read(first);
    read(args...);
} 
int n; 
char str[N];
ll ans;
int idx, var; //顶点和内存池
int cnt[N];
// 插入一个字符串
struct node {
	char val;
	int to;
	int next;
}e[N];
int head[N];

void insert()
{
    int p = 0;
    char C = getchar();
    for (int i = 0; C != '\n'; i ++, C = getchar())
    {
        int v = -1;
        for(int i = head[p]; ~i; i = e[i].next)
           if(e[i].val == C) 
		   {
		      v = e[i].to;
		      p = v;
		      break;
		   }
		if(v == -1) 
		{
			e[idx] = {C,++ var/*开新节点*/,head[p]};
			head[p] = idx ++;//开新边
			p = var;
		}
		cnt[p] ++;
        ans = max((ll)cnt[p] * (i + 1),ans);
    }
   
}

int main()
{
     ms(head,-1);
	 read(n);
	 for(int i = 0; i < n; ++ i)
		insert();
	  printf("%lld\n",ans);
      return 0;
} 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值