题目链接: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. 2019-9-2-win10-uwp-保存用户选择文件夹

    title author date CreateTime categories win10 uwp 保存用户选择文件夹 lindexi 2019-09-02 12:57:38 +0800 2018-2 ...

  2. CSS实现火焰效果

    代码如下 //主要就是用css动画实现的 <!DOCTYPE html> <html lang="en"> <head> <meta ch ...

  3. 【笔记】http1.1支持的7种请求方法

    本文是本人复习http协议整理笔记,以备后续查阅. http1.1支持的7种请求方法:get.post.head.options.put.delete.trace 在internet应用中,最常用的请 ...

  4. DirectX11笔记(十二)--Direct3D渲染8--EFFECTS

    原文:DirectX11笔记(十二)--Direct3D渲染8--EFFECTS 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/u010333737 ...

  5. 7 种 Javascript 常用设计模式学习笔记

    7 种 Javascript 常用设计模式学习笔记 由于 JS 或者前端的场景限制,并不是 23 种设计模式都常用. 有的是没有使用场景,有的模式使用场景非常少,所以只是列举 7 个常见的模式 本文的 ...

  6. linux守护进程配置文件

    守护进程是一种运行在非交互模式下的程序.一般来说,守护进程任务是和联网区域有关的:它们等待连接,以便通过连接提供服务.Linux 可以使用从 Web 服务器到 ftp 服务器的很多守护进程. /etc ...

  7. php中array_slice和array_splice函数解析方式方法

    array_slice array array_slice ( array $array , int $offset [, int $length = NULL [, bool $preserve_k ...

  8. MaxCompute 助力衣二三构建智能化运营工具

    摘要:本文由衣二三CTO程异丁为大家讲解了如何基于MaxCompute构建智能化运营工具.衣二三作为亚洲最大的共享时装平台,MaxCompute是如何帮助它解决数据提取速度慢.数据口径差异等问题呢?程 ...

  9. SurfaceFlinger与Surface概述

    基本原理: SF一个Client对应一个app中的SurfaceComposerClient, 分别是Binder的n端和b端,主要用来CreateSurface 一个app中有多个Activity, ...

  10. mysql存储过程小解

    mysql 存储过程 1.创建语法 delimiter $$ --$$表示改变默认的分隔符,代表以下为存储过程,不然会以SQL的方式执行 drop procedure if exists pro_na ...