zoukankan      html  css  js  c++  java
  • 18.12.10 POJ 2774 Long Long Message(后缀数组)

    描述

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

    输入

    Two strings with lowercase letters on two of the input lines individually. Number of characters in each one will never exceed 100000.输出A single line with a single integer number – what is the maximum length of the original text written by the little cat.

    样例输入

    yeshowmuchiloveyoumydearmotherreallyicannotbelieveit
    yeaphowmuchiloveyoumydearmother

    样例输出

    27

    来源

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

     1 #include <iostream>
     2 #include <string.h>
     3 #include <algorithm>
     4 #include <stack>
     5 #include <string>
     6 #include <math.h>
     7 #include <queue>
     8 #include <stdio.h>
     9 #include <string.h>
    10 #include <vector>
    11 #include <fstream>
    12 #define maxn 200005
    13 #define inf 999999
    14 #define cha 127
    15 using namespace std;
    16 
    17 int n,allsize;
    18 int all[maxn][2], height[maxn], Rank[maxn],wa[maxn],wb[maxn],wv[maxn], Ws[maxn], sa[maxn];
    19 char line[maxn / 2];
    20 
    21 void buildHeight() {
    22     int i, j, k;
    23     for (int i = 0; i < allsize; i++)
    24         Rank[sa[i]] = i;
    25     for (i = k = 0; i < allsize; height[Rank[i++]] = k)
    26         for (k ? k-- : 0, j = sa[Rank[i] - 1];
    27             all[i + k][0] == all[j + k][0]; 
    28             k++
    29             );
    30 }
    31 
    32 void buildSa() {
    33     int i, j, p, *pm = wa, *k2sa = wb, *t, m = cha + n;
    34     for (i = 0; i < m; i++)Ws[i] = 0;
    35     for (i = 0; i < allsize; i++)Ws[pm[i] = all[i][0]]++;
    36     for (i = 1; i < m; i++)Ws[i] += Ws[i - 1];
    37     for (i = allsize - 1; i >= 0; i--)sa[--Ws[pm[i]]] = i;
    38     for (j = p = 1; p < allsize; j <<= 1, m = p) {
    39         for (p = 0, i = allsize - j; i < allsize; i++)k2sa[p++] = i;
    40         for (i = 0; i < allsize; i++)
    41             if (sa[i] >= j)k2sa[p++] = sa[i] - j;
    42         for (i = 0; i < m; i++)Ws[i] = 0;
    43         for (i = 0; i < allsize; i++)Ws[wv[i] = pm[k2sa[i]]]++;
    44         for (i = 1; i < m; i++)Ws[i] += Ws[i - 1];
    45         for (i = allsize - 1; i >= 0; i--)sa[--Ws[wv[i]]] = k2sa[i];
    46         for (t = pm, pm = k2sa, k2sa = t, pm[sa[0]] = 0, p = i = 1; i < allsize; i++) {
    47             int a = sa[i - 1], b = sa[i];
    48             if (k2sa[a] == k2sa[b] && k2sa[a + j] == k2sa[b + j])
    49                 pm[sa[i]] = p - 1;
    50             else
    51                 pm[sa[i]] = p++;
    52         }
    53     }
    54     return;
    55 }
    56 
    57 int findMax() {
    58     int max = 0;
    59     for (int i = 1; i < allsize; i++) {
    60         if (height[i] > max&&all[sa[i - 1]][1] != all[sa[i]][1])
    61             max = height[i];
    62     }
    63     return max;
    64 }
    65 
    66 void init() {
    67     for (int i = 1; i <= 2; i++) {
    68         scanf("%s", line);
    69         for (int j = 0; line[j]; j++) {
    70             all[allsize][1] = i;
    71             all[allsize++][0] = line[j];
    72         }
    73         all[allsize++][0] = '-';
    74     }
    75     all[allsize - 1][0] = 0;
    76     buildSa();
    77     buildHeight();
    78     printf("%d
    ", findMax());
    79 }
    80 
    81 int main()
    82 {
    83     init();
    84     return 0;
    85 }
    View Code

    比较水,就是注意要有分隔符,选取的两个名次相邻的后缀必须是不同串的

    注定失败的战争,也要拼尽全力去打赢它; 就算输,也要输得足够漂亮。
  • 相关阅读:
    Apollo(阿波罗)配置中心Java客户端使用指南使用指南
    文本编码范围
    Java也可以像python般range出连续集合
    TCP连接性能指标之TCP关闭过程(四次挥手)
    网络设备性能指标之pps
    Nacos 服务发现,注册及管理
    关于 Executor 和 ExecutorService
    NUMA 平台
    Java 11 新垃圾回收器 ZGC
    Java 13 新特性
  • 原文地址:https://www.cnblogs.com/yalphait/p/10097968.html
Copyright © 2011-2022 走看看