POJ 2774 Long Long Message(后缀数组 两个字符串的最长公共字串 )
来源:互联网 发布:mac哪里下载软件 编辑:程序博客网 时间:2024/06/05 08:10
Long Long Message
Time Limit: 4000MS Memory Limit: 131072KTotal Submissions: 31587 Accepted: 12751Case Time Limit: 1000MS
Description
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 :(
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
yeshowmuchiloveyoumydearmotherreallyicannotbelieveityeaphowmuchiloveyoumydearmother
Sample Output
27
题意:求两个字符串的最长公共字串
解:模板题,直接利用height数组 判断是否在不同的字符串中 更新答案
这篇博客解释的很详细http://blog.csdn.net/zy691357966/article/details/46996799
#include<iostream>#include<algorithm>#include<cstdio>#include<cstdlib>#include<cstring>#include<vector>#include<map>#include <set>//#include <bits/stdc++.h>using namespace std;const int N = 4e5+10;typedef long long LL;typedef pair<int,int>pi;const LL mod = 1e9+7;const int MAXN=300000+5;char str1[MAXN],str2[MAXN];int sa[MAXN];int t1[MAXN],t2[MAXN],c[MAXN];int rank1[MAXN],height[MAXN];void build_sa(int s[],int n ,int m){ int i,j,p,*x=t1,*y=t2; for(int i=0;i<m;i++) c[i]=0; for(int i=0;i<n;i++) c[x[i]=s[i]]++; for(int i=0;i<m;i++) c[i]+=c[i-1]; for(int i=n-1;i>=0;i--) sa[--c[x[i]]]=i; for(j=1;j<=n;j<<=1) { p=0; for(i=n-j;i<n;i++) y[p++]=i; for(i=0;i<n;i++) if(sa[i]>=j) y[p++]=sa[i]-j; for(i=0;i<m;i++) c[i]=0; for(i=0;i<n;i++) c[x[y[i]]]++; for(i=0;i<m;i++) c[i]+=c[i-1]; for(i=n-1;i>=0;i--) sa[--c[x[y[i]]]]=y[i]; swap(x,y); p=1;x[sa[0]]=0; for(i=1;i<n;i++) x[sa[i]]=(y[sa[i-1]]==y[sa[i]])&&(y[sa[i-1]+j]==y[sa[i]+j])?p-1:p++; if(p>=n) break; m=p; }}int s[MAXN];void getheight(int s[],int n){ int i, j, k=0; for(i=0;i<=n;i++) rank1[sa[i]]=i; for(i=0;i<n;i++) { if(k) k--; j=sa[rank1[i]-1]; while(s[i+k]==s[j+k]) k++; height[rank1[i]]=k; } return ;}int main(){ while(scanf("%s",str1)!=EOF) { int len1=strlen(str1); for(int i=0;i<len1;i++) s[i]=str1[i]; s[len1]='*'; scanf("%s",str2); int len2=strlen(str2); for(int i=len1+1,j=0;j<=len2;i++,j++) s[i]=str2[j]; build_sa(s,len1+len2+2,256); getheight(s,len1+len2+1); int ans=0; for(int i=2;i<=len1+len2+1;i++) { int maxt=max(sa[i-1],sa[i]); int mint=min(sa[i-1],sa[i]); if(mint<len1&&maxt>len1) ans=max(ans,height[i]); } printf("%d\n",ans); } return 0;}
阅读全文
0 0
- POJ 2774 Long Long Message(后缀数组 两个字符串的最长公共字串 )
- 【后缀数组】 POJ 2774 Long Long Message 两个字符串的最长公共子串长度
- POJ 2774 Long Long Message 求两个串最长公共子串(后缀数组)
- (Relax 后缀数组1.4)POJ 2774 Long Long Message(求两个字符串公共子串的最大长度)
- poj2774Long Long Message【后缀数组求最长公共字串】
- POJ 2774 Long Long Message(2.3 求两个字符串的最长公共子串)
- POJ 2774 Long Long Message+Hdu 1403 Longest Common Substring (后缀数组 最长公共子串)
- POJ 2774 Long Long Message(后缀数组求最长公共子串,4级)
- poj 2774 Long Long Message,后缀数组,求最长公共子串 hdu1403
- POJ 2774 Long Long Message(最长公共子串 -初学后缀数组)
- POJ 题目2774 Long Long Message(后缀数组,求最长公共子串长度)
- POJ 2774 long long message(后缀数组求最长公共子串)
- POJ 2774 Long Long Message (后缀数组求最长公共子串)
- POJ 2774 Long Long Message(后缀数组[最长公共子串])
- POJ 2774 Long Long Message 后缀数组求最长公共子串
- POJ 2774 Long Long Message(后缀数组:公共子串)
- POJ 2774 Long Long Message(后缀数组:公共子串)
- poj2774 Long Long Message && hdu 1403 Longest Common Substring 最长公共字串【后缀数组(倍增)】
- uva 11292 智者斗恶龙
- 关于补零扩展与补符号位扩展
- sql 面试题
- Android屏幕尺寸获取方法详解
- ThinkPHP5学习笔记【持续更新……】
- POJ 2774 Long Long Message(后缀数组 两个字符串的最长公共字串 )
- Gradle 'VitamioBundleStudio-master' project refresh failed
- 坚持是一件困难的事情
- Oraclr数据库安装检查失败解决处理过程
- vector向量类(2)
- 算法 第四版 2.2.8
- Java Executors(线程池)
- android webview Not allowed to load local resource错误的解决办法
- xxx上