2018华工校赛 E Youhane Assembler

设查询的字符串为A和B,B接到A前面(中间用不在字符集内的字符隔开),求next数组的最后一项。因为next[i] = 同时是字串0~i的前缀/后缀的最长字串的长度。

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<iostream>
#include<string>
#include<map>
using namespace std;
typedef long long ll;

int N;
map< int , string >mmp;
int Q;
int l,r;
int lps[300005];
map< pair< int , int > , int >res;

int min2(int a,int b){return a<b?a:b;}
void initlps(string x){
    lps[0]=lps[1]=0;
    int len = x.length();
    for(int i=1;i<len;i++){
        int j = lps[i];
        while(j&&x[i]!=x[j]){
            j = lps[j];
        }
        lps[i+1] = (x[i]==x[j])? j+1: 0;
    }
}
int chk(int l,int r){
    string t = mmp[r] + "$" + mmp[l];
    initlps(t);
    /*
    cout<<t<<endl;
    cout<<"-> "; for(int i=0;i<=t.length();i++){
        cout<<lps[i]<<" ";
    } cout<<endl;
    */
    int lenr = mmp[r].length(); lenr++;
    int lenl = mmp[l].length();
    return min2(lps[lenl+lenr], min2(lenr-1, lenl));
}
int main(){
    ios::sync_with_stdio(false);
    cin>>N;
    for(int i=0;i<N;i++){
        string x; cin>>x; mmp[i+1]=x;
    }
    cin>>Q;
    for(int i=0;i<Q;i++){
        cin>>l>>r;
        cout<<chk(l,r)<<endl;
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值