JZOJ5795. 2018.08.10【2018提高组】模拟A组&省选 词典

6 篇文章 0 订阅

题目

这里写图片描述
这里写图片描述

题解

其实这题也非常简单,
想到一种非常非常暴力的做法,
用所有的t串,建出一棵tire,
然后每个节就将它子树里面所有的结尾作为1,然后求出全0串。
其实正解的思路差不多,
每个节点维护一个lst和v,
顺序将t插入,
假设当前 ti t i 遍历到点x,
那么就 vx=max(vx,ilstx1),lstx=i v x = m a x ( v x , i − l s t x − 1 ) , l s t x = i
这个很好理解吧。

code

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <string.h>
#include <cmath>
#define ll long long
#define N 8000003
#define M 1003
#define P putchar
#define G getchar
using namespace std;
char ch;
void read(int &n)
{
    n=0;
    ch=G();
    while((ch<'0' || ch>'9') && ch!='-')ch=G();
    ll w=1;
    if(ch=='-')w=-1,ch=G();
    while('0'<=ch && ch<='9')n=(n<<3)+(n<<1)+ch-'0',ch=G();
    n*=w;
}

int max(int a,int b){return a<b?b:a;}
void write(ll x){if(x>9) write(x/10);P(x%10+'0');}

int v[N],z[M][M],son[3][N],tot,lst[N],n,m,x,y;

int main()
{
    freopen("word.in","r",stdin);
    freopen("word.out","w",stdout);

    read(n);read(m);tot=1;
    for(int i=1;i<=n;i++)
    {
        for(;ch<'a' || ch>'c';ch=G());
        for(x=1;'a'<=ch && ch<='c';ch=G())
        {
            v[x]=max(v[x],i-lst[x]-1);
            lst[x]=i;
            if(son[ch-'a'][x]==0)son[ch-'a'][x]=++tot;
            x=son[ch-'a'][x];
        }
        v[x]=max(v[x],i-lst[x]-1);
        lst[x]=i;
    }

    for(int i=1;i<=tot;i++)
        v[i]=max(v[i],n-lst[i]);

    for(int i=1;i<=m;i++)
    {
        for(;ch<'a' || ch>'c';ch=G());y=1;
        for(x=1;'a'<=ch && ch<='c';ch=G())
        {
            if(son[ch-'a'][x]==0)y=0;
            x=son[ch-'a'][x];
        }

        if(y)write(v[x]);else write(n);
        P('\n');

    }

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值