题目链接: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. Angular.js的自定义功能

    1,自定义指令,在html中输入标签显示想要的指令 html script部分 2,标签中的属性的 有属性值时可以通过函数的参数返回属性值 没有属性值时可以设置属性值(自定义属性值) html部分   ...

  2. hdu 1296 Polynomial Problem(多项式模拟)

    Problem Description We have learned how to obtain the value of a polynomial when we were a middle sc ...

  3. 二分查找(BinSearch)的Javascript实现

    二分查找 解析:二分查找,也为折半查找.对于一个从小到大排列的有序数组,首先要找到一个中间值,通过与中间值比较,大的放又,小的放在左边.再在两边中寻找中间值,持续以上操作,直到找到所在位置为止. 1. ...

  4. Codeforces 113B

    题目链接 B. Petr# time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...

  5. python学习笔记08-- socket编程

    本节内容: 一.网络基础知识 二.socket概念及相关语法 2.1socket概念 2.2socket解释 2.3socket模块功能介绍 2.4socket粘包问题 2.5Socket多并发 一. ...

  6. NOIP模拟 17.8.14

    NOIP模拟17.8.14 (天宇哥哥考察细心程度的题) [样例解释]如果删去第一个 1:在[3,1,2]中有 3 个不同的数如果删去 3:在[1,1,2]中有 2 个不同的数如果删去第二个 1:在[ ...

  7. JS判断PC 手机端显示不同的内容

    方法一: function goPAGE() { if ((navigator.userAgent.match(/(phone|pad|pod|iPhone|iPod|ios|iPad|Android ...

  8. 在maven多模块结构中,并且使用overlay的情况下使用jetty热部署

    在使用maven多模块的结构的时候,同时有多个web工程使用maven-war-plugin的overlay来组织的时候,本地开发时如何在eclipse里面启动容器并且可以热部署调试是个比较麻烦的问题 ...

  9. JavaScript--阻止事件冒泡stopPropagation和cancelBubble

    <!DOCTYPE html> <html lang="en" xmlns="http://www.w3.org/1999/html"> ...

  10. 【JZOJ4762】【NOIP2016提高A组模拟9.7】千帆渡

    题目描述 输入 输出 样例输入 5 1 4 2 5 1 4 1 1 2 4 样例输出 2 1 4 数据范围 解法 设f[i][j]表示前 i个蓝色帆船中,选择了第 j个红色帆船作为结尾的最大答案. 那 ...