• SPOJ: LCS


    题面

    输入(2)个长度不大于(250000)的字符串,输出这(2)个字符串的最长公共子串。如果没有公共子串则输出(0)

    Sol

    一个串建立(sam)
    另一个串在上面匹配

    # include <bits/stdc++.h>
    # define IL inline
    # define RG register
    # define Fill(a, b) memset(a, b, sizeof(a))
    using namespace std;
    typedef long long ll;
    
    template <class Int>
    IL void Input(RG Int &x){
    	RG int z = 1; RG char c = getchar(); x = 0;
    	for(; c < '0' || c > '9'; c = getchar()) z = c == '-' ? -1 : 1;
    	for(; c >= '0' && c <= '9'; c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
    	x *= z;
    }
    
    const int maxn(5e5 + 5);
    
    int n, trans[26][maxn], fa[maxn], len[maxn], tot = 1, last = 1, ans;
    char s[maxn];
    
    IL void Extend(RG int c){
    	RG int np = ++tot, p = last; last = np;
    	len[np] = len[p] + 1;
    	while(p && !trans[c][p]) trans[c][p] = np, p = fa[p];
    	if(!p) fa[np] = 1;
    	else{
    		RG int q = trans[c][p];
    		if(len[q] == len[p] + 1) fa[np] = q;
    		else{
    			RG int nq = ++tot;
    			len[nq] = len[p] + 1, fa[nq] = fa[q];
    			for(RG int i = 0; i < 26; ++i) trans[i][nq] = trans[i][q];
    			fa[q] = fa[np] = nq;
    			while(p && trans[c][p] == q) trans[c][p] = nq, p = fa[p];
    		}
    	}
    }
    
    int main(RG int argc, RG char* argv[]){
    	scanf(" %s", s), n = strlen(s);
    	for(RG int i = 0; i < n; ++i) Extend(s[i] - 'a');
    	scanf(" %s", s), n = strlen(s);
    	for(RG int i = 0, nw = 1, cnt = 0; i < n; ++i){
    		RG int c = s[i] - 'a';
    		if(trans[c][nw]) ++cnt, nw = trans[c][nw];
    		else{
    			while(nw && !trans[c][nw]) nw = fa[nw], cnt = len[nw];
    			if(!nw) nw = 1, cnt = 0;
    			else cnt++, nw = trans[c][nw];
    		}
    		ans = max(ans, cnt);
    	}
    	printf("%d
    ", ans);
    	return 0;
    }
    
  • 相关阅读:
    线程安全和非线程安全
    spring MVC和hibernate的结合
    Spring学习笔记1——基础知识 (转)
    bitset && Luogu 3674 小清新人渣的本愿
    luogu P3452 [POI2007]BIU-Offices
    每日刷题记录
    Codeforces Round #721 (Div. 2) B2. Palindrome Game (hard version)
    2019湘潭邀请赛A
    2021CCPC浙江省赛 B
    Codeforces Round #720 (Div. 2) D
  • 原文地址:https://www.cnblogs.com/cjoieryl/p/8900910.html
Copyright © 2020-2023  润新知