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 Next[N];

void GetNext(char a[], int n)
{
    int i=0,j=-1;
    Next[0]=-1;
    while(i<n)
    {
        if(j==-1 || a[i]==a[j])
            Next[++i] = ++j;
        else
            j=Next[j];
    }
}

int kmp(char a[], char b[], int n)
{
    int i=0, j=0;
    while(i<60)
    {

        if(j==-1 || b[i] == a[j])
            i++, j++;
        else
            j = Next[j];
        if(j==n)
            return true;
    }
    return false;
}

int main()
{
    int T, n,f;
    scanf("%d", &T);
    while(T--)
    {
        char ans[N]="Z";
        scanf("%d", &n);
        for(int i=0; i<n; i++)
            scanf("%s", s[i]);
        for(int len=60; len>=3; len--)///子串的长度;
        {
            for(int i=0; i<=60-len; i++)///子串的开始下标;
            {
                char subStr[N]= {0};
                strncpy(subStr, s[0]+i, len);

                GetNext(subStr, len);///从subStr中得到Next;
                int flag = 0;
                for(int j=1; j<n; j++)
                {
                    if(!kmp(subStr, s[j], len))
                    {
                        flag=1;
                        break;
                    }
                }
                if(flag == 0 && strcmp(ans, subStr)>0)
                    strcpy(ans, subStr);
            }
            f=0;
            if(ans[0]!=‘Z‘)
            {
                printf("%s\n", ans);
                f=1;
                break;
            }
        }
        if(f==0)
            printf("no significant commonalities\n");

    }
    return 0;
}

时间: 2024-07-28 12:57:36

Blue Jeans---poj3080(kmp+暴力求子串)的相关文章

POJ 题目3080 Blue Jeans(KMP+暴力)

Blue Jeans Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 14303   Accepted: 6367 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求多个串公共子串

题意:给出1-10个长度为60的字符串,求出最长的公共子串(长度不能小于3),如果有多个一样长的,输出字典序最短的. 解法:想到kmp时,自己第一反应枚举第一个串的所有子串,在其他所有串中走一遍kmp,复杂度为10*60*60*60,但是发现只需枚举第一个串后缀就可以,每次枚举记录在所有串能走最远中走的最短的那个长度.这样复杂度就成了10*60*60,0ms AC. 代码: /**************************************************** * autho

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

POJ Blue Jeans [枚举+KMP]

传送门 F - Blue Jeans Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Description The Genographic Project is a research partnership between IBM and The National Geographic Society that is analyzing DNA from hun

POJ 3080--Blue Jeans【KMP &amp;&amp; 暴力枚举】

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

poj 3080 Blue Jeans (KMP)

http://poj.org/problem?id=3080 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 populate

poj 3080 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 tas

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