码迷,mamicode.com
首页 > 编程语言 > 详细

Long Long Message 后缀数组入门题

时间:2015-03-03 06:18:33      阅读:244      评论:0      收藏:0      [点我收藏+]

标签:

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

给你两串字符,要你找出在这两串字符中都出现过的最长子串

  1 #include <iostream>
  2 #include <cstdio>
  3 #include <cstring>
  4 #include <cmath>
  5 #include <algorithm>
  6 #include <string>
  7 #include <vector>
  8 #include <stack>
  9 #include <queue>
 10 #include <set>
 11 #include <map>
 12 #include <list>
 13 #include <iomanip>
 14 #include <cstdlib>
 15 using namespace std;
 16 const int INF=0x5fffffff;
 17 const double EXP=1e-8;
 18 const int MS=200005;
 19 //   KMP  TRIE  DFA  SUFFIX
 20 int dp[MS][20];        //    RMQ
 21 int t1[MS],t2[MS],c[MS],v[MS];
 22 int rank[MS],sa[MS],height[MS];
 23 char str[MS],str1[MS];
 24 int s[MS];
 25 int cmp(int *r,int a,int b,int k)
 26 {
 27     return r[a]==r[b]&&r[a+k]==r[b+k];
 28 }
 29 
 30 void get_sa(int *r,int *sa,int n,int m)
 31 {
 32     int i,j,p,*x=t1,*y=t2;
 33     for(i=0;i<m;i++)
 34         c[i]=0;
 35     for(i=0;i<n;i++)
 36         c[x[i]=r[i]]++;
 37     for(i=1;i<m;i++)
 38         c[i]+=c[i-1];
 39     for(i=n-1;i>=0;i--)
 40         sa[--c[x[i]]]=i;
 41     p=1;j=1;
 42     for(;p<n;j*=2,m=p)
 43     {
 44         for(p=0,i=n-j;i<n;i++)
 45             y[p++]=i;
 46         for(i=0;i<n;i++)
 47             if(sa[i]>=j)
 48                 y[p++]=sa[i]-j;
 49         for(i=0;i<n;i++)
 50             v[i]=x[y[i]];
 51         for(i=0;i<m;i++)
 52             c[i]=0;
 53         for(i=0;i<n;i++)
 54             c[v[i]]++;
 55         for(i=1;i<m;i++)
 56             c[i]+=c[i-1];
 57         for(i=n-1;i>=0;i--)
 58             sa[--c[v[i]]]=y[i];
 59         swap(x,y);
 60         x[sa[0]]=0;
 61         for(p=1,i=1;i<n;i++)
 62             x[sa[i]]=cmp(y,sa[i-1],sa[i],j)?p-1:p++;
 63     }
 64 }
 65 
 66 void get_height(int *r,int n)
 67 {
 68     int i,j,k=0;
 69     for(i=1;i<=n;i++)
 70         rank[sa[i]]=i;
 71     //height[i]>=height[i-1]-1;
 72     for(i=0;i<n;i++)
 73     {
 74         if(k)
 75             k--;
 76         else
 77             k=0;
 78         j=sa[rank[i]-1];
 79         while(r[i+k]==r[j+k])
 80             k++;
 81         height[rank[i]]=k;
 82     }
 83 }
 84 
 85 int main()
 86 {
 87     scanf("%s%s",str,str1);
 88     int n=0,len=strlen(str);
 89     for(int i=0;i<len;i++)
 90         s[n++]=str[i]-a+1;
 91     s[n++]=28;   //  分隔符
 92     len=strlen(str1);
 93     for(int i=0;i<len;i++)
 94         s[n++]=str1[i]-a+1;
 95     s[n]=0;   //  为了方便比较
 96     get_sa(s,sa,n+1,30);
 97     get_height(s,n);
 98     int maxv=0;
 99     len=strlen(str);
100     for(int i=2;i<n;i++)
101     {
102         if(height[i]>maxv)
103         {
104             if(0<=sa[i-1]&&sa[i-1]<len&&len<sa[i])
105                 maxv=height[i];
106             if(0<=sa[i]&&sa[i]<len&&len<sa[i-1])
107                 maxv=height[i];
108         }
109     }
110     printf("%d\n",maxv);
111     return 0;
112 }

 

Long Long Message 后缀数组入门题

标签:

原文地址:http://www.cnblogs.com/767355675hutaishi/p/4310063.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!