题目链接:http://poj.org/problem?id=3080

题目大意:给你N个长度为60的字符串(N<=10),求他们的最长公共子串(长度>=3)。

题目分析:KMP字符串匹配基础题。直接枚举第1个字符串的所有子串,判断这个子串是否出现在另外N-1个串中。

实现代码如下:

#include <cstdio>
#include <string>
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
const int maxn = 66; int n, m, nxt[maxn];
string s, t; // s代表母串,t代表子串 int T, N;
string ss[11];
vector<string> tt; void cal_next() {
m = t.length();
for (int i = 0, j = -1; i < m; i ++) {
while (j != -1 && t[j+1] != t[i]) j = nxt[j];
nxt[i] = (j+1 < i && t[j+1] == t[i]) ? ++j : -1;
}
} bool check_s_has_t() {
n = s.length(); // cal_next();
for (int i = 0, j = -1; i < n; i ++) {
while (j != -1 && t[j+1] != s[i]) j = nxt[j];
if (t[j+1] == s[i]) {
j ++;
if (j >= m-1) {
return true;
}
}
}
return false;
} bool cmp(string s, string t) {
if (s.length() != t.length()) return s.length() > t.length();
return s < t;
} int main() {
cin >> T;
while (T --) {
cin >> N;
for (int i = 0; i < N; i ++) cin >> ss[i];
tt.clear();
for (int i = 0; i < 60; i ++) {
for (int j = 3; i+j <= 60; j ++) {
string tmp_s = ss[0].substr(i, j);
tt.push_back(tmp_s);
// cout << "tmp: " << tmp_s << endl;
}
}
sort(tt.begin(), tt.end(), cmp);
bool findOne = false;
for (int i = 0; i < tt.size(); i ++) {
t = tt[i];
cal_next();
bool flag = true;
for (int j = 1; j < N; j ++) {
s = ss[j];
if (check_s_has_t() == false) {
flag = false;
break;
}
}
if (flag == true) {
findOne = true;
break;
}
}
if (!findOne) puts("no significant commonalities");
else cout << t << endl;
}
return 0;
}

作者:zifeiy

POJ3080 Blue Jeans 题解 KMP算法的更多相关文章

  1. poj3080 Blue Jeans【KMP】【暴力】

    Blue Jeans Time Limit: 1000MS   Memory Limit: 65536K Total Submissions:21746   Accepted: 9653 Descri ...

  2. POJ3080 Blue Jeans —— 暴力枚举 + KMP / strstr()

    题目链接:https://vjudge.net/problem/POJ-3080 Blue Jeans Time Limit: 1000MS   Memory Limit: 65536K Total ...

  3. POJ Blue Jeans [枚举+KMP]

    传送门 F - Blue Jeans Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u ...

  4. POJ3080——Blue Jeans(暴力+字符串匹配)

    Blue Jeans DescriptionThe Genographic Project is a research partnership between IBM and The National ...

  5. poj3080 Blue Jeans(暴枚+kmp)

    Description The Genographic Project is a research partnership between IBM and The National Geographi ...

  6. POJ3080 - Blue Jeans(KMP+二分)

    题目大意 求N个字符串的最长公共字串 题解 和POJ1226做法一样...注意是字典序最小的...WA了一次 代码: #include <iostream> #include <cs ...

  7. kuangbin专题十六 KMP&&扩展KMP POJ3080 Blue Jeans

    The Genographic Project is a research partnership between IBM and The National Geographic Society th ...

  8. POJ 3080 Blue Jeans (KMP)

    求出公共子序列  要求最长  字典序最小 枚举第一串的所有子串   然后对每一个串做KMP.找到目标子串 学会了   strncpy函数的使用   我已可入灵魂 #include <iostre ...

  9. POJ - 3080 Blue Jeans 【KMP+暴力】(最大公共字串)

    <题目链接> 题目大意: 就是求k个长度为60的字符串的最长连续公共子串,2<=k<=10 限制条件: 1.  最长公共串长度小于3输出   no significant co ...

随机推荐

  1. POJ1991 NOI1999棋盘分割

    棋盘分割 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 15581   Accepted: 5534 Description ...

  2. Poj 1182种类(带权)并查集

    题目链接 食物链 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 44316 Accepted: 12934 Descriptio ...

  3. django1.11启动错误

    错误信息: 复制代码 Unhandled exception in thread started by <function check_errors..wrapper at 0x10f03b8c ...

  4. 2018-12-25-Roslyn-如何使用-MSBuild-Copy-复制文件

    title author date CreateTime categories Roslyn 如何使用 MSBuild Copy 复制文件 lindexi 2018-12-25 9:35:8 +080 ...

  5. Python接口自动化(一)接口基础

    HTTP接口熟悉 常见接口介绍 接口工具的使用 fiddler如何mock数据 常见接口基础面试 如何理解接口?前后端解耦,前端和后端数据对接桥梁 接口测试和功能测试区别在哪?接口测试是功能测试的一种 ...

  6. MySql实现Oracle的row_number()over(partition by ... order by ...)

    SELECT IF(t1.id = @id,@rownum := @rownum + 1,@rownum := 1)AS rownum, t1.*, @id := t1.id FROM (SELECT ...

  7. poj3463&&hdu1688 次短路(dijkstra)

    A*算法超内存. 对于最短路,我们可以维护dis[]数组,来求得最短路,但是此题有次短路,所以定义dis[][2],dis[][0]表示最短路,dis[][1]表示次短路;cnt[][2],cnt[] ...

  8. python 面向对象编程语言

  9. python三种导入模块的方法和区别

    方法一: import modname 模块是指一个可以交互使用,或者从另一Python 程序访问的代码段.只要导入了一个模块,就可以引用它的任何公共的函数.类或属性.模块可以通过这种方法来 使用其它 ...

  10. qbao

    # -*- coding: utf-8 -*- import Image, cStringIO, webbrowser, re, time, math import urllib, urllib2, ...