POJ 2774 Long Long Message(后缀数组求最长公共子串,4级)
来源:互联网 发布:大数据可视化工具三维 编辑:程序博客网 时间:2024/05/18 02:24
J - Long Long Message
Time Limit:4000MS Memory Limit:131072KB 64bit IO Format:%I64d & %I64uAppoint description:
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
#include<iostream>#include<cstring>#include<cstdio>#define ll(x) (1<<x)#define FOR(i,a,b) for(int i=a;i<=b;++i)#define clr(f,z) memset(f,z,sizeof(f))using namespace std;const int msize=2e5+9;class SUFFIX_ARRAY{public: int rank[msize],h[msize],c[msize],sa[msize]; bool cmp(int*r,int i,int k) { return r[ sa[i] ]==r[ sa[i-1] ]&&r[ sa[i]+k ]==r[ sa[i-1]+k ]; } void build_SA(char*s,int n,int m) { int*wx=h,*wy=rank; FOR(i,0,m-1)c[i]=0; FOR(i,0,n-1)++c[ wx[i]=s[i] ]; FOR(i,1,m-1)c[i]+=c[i-1]; for(int i=n-1;i>=0;--i)sa[ --c[ wx[i] ] ]=i; for(int k=1;k<=n;k<<=1) { int p=0; FOR(i,n-k,n-1)wy[p++]=i; FOR(i,0,n-1)if(sa[i]>=k)wy[p++]=sa[i]-k; FOR(i,0,m-1)c[i]=0; FOR(i,0,n-1)++c[ wx[ wy[i] ] ]; FOR(i,1,m-1)c[i]+=c[i-1]; for(int i=n-1;i>=0;--i)sa[ --c[ wx[ wy[i] ] ] ]=wy[i]; swap(wx,wy); p=1;wx[ sa[0] ]=0; FOR(i,1,n-1)wx[ sa[i] ]=cmp(wy,i,k)?p-1:p++; if(p>=n)break; m=p; } } void get_H(char*s,int n) { int k=0; FOR(i,0,n)rank[ sa[i] ]=i; FOR(i,0,n-1) { if(k)--k; int j=sa[ rank[i]-1 ]; while(s[i+k]==s[j+k])++k; h[ rank[i] ]=k; } } void debug(int n) { printf("sa="); FOR(i,0,n)printf("%d ",sa[i]);puts(""); printf("rank="); FOR(i,0,n)printf("%d ",rank[i]);puts(""); printf("h="); FOR(i,0,n)printf("%d ",h[i]);puts(""); } int rmq[msize][33],bit[msize]; void initRMQ(int n) { bit[0]=-1; FOR(i,1,n)bit[i]=(i&(i-1))==0?bit[i-1]+1:bit[i-1]; FOR(i,1,n)rmq[i][0]=h[i]; FOR(i,1,bit[n]) for(int j=1;j+ll(i)-1<=n;++j) rmq[j][i]=min(rmq[j][i-1],rmq[j+ll(i-1)][i-1]); } int LCP(int l,int r) { l=rank[l],r=rank[r]; if(l>r)swap(l,r); ++l; int t=bit[r-l+1]; r-=ll(t)-1;///这个居然忘了 return min(rmq[l][t],rmq[r][t]); } int has[msize]; void getstring(char*s,int len,int n) { int ans=0; FOR(i,1,n) if((sa[i]>len&&sa[i-1]<len)||(sa[i]<len&&sa[i-1]>len)) ans=max(ans,h[i]); printf("%d\n",ans); }};SUFFIX_ARRAY ty;char s[2][msize+msize];int r[msize];int main(){ int cas=0; //freopen("data.in","r",stdin); while(~scanf("%s%s",s[0],s[1])) { int len,kn; kn=len=strlen(s[0]); s[0][len]=127; for(int i=0;s[1][i];++i) s[0][++len]=s[1][i]; s[0][++len]=0; ++len; ty.build_SA(s[0],len+1,128); ty.get_H(s[0],len); //ty.initRMQ(len); ty.getstring(s[0],kn,len); }}
- POJ 2774 Long Long Message(后缀数组求最长公共子串,4级)
- POJ 2774 Long Long Message 求两个串最长公共子串(后缀数组)
- POJ 题目2774 Long Long Message(后缀数组,求最长公共子串长度)
- POJ 2774 Long Long Message (后缀数组求最长公共子串)
- poj 2774 Long Long Message,后缀数组,求最长公共子串 hdu1403
- POJ 2774 long long message(后缀数组求最长公共子串)
- POJ 2774 Long Long Message 后缀数组求最长公共子串
- POJ 2774 Long Long Message(最长公共子串 -初学后缀数组)
- Long Long Message+POJ+后缀数组之求最长公共子串
- POJ 2774 Long Long Message(后缀数组:公共子串)
- POJ 2774 Long Long Message(后缀数组:公共子串)
- POJ2774---Long Long Message(后缀数组,求最长公共子串)
- 【POJ2774】Long Long Message-求最长公共子串(后缀数组求法)
- POJ 2774 Long Long Message+Hdu 1403 Longest Common Substring (后缀数组 最长公共子串)
- 【后缀数组】 POJ 2774 Long Long Message 两个字符串的最长公共子串长度
- POJ 2774 Long Long Message(后缀数组[最长公共子串])
- 【后缀数组】【最长公共子串】Long Long Message POJ2774
- POJ 2774 Long Long Message(最长公共子串)
- LoadIcon的使用
- 基于linux-2.6.38.8内核的wifi驱动分析
- 已知中序遍历和后序遍历,求前序遍历
- 编码学习记录:输入包含N个自然数(N<10000),每个数不大于15000(鸽巢原理)
- 三星GT S7562 PIN 解锁方法
- POJ 2774 Long Long Message(后缀数组求最长公共子串,4级)
- 我的Android进阶之旅------>Android服务的生命周期回调方法
- Linux下Liberats SDIO wifi驱动分析
- android adapter 深刻分析
- 求n个数中最大值和最小值
- oracle导出
- FZU 1683 纪念SlingShot (构造矩阵)
- 题目1507:不用加减乘除做加法
- 为什么TCP是个可靠的协议?