Spoj 220 relevant phrases of annihilation)

Source: Internet
Author: User

Address: http://www.spoj.com/problems/PHRASES/

Question: Give You n strings and find the longest common compound substring.

Analysis: It is similar to the longest common repeated substrings of two strings. It is also used to synthesize all strings into one string, construct an array of suffixes, and then the length of the binary string, if the difference between the upper and lower bounds of the substring of N is greater than or equal to the current length, all strings must satisfy the conditions described in the solution. Otherwise, no solution exists...

Code:

/** Head files */# include <cstdlib> # include <cctype> # include <cstring> # include <cstdio> # include <cmath> # include <algorithm> # include <vector> # include <string> # include <iostream> # include <sstream> # include <map> # include <set> # include <queue> # include <stack> # include <fstream> # include <numeric> # include <iomanip> # include <bitset> # include <list> # include <stdexcept> # include <functional> # include <utility> # In Clude <ctime> using namespace STD;/** some operate */# define Pb push_back # define MP make_pair # define rep (I, n) for (I = 0; I <(n); ++ I) # define upto (I, L, H) for (I = (l); I <= (h); ++ I) # define down (I, H, L) for (I = (h); I >= (l); -- I) # define mset (ARR, Val) memset (ARR, val, sizeof (ARR) # define max3 (a, B, c) max (A, max (B, c) # define max4 (A, B, C, D) max (max (a, B), max (c, d) # define min3 (a, B, c) min (A, min (B, c )) # define min4 (A, B, C, D) min (min (A, B), min (c, d)/** some const */# define n 222222 # define M 222222 # define PI ACOs (-1.0) # define oo 1111111111/** some alias */typedef long ll;/** global variables * // ** some template names, just push Ctrl + J to get it in * // manacher evaluate the longest response substring // a combination of the MTH smaller pqueue priority queue // combk nelement sequence and // pmatrix n maximum sub-matrix of points // suffixarray suffix array template <typename t, int Len> struct suffixarray {int STR [Len * 3], sa [Len * 3]; int rank [Len], Hei Ght [Len]; int ID [Len]; int Len; bool equal (int * STR, int A, int B) {return STR [a] = STR [B] & STR [A + 1] = STR [B + 1] & STR [A + 2] = STR [B + 2];} bool cmp3 (int * STR, int * NSTR, int A, int B) {If (STR [a]! = STR [B]) return STR [a] <STR [B]; If (STR [A + 1]! = STR [B + 1]) return STR [A + 1] <STR [B + 1]; return NSTR [A + B % 3] <NSTR [B + B % 3];} void radixsort (int * STR, int * Sa, int * res, int N, int m) {int I; rep (I, m) ID [I] = 0; rep (I, n) ++ ID [STR [SA [I]; rep (I, m) ID [I + 1] + = ID [I]; down (I, n-1, 0) res [-- id [STR [SA [I] = sa [I];} void DC3 (int * STR, int * Sa, int N, int m) {# define f (x) (X)/3 + (x) % 3 = 1? 0: One) # define g (x) <one? (X) * 3 + 1 :( (x)-One) * 3 + 2) int * NSTR = STR + N, * NSA = SA + N, * tmpa = rank, * tmpb = height; int I, J, K, Len = 0, num = 0, zero = 0, one = (n + 1)/3; rep (I, n) if (I % 3) tmpa [Len ++] = I; STR [N] = STR [n + 1] = 0; radixsort (STR + 2, tmpa, tmpb, len, m); radixsort (STR + 1, tmpb, tmpa, Len, m); radixsort (STR + 0, tmpa, tmpb, Len, M ); NSTR [F (tmpb [0])] = num ++; upto (I, 1, len-1) NSTR [F (tmpb [I])] = equal (STR, tmpb [I-1], tmpb [I])? Num-1: num + +; If (Num <Len) DC3 (NSTR, NSA, Len, num); else rep (I, Len) NSA [NSTR [I] = I; if (N % 3 = 1) tmpa [zero ++] = n-1; rep (I, Len) if (NSA [I] <one) tmpa [zero ++] = NSA [I] * 3; radixsort (STR, tmpa, tmpb, zero, m); rep (I, Len) tmpa [NSA [I] = g (NSA [I])] = I; I = J = 0; rep (k, n) if (j> = Len | (I <zero & cmp3 (STR, tmpa, tmpb [I], NSA [J]) sa [k] = tmpb [I ++]; else SA [k] = NSA [J ++];} void initsa (T * s, int N, int m) {int I, j, k = 0; STR [Len = N] = 0; rep (I, n) STR [I] = s [I]; DC3 (STR, sa, n + 1, m); rep (I, n) SA [I] = sa [I + 1]; rep (I, n) rank [SA [I] = I; rep (I, n) {If (k) -- k; If (rank [I]) for (j = sa [rank [I]-1]; STR [I + k] = STR [J + k]; ++ K); else K = 0; height [rank [I] = K ;}}; suffixarray <char, N> MSA; char s [N], TMP [N]; int P [N], low [22], high [22]; int N; bool OK (INT m) {int I = 0, J, K; while (I <MSA. len) {While (I <MSA. len & MSA. height [I] <m) + + I; j = I-1; upto (k, 1, n) low [k] = MSA. len, high [k] =-1; do {k = MSA. sa [J ++]; low [p [k] = min (low [p [K], k ); high [p [k] = max (high [p [k], k);} while (j <MSA. len & MSA. height [J]> = m); upto (k, 1, n) if (high [k]-low [k] <m) break; If (k> N) return 1; I = J;} return 0;} int main () {int I, J, K, M, T, ans; scanf ("% d ", & T); While (t --) {scanf ("% d", & N); M = 0; upto (I, 1, n) {scanf ("% s", TMP); k = strlen (TMP); rep (j, k) s [M + J] = TMP [J], P [M + J] = I; P [M + k] = 0; s [M + k] = I; M = m + k + 1;} m --; MSA. initsa (S, M, 256); ans = I = 0, j = m; while (I <= J) {M = (I + J)> 1; if (OK (M) ans = m, I = m + 1; else J = m-1;} printf ("% d \ n", ANS);} return 0 ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.