Match:Blue Jeans(POJ 3080)

题目大意:给你m串字符串,要你找最长的相同的连续字串
这题暴力kmp即可,注意要按字典序排序,同时,是len<3才输出no significant commonalities
#include <iostream>
#include <functional>
#include <algorithm>
#include <string.h>
#define MAX 60 using namespace std; typedef char* _String;
typedef int Position;
static char str[][],ans[];
static int _next[]; bool KmpSearch(_String, _String, const int);
void Get_Next(_String, const int);
void Search(const int); int main(void)//暴力枚举第一行
{
int case_sum, m;
//freopen("in.txt", "r", stdin);
scanf("%d", &case_sum); while (case_sum--)
{
scanf("%d", &m);
getchar(); for (int i = ; i < m; i++)
scanf("%s", str[i]);
Search(m);
}
return EXIT_SUCCESS;
} void Search(const int m)
{
int len, ans_len = -, pos, if_match;
char tmp; for (len = ; len <= MAX; len++)
{
for (pos = ; pos + len <= MAX; pos++)
{
if_match = ;
Get_Next(&str[][pos], len);
for (int i = ; i < m; i++)
if_match += KmpSearch(str[i], &str[][pos], len);
if (if_match == m - && len >= ans_len)
{
if (len == ans_len)
{
tmp = str[][pos + len];
str[][pos + len] = '\0';
if (strcmp(&str[][pos], ans) < )
strcpy(ans, &str[][pos]);
str[][pos + len] = tmp;
}
else if (len > ans_len)
{
ans_len = len;
tmp = str[][pos + len];
str[][pos + len] = '\0';
strcpy(ans, &str[][pos]);
str[][pos + len] = tmp;
}
}
}
}
if (ans_len < )
printf("no significant commonalities\n");
else
printf("%s\n", ans);
} bool KmpSearch(_String str_m, _String text, const int t_len)
{
Position i = , j = ; while (i < MAX && j < t_len)
{
if (j == - || str_m[i] == text[j])
{
i++;
j++;
}
else j = _next[j];
}
if (j == t_len)
return true;
else
return false;
} void Get_Next(_String text, const int t_len)
{
Position i = , k = -;
_next[] = -; while (i < t_len)
{
if (k == - || text[i] == text[k])
{
i++;
k++;
_next[i] = text[i] != text[k] ? k : _next[k];
}
else k = _next[k];
}
}

Match:Blue Jeans(POJ 3080)的更多相关文章
- (字符串 KMP)Blue Jeans -- POJ -- 3080:
链接: http://poj.org/problem?id=3080 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=88230#probl ...
- Blue Jeans - POJ 3080(多串的共同子串)
题目大意:有M个串,每个串的长度都是60,查找这M个串的最长公共子串(连续的),长度不能小于3,如果同等长度的有多个输出字典序最小的那个. 分析:因为串不多,而且比较短,所致直接暴力枚举的第一个串 ...
- Blue Jeans - poj 3080(后缀数组)
大致题意: 给出n个长度为60的DNA基因(A腺嘌呤 G鸟嘌呤 T胸腺嘧啶 C胞嘧啶)序列,求出他们的最长公共子序列 使用后缀数组解决 #include<stdio.h> #include ...
- Blue Jeans POJ 3080 寻找多个串的最长相同子串
Description The Genographic Project is a research partnership between IBM and The National Geographi ...
- POJ 3080 Blue Jeans (求最长公共字符串)
POJ 3080 Blue Jeans (求最长公共字符串) Description The Genographic Project is a research partnership between ...
- poj 3080 Blue Jeans
点击打开链接 Blue Jeans Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10243 Accepted: 434 ...
- POJ 3080 Blue Jeans (字符串处理暴力枚举)
Blue Jeans Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 21078 Accepted: ...
- POJ 3080 Blue Jeans(Java暴力)
Blue Jeans [题目链接]Blue Jeans [题目类型]Java暴力 &题意: 就是求k个长度为60的字符串的最长连续公共子串,2<=k<=10 规定: 1. 最长公共 ...
- POJ 3080 Blue Jeans 找最长公共子串(暴力模拟+KMP匹配)
Blue Jeans Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20966 Accepted: 9279 Descr ...
随机推荐
- SharePoint Server 2010 中的基本任务
SharePoint Foundation 和 SharePoint Server 概述 SharePoint Foundation 2010 是一项用于 SharePoint 网站的基础技术,它可以 ...
- ICloud没有密码怎么注销?
忘了密码的用这方法就可以啦1.现在我说你做 . 先进入 iCloud 点击 某某账户(要删的账户)2.第二栏密码那里删除原来的再输入ios(任意密码)3.然后点完成 之后会出现错误.请点 好.然后左上 ...
- 【PHP面向对象(OOP)编程入门教程】13.访问类型(public,protected,private)
类型的访问修饰符允许开发人员对类成员的访问进行限制,这是PHP5的新特性,但却是OOP语言的一个好的特性.而且大多数OOP语言都已支持此特性.PHP5支持如下3种访问修饰符: public (公有的. ...
- 1.2 从 ACID 到 CAP/BASE
1.事务 事务(Tranction)是指,由一系列对系统中数据进行访问与更新操作,所组成的一个逻辑执行单元.狭义上的事务是指数据库事务. 事务有四个特性. 原子性:原子性要求事务只允讲有两种状态,全部 ...
- Linux中获取本机网络信息的几个函数及应用
一.读取/etc/hosts 几个函数 头文件<netdb.h> 1.void sethostent(int stayopen);//开打/etc/hosts 配置文件 2.struct ...
- Codeforces 260 B. Fedya and Maths
题目链接:http://codeforces.com/contest/456/problem/B 解题报告:输入一个n,让你判断(1n + 2n + 3n + 4n) mod 5的结果是多少?注意n的 ...
- iOS开发——常见错误——使用MJRefresh返回上一个界面蹦掉的情况
最近在使用MJRefresh框架时发现了一个bug 下面是我的源代码 前一个界面 -(void)tableView:(UITableView *)tableView didSelectRowAtInd ...
- 架设 OpenLDAP服务器
OpenLDAP是一个开放源代码的软件,可以免费获取使用,其主页地址是:http://www.openldap.org/.在RHEL 6上安装OpenLDAP还需要libtool-ltdl-2.2.6 ...
- Android沉浸式任务栏的实现
1.MainActivity.java public class MainActivity extends Activity { @Override protected void onCreate(B ...
- 剑指Offer 变态跳台阶
题目描述 一只青蛙一次可以跳上1级台阶,也可以跳上2级……它也可以跳上n级.求该青蛙跳上一个n级的台阶总共有多少种跳法. 其实就是斐波那契数列问题. 假设f(n)是n个台阶跳的次数. f(1) = ...