- 浏览: 37557 次
文章分类
- 全部博客 (41)
- 卧鸟个去 (2)
- Transform (2)
- Mathmatic (9)
- Plant-Tree (7)
- Data-Struct (12)
- Red-Black-Tree (1)
- Radix-Tree (1)
- Trie (2)
- String (4)
- BST (2)
- Amazing-Union-Find-Set (1)
- HDU (27)
- OJ (32)
- BFS (3)
- Pretty-Suffix-Array (2)
- POJ (6)
- Graceful-Segment-Tree (2)
- Geometry (6)
- Priority-Queue (2)
- Dynamic-Programing (1)
- DP (3)
- LCS (1)
- Convex-Hull (2)
- Triangulation (1)
- DFS (3)
- Combinatorial-Mathematics (2)
- Big-Number (1)
- Statistic (3)
- STL (1)
- Shortest-Path (3)
- ZOJ (1)
- Leftist-Tree (1)
- Prime (1)
- Binary-Index-Tree (1)
- (1)
- Stack (1)
- SPFA (0)
- CRT (1)
HDU 1403 Longest Common Substring .
Longest Common Substring
Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1453 Accepted Submission(s): 419
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 今次系第一次用倍增算法构建后缀数组,我写既呢个其实比较好理解,哈哈,因为我好老实甘对SA两个关键字排序鸟,无花巧野,好直接,不过自己写既效率真系不敢恭维,可能直接过头………………{= =}3898619 2011-05-02 09:27:06 Accepted 1403 1015MS 8056K 2800 B G++ 10SGetEternal{(。)(。)}! 3898664 2011-05-02 09:45:07 Accepted 1403 968MS 8060K 2901 B G++ 10SGetEternal{(。)(。)}! 3898667 2011-05-02 09:45:32 Accepted 1403 843MS 6844K 2901 B G++ 10SGetEternal{(。)(。)}! 3898750 2011-05-02 10:09:18 Accepted 1403 828MS 6852K 3481 B G++ 10SGetEternal{(。)(。)}! 3898973 2011-05-02 11:06:51 Accepted 1403 750MS 6964K 2441 B G++ 10SGetEternal{(。)(。)}! 3900666 2011-05-02 20:02:15 Accepted 1403 734MS 6900K 2827 B C++ 10SGetEternal{(。)(。)}! 3900874 2011-05-02 20:42:53 Accepted 1403 484MS 6900K 2496 B C++ 10SGetEternal{(。)(。)}! 3900895 2011-05-02 20:45:23 Accepted 1403 468MS 6900K 2258 B C++ 10SGetEternal{(。)(。)}! 3900928 2011-05-02 20:49:18 Accepted 1403 93MS 6756K 2329 B C++ 10SGetEternal{(。)(。)}! 93ms!!!!!感动到内牛满面!!!!!!!!!!{= =} 下面献上优化代码,终于可以做模版鸟可以。
#include<iostream.h> #include<stdio.h> #include<string.h> #include<math.h> #include<memory.h> #define MAXI 200011 int l, key1[MAXI], key2[MAXI], rank[MAXI], tsa[MAXI], sa[MAXI], h[MAXI], heigth[MAXI], hash[MAXI]; char buf[MAXI], tbuf[MAXI]; int *s1 = sa, *s2 = tsa; void CS(int *key) { int i, *t; memset(hash, 0, sizeof hash); for (i = 0; i < l; i++) hash[key[s1[i]]]++; for (i = 1; i < MAXI; i++) hash[i] += hash[i-1]; for (i = l - 1; i >= 0; i--) s2[--hash[key[s1[i]]]] = s1[i]; t = s1; s1 = s2; s2 = t; } void CSA(char *str) { int i, j; for (i = 0; i < l; i++) s1[i] = i, key1[i] = str[i]; CS(key1); for (rank[s1[0]] = 0, i = 1; i < l; i++) if (key1[s1[i]] == key1[s1[i - 1]]) rank[s1[i]] = rank[s1[i-1]]; else rank[s1[i]] = rank[s1[i - 1]] + 1; for (j = 1; j < l; j *= 2) { for (i = 0; i < l; i++) { key1[i] = rank[i]; if (i + j < l ) key2[i] = rank[i + j]; else key2[i] = 0; } CS(key2); CS(key1); for (rank[s1[0]] = 0, i = 1; i < l; i++) if (key1[s1[i]] == key1[s1[i - 1]] && key2[s1[i]] == key2[s1[i - 1]]) rank[s1[i]] = rank[s1[i-1]]; else rank[s1[i]] = rank[s1[i - 1]] + 1; } if (s1 != sa) memcpy(sa, tsa, sizeof tsa); } void CHA(char *str) { int i, j, k = 0; for (i = 0; i < l; heigth[rank[i++]] = k) for (k? k-- : 0, j = sa[rank[i] - 1]; str[i + k] == str[j + k]; k++); } void CHA1(char *str) { int i, j, k, add; for (i = 0; i < l; i++) { if (rank[i] == 0) { h[i] = 0; continue; } j = rank[i]; k = rank[i] - 1; if (i == 0 || h[i - 1] <= 1) add = 0; else add = h[i - 1] - 1; while (str[sa[j] + add] == str[sa[k] + add]) add++; h[i] = add; } for (i = 0; i < l; i++) heigth[rank[i]] = h[i]; } int main() { int l1, l2, T, i, j, k, p, maxi; double sum, ave; while (scanf("%s", tbuf) != EOF) { l1 = strlen(tbuf); for (l = i = 0; i < l1; i++) buf[l++] = tbuf[i]; buf[l++] = '#'; scanf("%s", tbuf); l2 = strlen(tbuf); for (i = 0; i < l2; i++) buf[l++] = tbuf[i]; buf[l++] = 0; CSA(buf); CHA(buf); for (maxi = 0, i = 1; i < l; i++) if ((sa[i] < l1 && sa[i - 1] > l1) || (sa[i - 1] < l1 && sa[i] > l1)) if (heigth[i] > maxi) maxi = heigth[i]; printf("%d\n", maxi); } return 0; }
#include<iostream> #include<string> using namespace std; #define MAXI 200011 char str[MAXI]; int *sa = new int [MAXI]; int *tsa = new int [MAXI]; int *rank = new int [MAXI]; int *k1 = new int [MAXI]; int *k2 = new int [MAXI]; int *t, l, hash[MAXI], height[MAXI]; void CSA() { int i, j, k, sigm = MAXI; for (i = 0; i < sigm; i++) hash[i] = 0; for (i = 0; i < l; i++) hash[k1[i] = str[i]]++; for (i = 1; i < sigm; i++) hash[i] += hash[i - 1]; for (i = l - 1; i >= 0; i--) sa[--hash[str[i]]] = i; for (j = 1; j < l; j <<= 1, sigm++) { for (k = 0, i = l - j; i < l; i++) tsa[k++] = i; for (i = 0; i < l; i++) if (sa[i] >= j) tsa[k++] = sa[i] - j; for (i = 0; i < l; i++) k2[i] = k1[sa[i]]; for (swap(sa, tsa), i = 0; i < sigm; i++) hash[i] = 0; for (i = 0; i < l; i++) hash[k1[sa[i]]]++; for (i = 1; i < sigm; i++) hash[i] += hash[i - 1]; for (i = l - 1; i >= 0; i--) tsa[--hash[k1[sa[i]]]] = sa[i]; swap(sa, tsa); swap(k1, k2); for (sigm = k1[sa[0]] = 0, i = 1; i < l; i++) if (k2[sa[i]] == k2[sa[i - 1]] && k2[sa[i] + j] == k2[sa[i - 1] + j]) k1[sa[i]] = sigm; else k1[sa[i]] = ++sigm; } for (i = 0; i < l; i++) rank[sa[i]] = i; } void CHA() { int i, add; for (i = 0; i < l; i++) if (rank[i] == 0) height[rank[i]] = 0; else { if (i == 0 || height[rank[i - 1]] <= 1) add = 0; else add = height[rank[i - 1]] - 1; while (str[sa[rank[i]] + add] == str[sa[rank[i] - 1] + add]) add++; height[rank[i]] = add; } } int main() { int i, l1, l2, maxi; char tbuf[MAXI]; while (scanf("%s", tbuf) != EOF) { l1 = strlen(tbuf); for (i = l = 0; i < l1; i++) str[l++] = tbuf[i]; str[l++] = '#'; scanf("%s", tbuf); l2 = strlen(tbuf); for (i = 0; i < l2; i++) str[l++] = tbuf[i]; str[l++] = 0; CSA(), CHA(); for (maxi = 0, i = 1; i < l; i++) if ((sa[i] < l1 && sa[i - 1] > l1) || (sa[i - 1] < l1 && sa[i] > l1)) if (height[i] > maxi) maxi = height[i]; printf("%d\n", maxi); } return 0; }
发表评论
-
HDU 1370 Biorhythms
2011-08-03 10:27 1191Biorhythms Time Limit: 2000/10 ... -
HDU 1075 What Are You Talking About
2011-08-04 11:00 866What Are You Talking About Tim ... -
HDU 1058 Humble Numbers
2011-08-02 15:55 1222Humble Numbers Time Limit: 200 ... -
HDU 2095 find your present (2)
2011-08-02 16:13 817find your present (2) Time Lim ... -
HDU 1022 Train Problem I
2011-08-02 21:00 1013Train Problem I Time Limit: 20 ... -
2142 HDU box
2011-08-02 21:21 763box Time Limit: 3000/1000 MS ( ... -
HDU 2151 Worm
2011-08-01 20:48 849Worm Time Limit: 1000/1000 MS ... -
HDU 2722 Here We Go(relians) Again
2011-08-02 00:06 1026Here We Go(relians) Again Time ... -
HDU 3791 二叉搜索树
2011-08-02 14:26 1207二叉搜索树 Time Limit: 20 ... -
PKU 2352 Stars
2011-07-31 21:47 1026Stars Time Limit: 1000MS ... -
PKU 2774 Long Long Message
2011-07-31 21:26 902Long Long Message Time Li ... -
PKU 2777 Count Color
2011-07-31 21:31 795Count Color Time Limit: 1 ... -
HDU 2098 分拆素数和
2011-07-31 21:08 1062分拆素数和 Time Limit: 1000/1000 MS ... -
ZOJ 3512 Financial Fraud .
2011-07-31 20:49 1283Financial Fraud Time Limit: 3 ... -
HDU 1798 Tell me the area .
2011-07-31 20:47 1124Tell me the area Time Limit: 3 ... -
HDU 2962 Trucking .
2011-07-31 20:46 684Trucking Time Limit: 20000/100 ... -
HDU 1596 find the safest road .
2011-07-31 20:45 605find the safest road Time Limi ... -
HDU 2553 N皇后问题 .
2011-07-31 20:20 703N皇后问题 Time Limit: 2000/1000 MS ... -
HDU 1392 Surround the Trees .
2011-07-31 20:19 795Surround the Trees Time Limit: ... -
HDU 1234 开门人和关门人 .
2011-07-31 20:17 674开门人和关门人 Time Limit: 2000/1000 ...
相关推荐
杭电操作系统实验 HDU操作系统实验.zip杭电操作系统实验 HDU操作系统实验.zip杭电操作系统实验 HDU操作系统实验.zip杭电操作系统实验 HDU操作系统实验.zip杭电操作系统实验 HDU操作系统实验.zip杭电操作系统实验 HDU...
HDU操作系统实验.zip大学期间操作系统实验-HDU操作系统实验.zip大学期间操作系统实验-HDU操作系统实验.zip大学期间操作系统实验-HDU操作系统实验.zip大学期间操作系统实验-HDU操作系统实验.zip大学期间操作系统实验-...
【标题】"hdu_acm_1084.rar_ACM_HDU10_acm10_hdu_hdu 1084" 提供的是一个关于杭电(HDU)ACM竞赛第1084题的解决方案。该题目可能是在编程竞赛中常见的算法问题,而ACM(国际大学生程序设计竞赛)是全球知名的编程...
【HDU-GO v19.1225.2.zip】是一个针对杭州电子科技大学(HDU)选课系统的浏览器插件,版本号为v19.1225.2。这个插件的主要功能是优化和提升学生在进行网络选课时的体验,它可能包含了增强界面、自动化操作、数据解析...
【标题】"HDU-ACM_java.rar" 是一个针对杭州电子科技大学(HDU)ACM竞赛的资源压缩包,其中包含的是使用Java语言编写的算法解决方案。这个压缩包主要面向那些参与或准备参与ACM国际大学生程序设计竞赛(ICPC)的参赛...
【标题】"HDU-2000-2099.zip_hdu2000" 是一个包含杭电(Hangzhou Dianzi University)ACM竞赛题目解题报告的压缩包,覆盖了编号从2000到2099的题目。这个资源对于学习算法、提高编程技巧以及准备ACM/ICPC(国际大学...
HDU(杭州电子科技大学在线评测系统)是一个知名的编程竞赛平台,为编程爱好者提供了大量的算法题目进行练习和比赛。这个名为"HDU-2000-2099.rar_hdu"的压缩包包含了该平台从2000到2099共100道题目的源代码。这些...
个人数据库原理课程设计的期末作业留档_HDU-suspermarket_sys
ACM HDU 题目分类 ACM HDU 题目分类是指对 HDU 在线判题系统中题目的分类,总结了大约十来个分类。这些分类将有助于编程选手更好地理解和解决问题。 DP 问题 DP(Dynamic Programming,动态规划)是一种非常重要...
【标题】"hdu.rar_HDU 1089.cpp_OJ题求和_hdu_horsekw5_杭电obj" 提供的信息是关于一个压缩文件,其中包含了一个名为 "HDU 1089.cpp" 的源代码文件,这个文件是为了解决杭州电子科技大学(Hangzhou Dianzi ...
这个压缩文件包含的是作者个人提交并解决的ACM(国际大学生程序设计竞赛,International Collegiate Programming Contest)题目,这些题目来源于HDU的在线编程平台。 【描述】"杭电的一些acm题目,都是我自己一个一...
标题中的"hdu5102.zip_K."暗示这是一个与编程竞赛相关的题目,通常在HDU(杭州电子科技大学)在线判题系统中出现。这个题目可能是一个编程挑战,要求参赛者解决一个特定的问题,并提交源代码以供自动评判。"K."可能...
HDU-ACM课件.rar 是一个专门为编程竞赛爱好者准备的资源包,主要涵盖了ACM(国际大学生程序设计竞赛)中常见的算法知识。这个压缩包包含了一系列与算法相关的主题,旨在帮助初学者理解和掌握基础及进阶算法。下面将...