POJ 2774 Long Long Message
2016-02-13 21:49
281 查看
本题地址:http://poj.org/problem?id=2774
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:
All characters in messages are lowercase Latin letters, without punctuations and spaces.
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.
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.
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.
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 :(
yeaphowmuchiloveyoumydearmother
第一次过后缀数组+高度数组,泪流涕下啊(HDU1403莫名WA)
这里的后缀数组是O(nlog²n)的版本
ps:子序列(subsequence)是不改变序列内元素顺序的前提下删除一些元素得到的新序列,而子串是原串中连续的一段
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:
All characters in messages are lowercase Latin letters, without punctuations and spaces.
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.
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.
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
27Source
POJ Monthly–2006.03.26,Zeyuan Zhu,”Dedicate to my great beloved mother.”题解
很久没有写博客了。这题勒,后缀数组+高度数组的裸题:最长公共子串(这不很明显吗)。第一次过后缀数组+高度数组,泪流涕下啊(HDU1403莫名WA)
这里的后缀数组是O(nlog²n)的版本
ps:子序列(subsequence)是不改变序列内元素顺序的前提下删除一些元素得到的新序列,而子串是原串中连续的一段
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <string> using namespace std; #define MAX_N 200010 int n,k; int zrank[MAX_N+1]; int tmp[MAX_N+1]; bool cmp_sa(int i,int j) { if(zrank[i]!=zrank[j]) return zrank[i]<zrank[j]; else { int ri=i+k<=n?zrank[i+k]:-1; int rj=j+k<=n?zrank[j+k]:-1; return ri<rj; } } void construct_sa(string S,int *sa) { n=S.length(); for(int i=0;i<=n;i++) { sa[i]=i; zrank[i]=i<n?S[i]:-1; } for(k=1;k<=n;k*=2) { sort(sa,sa+n+1,cmp_sa); tmp[sa[0]]=0; for(int i=1;i<=n;i++) tmp[sa[i]]=tmp[sa[i-1]] +(int)(cmp_sa(sa[i-1],sa[i])); for(int i=0;i<=n;i++) { zrank[i]=tmp[i]; } } } void construct_lcp(string S,int *sa,int *lcp) { int n=S.length(); for(int i=0;i<=n;i++) zrank[sa[i]]=i; int h=0; lcp[0]=0; for(int i=0;i<n;i++) { int j=sa[zrank[i]-1]; if(h>0) h--; while(j+h<n&&i+h<n) { if(S[j+h]!=S[i+h]) break; h++; } lcp[zrank[i]-1]=h; } } #define MAX_L 200010 string S,T; int sa[MAX_L],lcp[MAX_L]; int main() { cin>>S>>T; int s1=S.length(); S+='\0'+T; construct_sa(S,sa); construct_lcp(S,sa,lcp); int ans=0; for(int i=0;i<S.length();i++) if((sa[i]<s1)!=(sa[i+1]<s1)) ans=max(ans,lcp[i]); printf("%d",ans); }
相关文章推荐
- 初学ACM - 组合数学基础题目PKU 1833
- POJ ACM 1001
- POJ ACM 1002
- 1611:The Suspects
- POJ1089 区间合并
- POJ 2635 The Embarrassed Cryptographe
- POJ 3292 Semi-prime H-numbers
- POJ 2773 HAPPY 2006
- POJ 3090 Visible Lattice Points
- POJ-2409-Let it Bead&&NYOJ-280-LK的项链
- POJ-1695-Magazine Delivery-dp
- POJ1523 SPF dfs
- POJ-1001 求高精度幂-大数乘法系列
- POJ-1003 Hangover
- POJ-1004 Financial Management
- 用单调栈解决最大连续矩形面积问题
- 2632 Crashing Robots的解决方法
- 1573 Robot Motion (简单题)
- POJ 1200 Crazy Search(简单哈希)
- 【高手回避】poj3268,一道很水的dijkstra算法题