Long Long Message (SAM——两个字符串的最长公共子串)

https://cn.vjudge.net/problem/POJ-2774

The little cat is majoring in physics in the capital of Byterland. A piece of sad news comes to him these days: his mother is getting ill. Being worried about spending so much on railway tickets (Byterland is such a big country, and he has to spend 16 shours on train to his hometown), he decided only to send SMS with his mother. 

The little cat lives in an unrich family, so he frequently comes to the mobile service center, to check how much money he has spent on SMS. Yesterday, the computer of service center was broken, and printed two very long messages. The brilliant little cat soon found out: 

1. All characters in messages are lowercase Latin letters, without punctuations and spaces. 
2. All SMS has been appended to each other – (i+1)-th SMS comes directly after the i-th one – that is why those two messages are quite long. 
3. His own SMS has been appended together, but possibly a great many redundancy characters appear leftwards and rightwards due to the broken computer. 
E.g: if his SMS is “motheriloveyou”, either long message printed by that machine, would possibly be one of “hahamotheriloveyou”, “motheriloveyoureally”, “motheriloveyouornot”, “bbbmotheriloveyouaaa”, etc. 
4. For these broken issues, the little cat has printed his original text twice (so there appears two very long messages). Even though the original text remains the same in two printed messages, the redundancy characters on both sides would be possibly different. 

You are given those two very long messages, and you have to output the length of the longest possible original text written by the little cat. 

Background: 
The SMS in Byterland mobile service are charging in dollars-per-byte. That is why the little cat is worrying about how long could the longest original text be. 

Why ask you to write a program? There are four resions: 
1. The little cat is so busy these days with physics lessons; 
2. The little cat wants to keep what he said to his mother seceret; 
3. POJ is such a great Online Judge; 
4. The little cat wants to earn some money from POJ, and try to persuade his mother to see the doctor :( 

Input

Two strings with lowercase letters on two of the input lines individually. Number of characters in each one will never exceed 100000.

Output

A single line with a single integer number – what is the maximum length of the original text written by the little cat.

Sample Input

yeshowmuchiloveyoumydearmotherreallyicannotbelieveit
yeaphowmuchiloveyoumydearmother

Sample Output

27

 

#include<cstdio>
#include<cstring>
#include<algorithm>
//#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=1e5+10;//字符串长度 
const int maxc=26;
char s[maxn];
struct Suffix_Automaton {//打*的不一定用到 
    int next[maxn<<1][maxc];  //状态转移(尾部加一个字符的下一个状态)
	int len[maxn<<1]; //最长子串的长度(该节点子串数量=len[x]-len[link[x]])
	int link[maxn<<1];   //后缀链接(最短串前部减少一个字符所到达的状态)
	//int cnt[maxn<<1];   //被后缀连接的数(*) 
	int id; //结点编号
	int last; //最后结点
	ll endpos[maxn<<1]; // endpos数(一类子串的数量)
	int a[maxn];		
    int b[maxn<<1];
    //ll d[maxn<<1];//d[i]表示从状态i出发,不同的子串的数目,即不同的路径数
	void init() {	//初始化
		for(int i=1; i<=id; i++){ //常规初始化 
			link[i] = len[i] = 0;
			memset(next[i],0,sizeof(next[i]));
			endpos[i]=0;
			a[i]=0;
			b[i]=0;
		}
//		for(int i=1;i<=id;i++) {//非常规初始化 
//			d[i]=0;
//		}
		last = id = 1; //1表示root起始点 空集
	}
//SAM建图
	void add(int c) {     //插入字符,为字符ascll码值
		int x = ++id; //创建一个新结点x;
		len[x] = len[last] + 1; //  长度等于最后一个结点+1
		endpos[x] = 1;  //接受结点子串除后缀连接还需加一
		int p;  //第一个有C转移的结点;
		for (p = last; p && !next[p][c]; p = link[p])
			next[p][c] = x;//沿着后缀连接 将所有没有字符c转移的节点直接指向新结点
		if (!p){   //全部都没有c的转移 直接将新结点后缀连接到起点
			link[x] = 1;
		//	cnt[1]++;  
	    }
		else {
			int q = next[p][c];    //p通过c转移到的结点
			if (len[p] + 1 == len[q]){//pq是连续的
				link[x] = q;
			//	cnt[q]++; //将新结点后缀连接指向q即可,q结点的被后缀连接数+1
			} 
			else {
				int nq = ++id;   //不连续 需要复制一份q结点
				len[nq] = len[p] + 1;   //令nq与p连续
				link[nq] = link[q];   //因后面link[q]改变此处不加cnt
				memcpy(next[nq], next[q], sizeof(next[q]));  //复制q的信息给nq
				for (; p&&next[p][c] == q; p = link[p])
					next[p][c] = nq;    //沿着后缀连接 将所有通过c转移为q的改为nq
				link[q] = link[x] = nq; //将x和q后缀连接改为nq
				//cnt[nq] += 2; //  nq增加两个后缀连接	
			}
		}
		last = x;  //更新最后处理的结点	
	} 
	ll getSubNum() {	//求不相同子串数量
		ll ans = 0;
		for (int i = 2; i <= id; i++)
			ans += len[i]-len[link[i]];	//一状态子串数量等于len[i]-len[link[i]]
		return ans;
	} 
	void getTP(int Len){//对sam的节点按照len,从小到大排序重新标号,即给定节点的拓扑序
	    for(int i=1;i<=id;i++) a[len[i]]++;
		for(int i=1;i<=Len;i++) a[i]+=a[i-1];
		for(int i=1;i<=id;i++) b[a[len[i]]--]=i;
	}
	void getendpos(){//求每类子串的数量 ,即endpos集合的大小 
		for(int i=id;i>=1;i--){ //按拓扑序遍历 
			int e=b[i];
			endpos[link[e]]+=endpos[e];
		} 
	}
	void LLM(char s[],int Len){//求两个串的最长公共子串
	    int ans=0,cnt=0;
	    int now=1;
	    char base='a';
	    for(int i=0;i<Len;i++){
	      	int c=s[i]-base;
	      	if(next[now][c]){
	      	    cnt++;
	      	    now=next[now][c];
			}
			else{
			  	while(now&&!next[now][c]) now=link[now];
			  	if(!now) cnt=0,now=1;
			  	else cnt=len[now]+1,now=next[now][c];
			}
			ans=max(ans,cnt);
		}
		printf("%d\n",ans);
	} 
	/*
	void solve1(){ //求出现次数为k的子串种数 
		ll ans=0;
		for(int i=1;i<=id;i++){
			if(endpos[i]==K){
				ans+=len[i]-len[link[i]];
			}
		}
		printf("%lld\n",ans);
	}
	 */
	/*
	void solve1(){
		for(int i=id;i>1;i--){
			int v=b[i];
			if(endpos[v]>=A&&endpos[v]<=B) d[v]++;
			for(int j=0;j<26;j++){
				if(next[v][j]) d[v]+=d[next[v][j]];
			}
		}
		ll ans=0;
		for(int i=0;i<26;i++){
			if(next[1][i]) ans+=d[next[1][i]];
		}
		printf("%lld\n",ans);
	}
	*/
	/*
	void solve2(){//求出现次数>=k的子串的最大长度 
		int ans=0;
		for(int i=1;i<=id;i++){
			if(endpos[i]>=K){
				ans=max(ans,len[i]);
			}
		}
		printf("%d\n",ans);
	}*/
} sam;
int main(){
    scanf("%s",s);
    int len=strlen(s);
    sam.init();
    for(int i=0;i<len;i++){
    	sam.add(s[i]-'a');
	}
	scanf("%s",s);
	len=strlen(s);
	sam.LLM(s,len);
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值