POJ3080 Blue Jeans 【KMP 暴力水过】

题目描述

求n个字符串的最长公共序列,若长度相同,输出字典序最小的。若长度小于3,输出no significant commonalities

Sample Input

3

2

GATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATA

AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA

3

GATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATA

GATACTAGATACTAGATACTAGATACTAAAGGAAAGGGAAAAGGGGAAAAAGGGGGAAAA

GATACCAGATACCAGATACCAGATACCAAAGGAAAGGGAAAAGGGGAAAAAGGGGGAAAA

3

CATCATCATCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC

ACATCATCATAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA

AACATCATCATTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT

Sample Output

no significant commonalities

AGATAC

CATCATCAT

解题思路

暴力0ms可过....用ans储存当前最优解。

下面是代码

#include <cstdio>
#include <cstring>
const int maxn = 61;
int n;
int anslen = 0;
char ans[maxn];
char s[4010][maxn];
char p[maxn];//模式串
int main()
{
    int t;
    scanf("%d",&t);
    while(t--) {
        scanf("%d",&n);
        anslen = 0;
        for(int i = 0 ; i < n ; i ++) scanf("%s",s[i]);
        int len1 = strlen(s[0]);
        for(int i = 0 ; i < len1 ; i ++) {
            for(int j = i ; j < len1 ; j ++) {
                /** 模式串是s[0][i,i+1,...,j] */
                for(int k = i ; k <= j ; k ++) {
                    p[k-i] = s[0][k];
                }
                p[j-i+1] = '\0';
                /** 模式串已就绪 */
                bool flag = true;//记录其他主串是否都有p串
                int len = j-i+1;//长度
                if(len < anslen) continue;
                for(int k = 1 ; k < n ; k ++) {
                    if(strstr(s[k],p) == NULL) {
                        flag = false;
                        break;
                    }
                }
                if(flag) {
                    if(len == anslen) {//看字典序
                        if(strcmp(p,ans) < 0) {
                            strcpy(ans,p);
                        }
                    }else {
                        strcpy(ans,p);
                        anslen = len;
                    }
                }
            }
        }
        if(anslen < 3) printf("no significant commonalities\n");
        else printf("%s\n",ans);
    }
    return 0;
}
时间: 2024-10-04 00:15:05

POJ3080 Blue Jeans 【KMP 暴力水过】的相关文章

poj 3080 Blue Jeans (kmp暴力)

# include <stdio.h> # include <algorithm> # include <cstring> using namespace std; int next[100]; char pat[100]; char a[100][100]; int ma; int lenp; int n; void Getnext() { int i=0,j=-1; next[0]=-1; while(i<=lenp) { if(j==-1||pat[j]==

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

Blue Jeans DescriptionThe Genographic Project is a research partnership between IBM and The National Geographic Society that is analyzing DNA from hundreds of thousands of contributors to map how the Earth was populated. As an IBM researcher, you hav

POJ 3080 Blue Jeans KMP解法

使用KMP寻找最长的前缀的方法,比一般的暴力法有快了很多. 本题一般的暴力法需要的是O(m*n*n*n),其中m是有多少字符串,而n是字符串长度,而使用KMP就可以把时间效率提高到O(m*n*n),减少了一个n,提高了一个档次啦. 速度快很多. 准确来说应该是利用KMP寻找一个字符串A,在另一个字符串B任意位置出现的A的最长的前缀字符串. 理解好KMP的next table就好办了.每次查找到相等字符的时候,保存好最长的前缀. 注意本题的条件:选取最前的字典顺序输出.老害我错的条件. #incl

POJ 3080 Blue Jeans(KMP 最长公共子串)

Blue Jeans Description The Genographic Project is a research partnership between IBM and The National Geographic Society that is analyzing DNA from hundreds of thousands of contributors to map how the Earth was populated. As an IBM researcher, you ha

poj3080 Blue Jeans【KMP】【暴力】

Blue Jeans Time Limit: 1000MS   Memory Limit: 65536K Total Submissions:21746   Accepted: 9653 Description The Genographic Project is a research partnership between IBM and The National Geographic Society that is analyzing DNA from hundreds of thousan

POJ3080 Blue Jeans

Blue Jeans Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16142   Accepted: 7189 Description The Genographic Project is a research partnership between IBM and The National Geographic Society that is analyzing DNA from hundreds of thousa

kuangbin专题十六 KMP&amp;&amp;扩展KMP POJ3080 Blue Jeans

The Genographic Project is a research partnership between IBM and The National Geographic Society that is analyzing DNA from hundreds of thousands of contributors to map how the Earth was populated. As an IBM researcher, you have been tasked with wri

poj3080 Blue Jeans(暴枚+kmp)

Description The Genographic Project is a research partnership between IBM and The National Geographic Society that is analyzing DNA from hundreds of thousands of contributors to map how the Earth was populated.  As an IBM researcher, you have been ta

Blue Jeans---poj3080(kmp+暴力求子串)

题目链接:http://poj.org/problem?id=3080 题意就是求n个长度为60的串中求最长公共子序列(长度>=3):如果有多个输出字典序最小的: 我们可以暴力求出第一个串的所有子串,然后判断是否是其他的子串即可: #include<iostream> #include<stdio.h> #include<string.h> using namespace std; const int N = 107; char s[N][61]; int Nex