POJ-3080-Blue jeans(KMP, 暴力)
链接:
https://vjudge.net/problem/POJ-3080#author=alexandleo
题意:
给你一些字符串,让你找出最长的公共子串。
思路:
暴力枚举第一个串的子串,挨个匹配接下来的所有串.
找出最长子串中, 字典序最小的.
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
//#include <memory.h>
#include <queue>
#include <set>
#include <map>
#include <algorithm>
#include <math.h>
#include <stack>
#include <string>
#include <assert.h>
#include <iomanip>
#include <iostream>
#include <sstream>
#define MINF 0x3f3f3f3f
using namespace std;
typedef long long LL;
const int MAXN = 1e6+10;
int Next[MAXN];
string s[20];
int n;
void GetNext(string p)
{
int len = p.length();
Next[0] = -1;
int j = 0;
int k = -1;
while (j < len-1)
{
if (k == -1 || p[k] == p[j])
{
++k;
++j;
Next[j] = k;
}
else
k = Next[k];
}
}
bool Kmp(string ss, string p)
{
int lens = ss.length();
int lenp = p.length();
int i = 0, j = 0;
while (i < lens && j < lenp)
{
if (j == -1 || ss[i] == p[j])
{
i++;
j++;
}
else
j = Next[j];
}
return j == lenp;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int t;
cin >> t;
while (t--)
{
string res = "";
cin >> n;
for (int i = 1;i <= n;i++)
cin >> s[i];
int len = s[1].length();
for (int i = 0;i < len;i++)
{
for (int j = i;j < len;j++)
{
string tmp(s[1], i, j-i+1);
GetNext(tmp);
bool flag = true;
for (int k = 2;k <= n;k++)
{
if (!Kmp(s[k], tmp))
{
flag = false;
break;
}
}
if (flag)
{
if (tmp.length() > res.length())
res = tmp;
if (tmp.length() == res.length() && tmp < res)
res = tmp;
}
}
}
if (res.length() >= 3)
cout << res << endl;
else
cout << "no significant commonalities" << endl;
}
return 0;
}
POJ-3080-Blue jeans(KMP, 暴力)的更多相关文章
- POJ 3080 Blue Jeans(Java暴力)
Blue Jeans [题目链接]Blue Jeans [题目类型]Java暴力 &题意: 就是求k个长度为60的字符串的最长连续公共子串,2<=k<=10 规定: 1. 最长公共 ...
- POJ 3080 Blue Jeans (求最长公共字符串)
POJ 3080 Blue Jeans (求最长公共字符串) Description The Genographic Project is a research partnership between ...
- POJ 3080 Blue Jeans 找最长公共子串(暴力模拟+KMP匹配)
Blue Jeans Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20966 Accepted: 9279 Descr ...
- POJ 3080 Blue Jeans (字符串处理暴力枚举)
Blue Jeans Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 21078 Accepted: ...
- poj 3080 Blue Jeans
点击打开链接 Blue Jeans Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10243 Accepted: 434 ...
- poj 3080 Blue Jeans (暴力枚举子串+kmp)
Description The Genographic Project is a research partnership between IBM and The National Geographi ...
- POJ - 3080 Blue Jeans 【KMP+暴力】(最大公共字串)
<题目链接> 题目大意: 就是求k个长度为60的字符串的最长连续公共子串,2<=k<=10 限制条件: 1. 最长公共串长度小于3输出 no significant co ...
- POJ 3080 Blue Jeans、POJ 3461 Oulipo——KMP应用
题目:POJ3080 http://poj.org/problem?id=3080 题意:对于输入的文本串,输出最长的公共子串,如果长度相同,输出字典序最小的. 这题数据量很小,用暴力也是16ms,用 ...
- POJ 3080 Blue Jeans (KMP)
求出公共子序列 要求最长 字典序最小 枚举第一串的所有子串 然后对每一个串做KMP.找到目标子串 学会了 strncpy函数的使用 我已可入灵魂 #include <iostre ...
- POJ 3080 Blue Jeans (多个字符串的最长公共序列,暴力比较)
题意:给出m个字符串,找出其中的最长公共子序列,如果相同长度的有多个,输出按字母排序中的第一个. 思路:数据小,因此枚举第一个字符串的所有子字符串s,再一个个比较,是否为其它字符串的字串.判断是否为字 ...
随机推荐
- P1541 乌龟棋(动态规划)
(点击此处查看原题) 题意 此处有n个位置,记为1~n,每个位置上都对应一个权值,乌龟从编号为1的位置出发,利用m张爬行卡片到达位置n,爬行卡牌有四种,分别可以让乌龟移动1,2,3,4步,并保证将m张 ...
- TP5实现自定义抛出异常消息(关闭debug)
重写Handle的render方法,实现自定义异常消息----------------------------------------------------------------------- 首 ...
- 25-Perl CGI编程
1.Perl CGI编程什么是CGICGI 目前由NCSA维护,NCSA定义CGI如下:CGI(Common Gateway Interface),通用网关接口,它是一段程序,运行在服务器上如:HTT ...
- 执行sql语句,不依靠实体 获取string值
IList<string> list2 = Session.CreateSQLQuery(sql.ToString()).List<string>();
- JS基础_质数练习的改进,提高程序执行效率
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- warning LNK4076: 无效的增量状态文件“../×××.ilk”;正在非增量链接
VS编译警告:warning LNK4076: 无效的增量状态文件“../×××.ilk”;正在非增量链接 解决方法:删除程序提示的输出目录的×××.ilk,重新编译,即可
- centos7上的firewalld 的使用
#centos7上的firewalld 的使用 一.firewalld的基本启动关闭命令 启动服务------systemctl start firewalld 关闭服务------systemctl ...
- web登录的session、cookie和token
为什么会有登录这回事 首先这是因为HTTP是无状态的协议,所谓无状态就是在两次请求之间服务器并不会保存任何的数据,相当于你和一个人说一句话之后他就把你忘掉了.所以,登录就是用某种方法让服务器在多次请求 ...
- java.sql.SQLException: Could not retrieve transaction read-only status from server 问题解决
网上有2种主要说法 第一种 问题描述: java代码在开始事务后,先做了一个查询,再insert,此时会报: java.sql.SQLException: could not ret ...
- elementUI使用实录
新项目开发用到了elementUI,但是对这个虽然会用,但是细枝末节的东西每次都需要看官方文档才能想起来怎么用,故,记之. 1.form表单 -- 表单验证 在防止用户犯错的前提下,尽可能让用户更早地 ...