POJ3080-Blue Jeans(KMP,水)

大致题意:

就是求k个长度为60的字符串的最长连续公共子串,2<=k<=10

规定:

1、  最长公共串长度小于3不输出

2、  若出现等长的最长的子串,则输出字典序最小的串

思路:和POJ-3450-Corporate Identity一样二分+枚举,但是直接暴力也0ms

//192 KB	0 ms	题目太水,我就把POJ3450的代码改了几句话
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
char str[4010][210];
char minstr[210],tmp[210];
int next[210];
int lens[4010];
int n;
void getnext()
{
    next[0]=-1;
    int i=0,j=-1;
    while(tmp[i]!=0){
        while(j>-1&&tmp[i]!=tmp[j])
            j=next[j];
        j++;
        i++;
        next[i]=j;
    }
}
bool KMP()
{
    getnext();
    int lenm=strlen(tmp);
    for(int k=1;k<=n;k++){
        int i=0,j=0;
        while(i<lens[k]&&j<lenm){
            if(j==-1||str[k][i]==tmp[j]){
                i++;
                j++;
            }
            else j=next[j];
        }
        if(j!=lenm) return false;
    }
    return true;
}
bool ok(int x)
{
    int len=strlen(minstr);
    for(int i=0;i<=len-x;i++){
        strncpy(tmp,minstr+i,x);
        tmp[x]='\0';
        if(KMP()) return true;
    }
    return false;
}
int main()
{
    int _;
    scanf("%d",&_);
    while(_--){
        scanf("%d",&n);
        int minn=(1<<30);
        for(int i=1;i<=n;i++) {
            scanf("%s",str[i]);
            lens[i]=strlen(str[i]);
            if(minn>lens[i]){
                minn=lens[i];
                strcpy(minstr,str[i]);
            }
        }
        int lb=0,ub=minn+1;
        while(ub-lb>1){
            int mid=(lb+ub)/2;
            if(ok(mid)) lb=mid;
            else ub=mid;
        }

        char ans[210],first=1;
        for(int i=0;lb>=3&&i<=minn-lb;i++){
            strncpy(tmp,minstr+i,lb);
            tmp[lb]='\0';
            if(KMP()){
                if(first) {
                    first=0;
                    strcpy(ans,tmp);
                }
                else {
                    if(strcmp(ans,tmp)>0)
                        strcpy(ans,tmp);
                }
            }
        }
        if(lb>=3) printf("%s\n",ans);
        else printf("no significant commonalities\n");
    }
    return 0;
}
时间: 2024-10-25 02:15:40

POJ3080-Blue Jeans(KMP,水)的相关文章

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 最长公共子串)

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

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]==

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

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

POJ3080 Blue Jeans 【KMP 暴力水过】

题目描述 求n个字符串的最长公共序列,若长度相同,输出字典序最小的.若长度小于3,输出no significant commonalities Sample Input 3 2 GATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATA AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA 3 GATACCAGATACCAGATACCAGATACCAGATACCAGA

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

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

POJ 3080 Blue Jeans KMP

题意:找出所有字符串中最长相同字串 解题思路:枚举KMP 解题代码: 1 // File Name: getnext.cpp 2 // Author: darkdream 3 // Created Time: 2014年09月09日 星期二 22时35分02秒 4 5 #include<vector> 6 #include<list> 7 #include<map> 8 #include<set> 9 #include<deque> 10 #in