poj2774

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 :(

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

调了一个晚上,终于写出一份自己风格的SA

#include<cstdio>
#include<cstring>
#include<algorithm>
#define MN 200001
using namespace std;

int n,m=0;
char s1[MN];
int s[MN],ss[MN];
int v[MN],a[MN],sa[MN],q[MN],rank[MN],h[MN],mmh=0,len;
inline void gr(int x){
    rank[sa[1]]=1;
    for (int i=2;i<=m;i++) rank[sa[i]]=(s[sa[i]]==s[sa[i-1]]&&s[sa[i]+x]==s[sa[i-1]+x])?rank[sa[i-1]]:rank[sa[i-1]]+1;
    for (int i=1;i<=m;i++) s[i]=rank[i];
}
inline void qz(){for (int i=1;i<=m;i++) v[i]+=v[i-1];}
inline void getsa(){
    for (int j=1;j<=m;j++) v[j]=0;
    for (int i=1;i<=m;i++) v[s[i]]++,ss[i]=s[i];qz();
    for (int i=m;i>=1;i--) a[i]=v[s[i]],sa[v[s[i]]--]=i;gr(0);
    for (int i=1;i<m;i<<=1){
        for (int j=1;j<=m;j++) v[j]=0;
        for (int j=1;j<=m;j++) v[rank[j]]++;qz();
        for (int j=m;j>=1;j--) if (sa[j]>i) a[sa[j]-i]=v[rank[sa[j]-i]],q[v[rank[sa[j]-i]]--]=sa[j]-i;
        for (int j=m-i+1;j<=m;j++) a[j]=v[rank[j]],q[v[rank[j]]--]=j;
        for (int j=1;j<=m;j++) sa[j]=q[j];gr(i);
    }
}
inline void geth(){for (int i=1,k=0,j;i<=m;h[rank[i++]]=k) for (k?k--:0,j=sa[rank[i]-1];ss[i+k]==ss[j+k]&&i+k<=m&&j+k<=m;k++);}
int main(){
    scanf("%s",s1);
    len=n=strlen(s1);
    for (int i=0;i<n;i++) s[++m]=s1[i]-‘a‘+1;
    scanf("%s",s1);
    s[++m]=29;
    n=strlen(s1);
    for (int i=0;i<n;i++) s[++m]=s1[i]-‘a‘+1;
    getsa();
    geth();
    for (int i=2;i<=m;i++)
    if (mmh<h[i]&&((sa[i]<=len&&sa[i-1]>len)||(sa[i-1]<=len&&sa[i]>len))) mmh=h[i];
    printf("%d\n",mmh);
}

 
时间: 2024-10-06 18:36:59

poj2774的相关文章

POJ-2774 后缀数组基础题

POJ 2774 题意:求两个字符串的最长公共子序列. 总结:搞了半天还是不太理解,看着大神博客强行敲的..而且还看到有hash+二分做的. // POJ-2774 #include<iostream> #include<cstdio> #include<cstdlib> #include<algorithm> #include<cstring> #include<string> #include<cmath> #incl

【poj2774】 Long Long Message

[http://poj.org/problem?id=2774] (题目链接) 第一次用后缀数组,感觉有点神...才发现原来sa[0]是没用的.. 题意:给出两个只包含小写字母的字符串,求出最长连续公共子串. solution  将两个字符串合并为一个,并用分隔符隔开.之后跑后缀数组,求出height[],for一遍,找到在分隔符两侧的height值最大的便是答案. 代码: // poj2774 #include<algorithm> #include<iostream> #inc

[POJ2774]Long Long Message 解题报告

Long Long Message 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

【POJ2774】Long Long Message(后缀数组)

[POJ2774]Long Long Message(后缀数组) 题面 Vjudge Description Little cat在Byterland的首都读物理专业.这些天他收到了一条悲伤地信息:他的母亲生病了.担心买火车票花钱太多(Byterland是一个巨大的国家,因此他坐火车回家需要16小时),他决定只给母亲发短信. Little cat的家境并不富裕,因此他经常去营业厅查看自己发短信花了多少钱.昨天营业厅的电脑坏掉了,打印出两条很长的信息.机智的little cat很快发现: 1.信息

HiHocoder1415 : 后缀数组三&#183;重复旋律3 &amp; Poj2774:Long Long Message

题面 HiHocoder1415 Poj2774 Sol 都是求最长公共子串,\(hihocoder\)上讲的很清楚 把两个串拼在一起,中间用一个特殊字符隔开 那么答案就是排序后相邻两个不同串的后缀的\(height\) 为什么呢? 如果答案为不相邻的两个后缀的前缀,计算它们最长前缀时必定要跨越过这些中间\(height\)值,也就是选相邻的两个一定要比不选相邻的两个更优 # include <bits/stdc++.h> # define IL inline # define RG regi

POJ2774 Long Long Message 【SAM】

POJ2774 Long Long Message 找两个串的最长公共字串 对其中一个串\(s\)建\(SAM\),然后我们如何找到最长公共字串,办法就是枚举\(t\)串所有的前缀,然后找各个前缀的最长能和\(s\)串匹配的后缀. 如果一个个跑需要\(O(n^2)\),\(SAM\)可以来保存之前匹配的状态,假设现在匹配的状态是\(u\),匹配到的最长后缀长度为\(l\),那么现在考虑在当前状态后面加上一个字符,也就是成为\(t\)串一个新的前缀,那么最大能匹配的必然是在上一次匹配到的最长串的基

POJ2774 Long Long Message(后缀数组)

求两个字符串的最长公共子串. 拼接两个字符串中间用特殊字符隔开,max(height[i])(2<=i<=len,suffix(sa[i])与suffix(sa[i-1])分别属于两个字符串的后缀)就是答案! 1 #include<cstdio> 2 #include<cstring> 3 #include<cmath> 4 #include<algorithm> 5 using namespace std; 6 #define MAXN 222

poj2774 后缀数组2个字符串的最长公共子串

Long Long Message Time Limit: 4000MS   Memory Limit: 131072K Total Submissions: 26601   Accepted: 10816 Case 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

poj2774 Long Long Message(后缀数组or后缀自动机)

转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Long Long Message Time Limit: 4000MS   Memory Limit: 131072K Case Time Limit: 1000MS Description The little cat is majoring in physics in the capital of Byterland. A piece of sad news comes t