首页 > 其他 > 详细

Hdu 1403(后缀数组)

时间:2014-08-22 12:15:16      阅读:397      评论:0      收藏:0      [点我收藏+]

题目链接

Longest Common Substring

Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 4077    Accepted Submission(s): 1544


Problem Description
Given two strings, you have to tell the length of the Longest Common Substring of them.

For example:
str1 = banana
str2 = cianaic

So the Longest Common Substring is "ana", and the length is 3.

 

Input
The input contains several test cases. Each test case contains two strings, each string will have at most 100000 characters. All the characters are in lower-case.

Process to the end of file.

 

Output
For each test case, you have to tell the length of the Longest Common Substring of them.

 

Sample Input
banana cianaic

 

Sample Output
3
题意:求两个字符串的最长公共字串。
思路:如果说要求一个字符串中至少出现两次的长度最大的字串,那么结果就是后缀数组中相邻的两个后缀的高度
的最大值。也就是max(lcp[i], lcp[i+1]). 因此可以将两个字符串用一个不会出现的字符连接起来(如$)
Accepted Code:
 
 1 /*************************************************************************
 2     > File Name: 1403.cpp
 3     > Author: Stomach_ache
 4     > Mail: sudaweitong@gmail.com
 5     > Created Time: 2014年08月22日 星期五 09时00分05秒
 6     > Propose: sa + lcp
 7  ************************************************************************/
 8 
 9 #include <cmath>
10 #include <string>
11 #include <cstdio>
12 #include <fstream>
13 #include <cstring>
14 #include <iostream>
15 #include <algorithm>
16 using namespace std;
17 /*Let‘s fight!!!*/
18 
19 const int MAX_N = 100005 << 1;
20 int n, k;
21 int sa[MAX_N], lcp[MAX_N], rank[MAX_N], tmp[MAX_N];
22 
23 bool compare_sa(int i, int j) {
24       if (rank[i] != rank[j]) return rank[i] < rank[j];
25      else {
26         int ri = i + k <= n ? rank[i + k] : -1;
27         int rj = j + k <= n ? rank[j + k] : -1;
28         return ri < rj;
29     }
30 }
31 
32 void construct_sa(const string &S, int *sa) {
33       n = S.length();
34 
35       for (int i = 0; i <= n; i++) {
36           sa[i] = i;
37           rank[i] = i < n ? S[i] : -1;    
38      }
39     
40      for (k = 1; k <= n; k *= 2) {
41           sort(sa, sa + n + 1, compare_sa);
42 
43         tmp[sa[0]] = 0;
44         for (int i = 1; i <= n; i++) {
45               tmp[sa[i]] = tmp[sa[i - 1]] + (compare_sa(sa[i - 1], sa[i]) ? 1 : 0);
46         }
47         for (int i = 0; i <= n; i++) rank[i] = tmp[i];
48     }
49 }
50 
51 void construct_lcp(const string &S, int *sa, int *lcp) {
52     int n = S.length();
53     for (int i = 0; i <= n; i++) rank[sa[i]] = i;
54 
55     int h = 0;
56     lcp[0] = 0;
57     for (int i = 0; i < n; i++) {
58           int j = sa[rank[i] - 1];
59 
60         if (h > 0) h--;
61         for (; j + h < n && i + h < n; h++) if (S[i + h] != S[j + h]) break;
62 
63         lcp[rank[i] - 1] = h;
64     }
65 }
66 
67 string S, T;
68 
69 void solve() {
70     int len1 = S.length();
71     S = S + $ + T;
72 
73     construct_sa(S, sa);
74     construct_lcp(S, sa, lcp);
75 
76     int ans = 0;
77     for (unsigned i = 0; i < S.length(); i++) {
78           if ((sa[i] < len1) != (sa[i + 1] < len1))
79               ans = max(ans, lcp[i]);
80     }
81     cout << ans << endl;
82 }
83 
84 int main(void) {
85     ios::sync_with_stdio(false);
86     while (cin >> S >> T) {
87           solve();
88     }
89     return 0;
90 }

 

Hdu 1403(后缀数组),布布扣,bubuko.com

Hdu 1403(后缀数组)

原文:http://www.cnblogs.com/Stomach-ache/p/3928852.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!