• POJ2774 Long Long Message —— 后缀数组 两字符串的最长公共子串


    题目链接:https://vjudge.net/problem/POJ-2774

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

    Source

    POJ Monthly--2006.03.26,Zeyuan Zhu,"Dedicate to my great beloved mother."

    题意:

    求两个字符串的最长公共子串。

    题解(后缀数组):

    1.将两个字符串拼接在一起,中间用特殊字符隔开。然后求后缀数组。

    2.枚举height数组:对于height[i],如果sa[i]、sa[i-1]分别位于两个字符串中,那么height[i]即为两个字符串的公共子串,求最大值即可。

    后缀数组

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <algorithm>
     5 #include <vector>
     6 #include <cmath>
     7 #include <queue>
     8 #include <stack>
     9 #include <map>
    10 #include <string>
    11 #include <set>
    12 using namespace std;
    13 typedef long long LL;
    14 const int INF = 2e9;
    15 const LL LNF = 9e18;
    16 const int MOD = 1e9+7;
    17 const int MAXN = 2e5+100;
    18 
    19 bool cmp(int *r, int a, int b, int l)
    20 {
    21     return r[a]==r[b] && r[a+l]==r[b+l];
    22 }
    23 
    24 int t1[MAXN], t2[MAXN], c[MAXN];
    25 void DA(int str[], int sa[], int Rank[], int height[], int n, int m)
    26 {
    27     n++;
    28     int i, j, p, *x = t1, *y = t2;
    29     for(i = 0; i<m; i++) c[i] = 0;
    30     for(i = 0; i<n; i++) c[x[i] = str[i]]++;
    31     for(i = 1; i<m; i++) c[i] += c[i-1];
    32     for(i = n-1; i>=0; i--) sa[--c[x[i]]] = i;
    33     for(j = 1; j<=n; j <<= 1)
    34     {
    35         p = 0;
    36         for(i = n-j; i<n; i++) y[p++] = i;
    37         for(i = 0; i<n; i++) if(sa[i]>=j) y[p++] = sa[i]-j;
    38 
    39         for(i = 0; i<m; i++) c[i] = 0;
    40         for(i = 0; i<n; i++) c[x[y[i]]]++;
    41         for(i = 1; i<m; i++) c[i] += c[i-1];
    42         for(i = n-1; i>=0; i--) sa[--c[x[y[i]]]] = y[i];
    43 
    44         swap(x, y);
    45         p = 1; x[sa[0]] = 0;
    46         for(i = 1; i<n; i++)
    47             x[sa[i]] = cmp(y, sa[i-1], sa[i], j)?p-1:p++;
    48 
    49         if(p>=n) break;
    50         m = p;
    51     }
    52 
    53     int k = 0;
    54     n--;
    55     for(i = 0; i<=n; i++) Rank[sa[i]] = i;
    56     for(i = 0; i<n; i++)
    57     {
    58         if(k) k--;
    59         j = sa[Rank[i]-1];
    60         while(str[i+k]==str[j+k]) k++;
    61         height[Rank[i]] = k;
    62     }
    63 }
    64 
    65 char a[MAXN], b[MAXN];
    66 int r[MAXN], sa[MAXN], Rank[MAXN], height[MAXN];
    67 int main()
    68 {
    69     while(scanf("%s", a)!=EOF)
    70     {
    71         scanf("%s", b);
    72         int lena = strlen(a);
    73         int lenb = strlen(b);
    74         int len = 0;
    75         for(int i = 0; i<lena; i++) r[len++] = a[i];
    76         r[len++] = 1;
    77         for(int i = 0; i<lenb; i++) r[len++] = b[i];
    78         r[len] = 0;
    79         DA(r, sa, Rank, height, len, 128);
    80 
    81         int ans = 0;
    82         for(int i = 2; i<=len; i++)
    83             if((sa[i-1]<=lena&&sa[i]>lena)||(sa[i-1]>lena&&sa[i]<=lena))
    84                 ans = max(ans, height[i]);
    85         printf("%d
    ", ans);
    86     }
    87     return 0;
    88 }
    View Code

    二分 + 字符串哈希:

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <algorithm>
     5 #include <vector>
     6 #include <cmath>
     7 #include <queue>
     8 #include <stack>
     9 #include <map>
    10 #include <string>
    11 #include <set>
    12 using namespace std;
    13 typedef long long LL;
    14 const int INF = 2e9;
    15 const LL LNF = 9e18;
    16 const int MOD = 1e9+7;
    17 const int MAXN = 1e5+100;
    18 
    19 char a[MAXN], b[MAXN];
    20 int n, m;
    21 LL Map[MAXN], bit[MAXN], seed = 13331;
    22 bool test(int k)
    23 {
    24     Map[0] = 0;
    25     for(int i = 0; i<k; i++)
    26         Map[0] *= seed, Map[0] += a[i];
    27     for(int i = k; i<n; i++)
    28         Map[i-k+1] = Map[i-k]*seed - 1LL*a[i-k]*bit[k] + 1LL*a[i];
    29     int cnt = n-k+1;
    30     sort(Map, Map+cnt);
    31 
    32     LL tmp = 0;
    33     for(int i = 0; i<k; i++)
    34         tmp *= seed, tmp += b[i];
    35     if(binary_search(Map,Map+cnt,tmp)) return true;
    36     for(int i = k; i<m; i++)
    37     {
    38         tmp = tmp*seed - 1LL*b[i-k]*bit[k] + 1LL*b[i];
    39         if(binary_search(Map,Map+cnt,tmp))
    40             return true;
    41     }
    42     return false;
    43 }
    44 
    45 int main()
    46 {
    47     scanf("%s%s", a, b);
    48     n = strlen(a);
    49     m = strlen(b);
    50 
    51     bit[0] = 1;
    52     for(int i = 1; i<n; i++)
    53         bit[i] = bit[i-1]*seed;
    54     int l = 0, r = min(n, m);
    55     while(l<=r)
    56     {
    57         int mid = (l+r)/2;
    58         if(test(mid))
    59             l = mid + 1;
    60         else
    61             r = mid - 1;
    62     }
    63     printf("%d
    ", r);
    64 }
    View Code
  • 相关阅读:
    strlen和sizeof
    函数值传递和地址传递
    指向函数的指针变量
    for循环scanf赋值刷新缓冲区
    指针
    排序简化
    随机数找到最大值
    上楼梯问题
    分布式系统并发情况下会生成多个token
    Swagger 文档生成工具
  • 原文地址:https://www.cnblogs.com/DOLFAMINGO/p/8469238.html
Copyright © 2020-2023  润新知