hdu 2328 Corporate Identity (KMP)
2014-03-19 22:56
423 查看
链接:http://acm.hdu.edu.cn/showproblem.php?pid=2328
求公共字串,暴力+KMP
代码:
#include <stdio.h>
#include <string.h>
#define INF 0x3f3f3f3f
const int N = 205;
char s[4005]
;
int next
, n, t;
void get_next(char *seq, int m) {
next[0] = -1;
int j = next[0];
for (int i = 1; i < m; i++) {
while (j >= 0 && seq[i] != seq[j + 1]) j = next[j];
if (seq[i] == seq[j + 1]) j++;
next[i] = j;
}
}
bool kmp(char *seq, char *seq1, int n, int m) {
int j = next[0], ans = 0;
for (int i = 0; i < n; i++) {
while (j >= 0 && seq[i] != seq1[j + 1]) j = next[j];
if (seq[i] == seq1[j + 1]) j++;
if (j == m - 1) {
return true;
}
}
return false;
}
bool judge(char *seq1) {
for (int i = 1; i < n; i++)
if (!kmp(s[i], seq1, strlen(s[i]), strlen(seq1)))
return false;
return true;
}
void solve() {
char ans
, s2
;
strcpy(ans, "IDENTITY LOST");
int len = strlen(s[0]);
for (int i = 0; i < len; i++) {
get_next(s[0] + i, len - i);
memset(s2, 0, sizeof(s2)); int s2n = 0;
for (int j = i; j < len; j++) {
s2[s2n++] = s[0][j];
if (judge(s2)) {
if (strcmp(ans, "IDENTITY LOST") == 0)
strcpy(ans, s2);
else if (strlen(ans) < strlen(s2))
strcpy(ans, s2);
else if (strlen(ans) == strlen(s2) && strcmp(ans, s2) > 0)
strcpy(ans, s2);
}
}
}
printf("%s\n", ans);
}
int main() {
while (~scanf("%d", &n) && n) {
for (int i = 0; i < n; i++)
scanf("%s", s[i]);
solve();
}
return 0;
}
求公共字串,暴力+KMP
代码:
#include <stdio.h>
#include <string.h>
#define INF 0x3f3f3f3f
const int N = 205;
char s[4005]
;
int next
, n, t;
void get_next(char *seq, int m) {
next[0] = -1;
int j = next[0];
for (int i = 1; i < m; i++) {
while (j >= 0 && seq[i] != seq[j + 1]) j = next[j];
if (seq[i] == seq[j + 1]) j++;
next[i] = j;
}
}
bool kmp(char *seq, char *seq1, int n, int m) {
int j = next[0], ans = 0;
for (int i = 0; i < n; i++) {
while (j >= 0 && seq[i] != seq1[j + 1]) j = next[j];
if (seq[i] == seq1[j + 1]) j++;
if (j == m - 1) {
return true;
}
}
return false;
}
bool judge(char *seq1) {
for (int i = 1; i < n; i++)
if (!kmp(s[i], seq1, strlen(s[i]), strlen(seq1)))
return false;
return true;
}
void solve() {
char ans
, s2
;
strcpy(ans, "IDENTITY LOST");
int len = strlen(s[0]);
for (int i = 0; i < len; i++) {
get_next(s[0] + i, len - i);
memset(s2, 0, sizeof(s2)); int s2n = 0;
for (int j = i; j < len; j++) {
s2[s2n++] = s[0][j];
if (judge(s2)) {
if (strcmp(ans, "IDENTITY LOST") == 0)
strcpy(ans, s2);
else if (strlen(ans) < strlen(s2))
strcpy(ans, s2);
else if (strlen(ans) == strlen(s2) && strcmp(ans, s2) > 0)
strcpy(ans, s2);
}
}
}
printf("%s\n", ans);
}
int main() {
while (~scanf("%d", &n) && n) {
for (int i = 0; i < n; i++)
scanf("%s", s[i]);
solve();
}
return 0;
}
相关文章推荐
- hdu 2328 Corporate Identity(KMP)
- 【kmp】Corporate Identity HDU - 2328
- HDU 2328 POJ 3450 KMP
- HDU 2328 Corporate Identity (暴力 | kmp)
- HDU 2328 Corporate Identity(发现新大陆之KMP专题不用KMP也能过系列)
- hdu_2328_Corporate Identity(暴力枚举子串+KMP)
- 【kmp循环节】hdu 3746 Cyclic Nacklace
- HDU 1358 Period 求周期串(kmp)
- HDU 2203(KMP) 亲和串
- hdu1358 Period KMP
- hdu4333之扩展KMP
- [HDU 2594]Simpsons’ Hidden Talents[kmp求公共前后缀]
- hdu-5918 Sequence I(kmp)
- HDU 1711 Number Sequence(KMP)
- hdu 1686(标准的kmp,可当模板)
- hdu4300之KMP&&EKMP
- hdu 2594 Simpsons’ Hidden Talents(kmp)
- HDU 2087 剪花布条(字符串匹配KMP)
- hdu 1686 KMP
- HDU 1867 A + B for you again KMP解决问题的方法