zoukankan      html  css  js  c++  java
  • SPOJ 220 Relevant Phrases of Annihilation(后缀数组)

    You are the King of Byteland. Your agents have just intercepted a batch of encrypted enemy messages concerning the date of the planned attack on your island. You immedietaly send for the Bytelandian Cryptographer, but he is currently busy eating popcorn and claims that he may only decrypt the most important part of the text (since the rest would be a waste of his time). You decide to select the fragment of the text which the enemy has strongly emphasised, evidently regarding it as the most important. So, you are looking for a fragment of text which appears in all the messages disjointly at least twice. Since you are not overfond of the cryptographer, try to make this fragment as long as possible.

    Input

    The first line of input contains a single positive integer t<=10, the number of test cases. t test cases follow. Each test case begins with integer n (n<=10), the number of messages. The next n lines contain the messages, consisting only of between 2 and 10000 characters 'a'-'z', possibly with some additional trailing white space which should be ignored.

    Output

    For each test case output the length of longest string which appears disjointly at least twice in all of the messages.

    题目大意:给n个字符串,求在每个字符串中出现至少两次且不重叠的最长子串的长度。

    思路:每个字符串用不相同的字符连起来。求后缀数组和height[]数组。

    二分长度L,检查长度。

    检查的时候从前往后扫一遍,看有没有连起来的一部分,公共前缀大于等于L且在每个字符串中都出现了且不重叠。

    复杂度为O(nlogn)

    代码(0.34s):

      1 #include <cstdio>
      2 #include <algorithm>
      3 #include <iostream>
      4 #include <cstring>
      5 using namespace std;
      6 
      7 const int MAXN = 110000;
      8 
      9 char s[MAXN];
     10 int id[MAXN];
     11 int sa[MAXN], rank[MAXN], height[MAXN], c[MAXN], tmp[MAXN];
     12 int n, m, T;
     13 
     14 void makesa(int m) {
     15     memset(c, 0, m * sizeof(int));
     16     for(int i = 0; i < n; ++i) ++c[rank[i] = s[i]];
     17     for(int i = 1; i < m; ++i) c[i] += c[i - 1];
     18     for(int i = 0; i < n; ++i) sa[--c[rank[i]]] = i;
     19     for(int k = 1; k < n; k <<= 1) {
     20         for(int i = 0; i < n; ++i) {
     21             int j = sa[i] - k;
     22             if(j < 0) j += n;
     23             tmp[c[rank[j]]++] = j;
     24         }
     25         int j = c[0] = sa[tmp[0]] = 0;
     26         for(int i = 1; i < n; ++i) {
     27             if(rank[tmp[i]] != rank[tmp[i - 1]] || rank[tmp[i] + k] != rank[tmp[i - 1] + k])
     28                 c[++j] = i;
     29             sa[tmp[i]] = j;
     30         }
     31         memcpy(rank, sa, n * sizeof(int));
     32         memcpy(sa, tmp, n * sizeof(int));
     33     }
     34 }
     35 
     36 void calheight() {
     37     for(int i = 0, k = 0; i < n; height[rank[i++]] = k) {
     38         k -= (k > 0);
     39         int j = sa[rank[i] - 1];
     40         while(s[i + k] == s[j + k]) ++k;
     41     }
     42 }
     43 
     44 int mx[MAXN], mn[MAXN];
     45 int stk[MAXN];
     46 
     47 void update_max(int &a, int b) {
     48     if(a == -1 || a < b) a = b;
     49 }
     50 
     51 void update_min(int &a, int b) {
     52     if(a == -1 || a > b) a = b;
     53 }
     54 
     55 bool check(int L) {
     56     int sum = 0, top = 0;
     57     memset(mx, -1, m * sizeof(int));
     58     memset(mn, -1, m * sizeof(int));
     59     memset(c, 0, m * sizeof(int));
     60     for(int i = 0; i < n; ++i) {
     61         if(height[i] >= L) {
     62             update_max(mx[id[sa[i]]], sa[i]);
     63             update_min(mn[id[sa[i]]], sa[i]);
     64             stk[++top] = id[sa[i]];
     65             if(mx[id[sa[i]]] - mn[id[sa[i]]] >= L) {
     66                 if(!c[id[sa[i]]]) ++sum;
     67                 c[id[sa[i]]] = true;
     68                 if(sum >= m) return true;
     69             }
     70         } else {
     71             sum = 0;
     72             while(top) {
     73                 int t = stk[top--];
     74                 mx[t] = mn[t] = -1;
     75                 c[t] = false;
     76             }
     77             update_max(mx[id[sa[i]]], sa[i]);
     78             update_min(mn[id[sa[i]]], sa[i]);
     79             stk[++top] = id[sa[i]];
     80         }
     81     }
     82     return false;
     83 }
     84 
     85 int solve() {
     86     int l = 1, r = 5001;
     87     while(l < r) {
     88         int mid = (l + r) >> 1;
     89         if(check(mid)) l = mid + 1;
     90         else r = mid;
     91     }
     92     return l - 1;
     93 }
     94 
     95 int main() {
     96     scanf("%d", &T);
     97     while(T--) {
     98         scanf("%d", &m);
     99         n = 0;
    100         for(int i = 0; i < m; ++i) {
    101             scanf("%s", s + n);
    102             while(s[n]) id[n++] = i;
    103             s[n++] = i + 1;
    104         }
    105         s[n - 1] = 0;
    106         makesa(128);
    107         calheight();
    108         printf("%d
    ", solve());
    109     }
    110 }
    View Code
  • 相关阅读:
    How to show query plan and runtime statistic in Derby
    How to load Additional Supplied Modules for Postgres on Windows
    一步一步学List Control控件的用法(第一步)创建第一列
    如何为Postgresql数据库全文搜索(full text search)编写解析器(parser)
    [转]MFC和CLR的区别
    一步一步学Edit Control控件的用法
    将接收到的IP地址转化为字符串显示(代码)
    一步一步学List Control控件的用法(第三步)设置风格
    Andrew NgML第九&十章神经网络
    PAT 1019 General Palindromic Number[简单]
  • 原文地址:https://www.cnblogs.com/oyking/p/3537871.html
Copyright © 2011-2022 走看看