比赛 欢乐五一练练练 评测结果 AAAAAAAAAA
题目名称 很长的信息 最终得分 100
用户昵称 sxysxy 运行时间 1.259 s
代码语言 C++ 内存使用 8.30 MiB
提交时间 2017-04-26 19:17:44
显示代码纯文本
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <list>
#include <queue>
#include <algorithm>
using namespace std;
typedef unsigned long long ULL;
#define MAXN 200003 
ULL xp[MAXN];
ULL hasha[MAXN];
ULL hashb[MAXN];
ULL ha[MAXN];
ULL hb[MAXN];

char A[MAXN], B[MAXN];
int x = 233;
int la, lb;
int mlen;
int minlen;
bool check(int len)
{
    for(int i = 0; i < la-len+1; i++)
        hasha[i] = ha[i] - ha[i+len]*xp[len];
    for(int i = 0; i < lb-len+1; i++)
        hashb[i] = hb[i] - hb[i+len]*xp[len];
    sort(hashb, hashb+lb-len+1);
    for(int i = 0; i < la-len+1; i++)
    {
        ULL h = hasha[i];
        int p = lower_bound(hashb, hashb+lb-len+1, h)-hashb;
        if(p < lb-len+1 && h == hashb[p])return true;
    }
    return false;
}

int main()
{
    freopen("longlongmessage.in", "r", stdin);
    freopen("longlongmessage.out", "w", stdout);
    scanf("%s %s", A, B);
    la = strlen(A);
    lb = strlen(B);
    mlen = max(la, lb);
    minlen = min(la, lb);
    for(int i = la-1; i >= 0; i--)
        ha[i] = ha[i+1]*x + A[i];
    for(int i = lb-1; i >= 0; i--)
        hb[i] = hb[i+1]*x + B[i];
    xp[0] = 1;
    for(int i = 1; i <= mlen; i++)
        xp[i] = xp[i-1]*x;
    int l = 0, r = minlen;
    int ans;
    while(l <= r)
    {
        int m = (l+r)>>1;
        if(check(m))
        {
            ans = m;
            l = m+1;
        }else
            r = m-1;
    }
    printf("%d\n", ans);
    return 0;
}