poj 3080 kmp求解多个字符串的最长公共字串,(数据小,有点小暴力 16ms)

Blue Jeans

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 14113   Accepted: 6260

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 tasked with writing a program
that will find commonalities amongst given snippets of DNA that can be
correlated with individual survey information to identify new genetic
markers.

A DNA base sequence is noted by listing the nitrogen bases in the
order in which they are found in the molecule. There are four bases:
adenine (A), thymine (T), guanine (G), and cytosine (C). A 6-base DNA
sequence could be represented as TAGACC.

Given a set of DNA base sequences, determine the longest series of bases that occurs in all of the sequences.

Input

Input
to this problem will begin with a line containing a single integer n
indicating the number of datasets. Each dataset consists of the
following components:

  • A single positive integer m (2 <= m <= 10) indicating the number of base sequences in this dataset.
  • m lines each containing a single base sequence consisting of 60 bases.

Output

For
each dataset in the input, output the longest base subsequence common
to all of the given base sequences. If the longest common subsequence is
less than three bases in length, display the string "no significant
commonalities" instead. If multiple subsequences of the same longest
length exist, output only the subsequence that comes first in
alphabetical order.

Sample Input

3
2
GATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
3
GATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATA
GATACTAGATACTAGATACTAGATACTAAAGGAAAGGGAAAAGGGGAAAAAGGGGGAAAA
GATACCAGATACCAGATACCAGATACCAAAGGAAAGGGAAAAGGGGAAAAAGGGGGAAAA
3
CATCATCATCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
ACATCATCATAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AACATCATCATTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT

Sample Output

no significant commonalities
AGATAC
CATCATCAT
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
char test[15][100];
int next[1000];
int len1;
char head[100];
 int n;
 int ma;
 char result[100];
void getnext(){
    memset(next,0,sizeof(next));
    int i=0,j=-1;
    next[0]=-1;
    while(i<len1){
        if(j==-1||head[i]==head[j]){
            i++;
            j++;
            next[i]=j;
        }
        else
            j=next[j];
    }
}

int  kmp(){
    ma=100;
    for(int ti=1;ti<n;ti++){
        int i=0,j=0,m=0;
        while(i<60){
            if(j==-1||test[ti][i]==head[j]){
                i++;
                j++;
            }
            else
                j=next[j];
            if(j>m)
                m=j;
        }
        if(m<ma)
            ma=m;

    }
    return ma;
}

int main(){
    int T;
    scanf("%d",&T);
    while(T--){
        memset(test,0,sizeof(test));
        memset(result,0,sizeof(result));
        scanf("%d",&n);
        getchar();
        for(int i=0;i<n;i++){
            cin>>test[i];
        }
        int ans=0;
        for(int i=0;i<=57;i++){
            memset(head,0,sizeof(head));
            len1=60-i;
            strcpy(head,test[0]+i);   ///  枚举第一个串的所有后缀串(当然最后2个可以省去)
            head[len1]=‘\0‘;
            getnext();
            int temp=kmp(); ///  KMP求出这个后缀串与其余所有串的最大匹配。
            if(temp>ans){
                ans=temp;
                strncpy(result,test[0]+i,ans);
            }
            else if(temp==ans){   ///  存在多个最长公共子串,输出字典序最小的,WA了一次。
                if(strcmp(test[0]+i,result)<0)
                    strncpy(result,test[0]+i,ans);   ///  复习: strncpy()没有复制最后的‘\0‘。
            }

        }
        if(ans<3)
            printf("no significant commonalities\n");
        else
            printf("%s\n",result);
    }
    return 0;
}
				
时间: 2024-10-21 21:43:16

poj 3080 kmp求解多个字符串的最长公共字串,(数据小,有点小暴力 16ms)的相关文章

(字符串)最长公共字串(Longest-Common-SubString,LCS)

题目: 给定两个字符串X,Y,求二者最长的公共子串,例如X=[aaaba],Y=[abaa].二者的最长公共子串为[aba],长度为3. 子序列是不要求连续的,字串必须是连续的. 思路: 1.简单思想: 遍历两个字符串X.Y,分别比较X的字串与Y的字串,求出最长的公共字串. #include <iostream> #include <vector> using namespace std; int getComLen(char *str1,char *str2){ int len=

求解两个字符串的最长公共子序列

一,问题描述 给定两个字符串,求解这两个字符串的最长公共子序列(Longest Common Sequence).比如字符串1:BDCABA:字符串2:ABCBDAB 则这两个字符串的最长公共子序列长度为4,最长公共子序列是:BCBA 二,算法求解 这是一个动态规划的题目.对于可用动态规划求解的问题,一般有两个特征:①最优子结构:②重叠子问题 ①最优子结构 设 X=(x1,x2,.....xn) 和 Y={y1,y2,.....ym} 是两个序列,将 X 和 Y 的最长公共子序列记为LCS(X,

POJ 2250 &amp; UVA 531 Compromise(字符串、 最长公共子序列)

Compromise 题目: 题目大意: 这里有两篇短文,每篇短文有若干个单词,求这两篇短文中的最长公共子序列,并将其输出来! 没篇短文输入 为 "#" 时,结束该篇短文的输入. 这道题是多组测试数据,如果只写一组,那么就会 WA,我因为这就被 WA 了一次! 最长公共子序列的解法,就不多说了,基本上所有的算法书上都有介绍讲解. 这道题,题意和解法我认为都不是难点,我个人认为难点是在最长公共子序列的保存记录上. 对于最长公共子序列的记录保存上,我用了 C++ 的 STL 中的strin

poj--3450 KMP求多个字符串的最长公共子串

思路与前面的3080一样 代码如下: #include<iostream> #include<cstdio> #include<cstring> using namespace std; char str1[220],str2[220]; int next[220],n; char ch[4400][220]; void GetNext() { int j=0; int len=strlen(str2+1); next[1]=0; for(int i=2;i<=l

POJ 2774 (后缀数组 最长公共字串) Long Long Message

用一个特殊字符将两个字符串连接起来,然后找最大的height,而且要求这两个相邻的后缀的第一个字符不能在同一个字符串中. 1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 using namespace std; 5 6 const int maxn = 200000 + 10; 7 8 char s[maxn]; 9 int n; 10 int sa[maxn], rank[maxn],

后缀数组(多个字符串的最长公共子串)—— POJ 3294

对应POJ 题目:点击打开链接 Life Forms Time Limit:6666MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Description Problem C: Life Forms You may have wondered why most extraterrestrial life forms resemble humans, differing by superficial tra

C++求解汉字字符串的最长公共子序列 动态规划

    近期,我在网上看了一些动态规划求字符串最长公共子序列的代码.可是无一例外都是处理英文字符串,当处理汉字字符串时.常常会出现乱码或者不对的情况. 我对代码进行了改动.使用wchar_t类型存储字符串,可以正确的处理英文字符串和汉字字符串的最长公共子序列. 代码例如以下: #include "stdafx.h" #include <iostream> #define N 1000 using namespace std; //str1存储字符串1,str2存储字符串2

POJ 3450 Corporate Identity 求所有字符的最长公共子串

Description Beside other services, ACM helps companies to clearly state their "corporate identity", which includes company logo but also other signs, like trademarks. One of such companies is Internet Building Masters (IBM), which has recently a

求两个字符串的最长公共子串——Java实现

要求:求两个字符串的最长公共子串,如"abcdefg"和"adefgwgeweg"的最长公共子串为"defg"(子串必须是连续的) public class Main03{ // 求解两个字符号的最长公共子串 public static String maxSubstring(String strOne, String strTwo){ // 参数检查 if(strOne==null || strTwo == null){ return null