Codeforces 427d match & catch (suffix array, string)

Source: Internet
Author: User

Question

 

Reference: http://blog.csdn.net/xiefubao/article/details/24934617

 

Question: give two strings and find the shortest substring. So that the occurrence times of this substring in two strings are equal to 1. The occurrence is defined as overlapping.

 

Solution: Apply the suffix array. Small enumeration length. If a length Len is valid, the SA [I] ranking must exist. Sa [I] And s [I + 1] public prefix length is greater than or equal to Len, and SA [I] and [I-1] public prefix length is less than Len, at the same time, the common prefix lengths of SA [I + 1] and [I + 2] are smaller than Len, and both SA [I] and SA [I + 1] are ensured. The judge function skillfully implements these judgments.

 

 

// If a Len is valid, the SA [I] ranking must exist. // SA [I] And s [I + 1] public prefix length greater than or equal to Len, // and SA [I] and [I-1] public prefix length less than Len, // At the same time, the public prefix length of SA [I + 1] and [I + 2] is smaller than Len, // both SA [I] and SA [I + 1] are supported. # Include <stdio. h> # include <string. h >#include <algorithm> using namespace STD; # define maxn 1000001int wa [maxn], WB [maxn], WV [maxn], WS [maxn]; int CMP (int * r, int A, int B, int L) {return R [a] = R [B] & R [A + L] = R [B + L];} void da (int * r, int * Sa, int N, int m) {int I, j, P, * x = wa, * Y = WB, * t; for (I = 0; I <m; I ++) WS [I] = 0; for (I = 0; I <n; I ++) WS [x [I] = R [I] ++; for (I = 1; I <m; I ++) WS [I] + = ws [I-1]; for (I = n-1; I> = 0; I --) SA [-- ws [x [I] = I; (J = 1, P = 1; P <n; j * = 2, M = P) {for (P = 0, I = N-J; I <N; I ++) y [p ++] = I; for (I = 0; I <n; I ++) if (SA [I]> = J) Y [p ++] = sa [I]-J; for (I = 0; I <n; I ++) WV [I] = x [Y [I]; for (I = 0; I <m; I ++) WS [I] = 0; for (I = 0; I <n; I ++) WS [wv [I] ++; for (I = 1; I <m; I ++) WS [I] + = ws [I-1]; for (I = n-1; I> = 0; I --) sa [-- ws [wv [I] = Y [I]; for (t = x, x = Y, y = T, P = 1, X [SA [0] = 0, I = 1; I <n; I ++) x [SA [I] = CMP (Y, sa [I-1], sa [I], j )? P-1: P ++;} return;} int rankk [maxn], height [maxn]; void calheight (int * r, int * Sa, int N) {int I, j, k = 0; for (I = 1; I <= N; I ++) rankk [SA [I] = I; for (I = 0; I <n; height [rankk [I ++] = k) for (K? K --: 0, j = sa [rankk [I]-1]; R [I + k] = R [J + k]; k ++); return ;} char s [10010]; int len1, len2; int R [10010], sa [10010]; // This is a good method to remove the impact of subscript. Int judge (int n, int K) {int A, B; a = B = 0; For (INT I = 2; I <= N; I ++) {If (height [I] <k) {if (a = 1 & B = 1) return 1; a = 0, B = 0 ;} if (SA [I]> = 0 & SA [I] <len1) A ++; If (SA [I]> len1 & SA [I] <len2) B ++;} return 0;} int main () {scanf ("% s", S); len1 = strlen (s); s [len1] = 1; scanf ("% s", S + len1 + 1); len2 = strlen (s); For (INT I = 0; I <len2; I ++) R [I] = s [I]; R [len2] = 0; DA (R, SA, len2 + 1,128); calheight (R, SA, len2 ); int ans =-1; for (INT I = 1; I <= len1; I ++) {// enumeration length if (Judge (len2 + 1, I )) {ans = I; break;} printf ("% d \ n", ANS); Return 0 ;}
View code

 

Codeforces 427d match & catch (suffix array, string)

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.