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<=len;i++)
    {
        while(j>0&&str2[j+1]!=str2[i])
             j=next[j];
        if(str2[j+1]==str2[i])
             j++;
        next[i]=j;
    }
}

int KMP()
{
    GetNext();
    int j=0;
    int m=strlen(str2+1);
    int len=strlen(str1+1);
    for(int i=1;i<=len;i++)
    {
        while(j>0&&str2[j+1]!=str1[i])
            j=next[j];
        if(str2[j+1]==str1[i])
            j++;
        if(j==m)
            return 1;
    }
    return 0;
}

void solve(char *ans)
{
     int d=strlen(ch[1]);
     for(int i=0;i<d;i++)
     {
         int cnt=i;
         while(cnt<d)
         {
             int ii;
             for(int j=i;j<=cnt;j++)
                str2[j-i+1]=ch[1][j];
             str2[cnt+2-i]=‘\0‘;///记得最后一位加上字符串结束标志
             for(ii=2;ii<=n;ii++)
             {
                 strcpy(str1+1,ch[ii]);
                 if(KMP()==0)
                    break;
             }
             if(ii>n)
             {
                 if(strlen(str2+1)>strlen(ans+1))
                    strcpy(ans+1,str2+1);
                 else if (strlen(str2+1)==strlen(ans+1))
                 {
                    if(strcmp(ans+1,str2+1)>0)
                        strcpy(ans+1,str2+1);
                 }
                 cnt++;
             }
             else
                break;
         }
     }
     if(strlen(ans+1)==0)
         printf("IDENTITY LOST\n");
     else
         printf("%s\n",ans+1);
}

int main()
{
    while(scanf("%d",&n)&&n)
    {
        char ans[220];
        ///每次都必须要将ans数组清空,否则上次的答案会影响下次
        memset(ans,‘\0‘,sizeof(ans));
        for(int i=1;i<=n;i++)
            scanf("%s",ch[i]);
        solve(ans);
    }
  return 0;
}
时间: 2024-10-05 13:18:22

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

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

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

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 thousa

[URAL-1517][求两个字符串的最长公共子串]

Freedom of Choice URAL - 1517 Background Before Albanian people could bear with the freedom of speech (this story is fully described in the problem "Freedom of speech"), another freedom - the freedom of choice - came down on them. In the near fu

hdu1403---Longest Common Substring(后缀数组求2个字符串的最长公共子串)

Problem Description Given two strings, you have to tell the length of the Longest Common Substring of them. For example: str1 = banana str2 = cianaic So the Longest Common Substring is "ana", and the length is 3. Input The input contains several

【java】求两个字符串的最长公共子串

这个是华为OJ上的一道题目.首先,如果我们用java写代码,华为OJ有以下三条规则需遵守,否则编译无法通过或者用例无法通过,规则如下: (1)一定不可以有包名: (2)主类名只能为Main: (3)不可以输出与结果无关的信息. 好了,按照以上规则,我们写出来的代码如下(此代码不是最优的,只是用来记录华为OJ上java代码的书写规则): import java.util.Scanner; public class Main { public static void main(String[] ar

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

public static String findLongestOfTheSame(String s1,String s2) { char[] c1=s1.toCharArray(); char[] c2=s2.toCharArray(); int l1=c1.length; int l2=c2.length; int count=0,maxLength=0,start=0,end=0; boolean hasTheSame=false; for(int i=0;i<l1;i++) { coun

两个字符串的最长公共子串

import java.util.Scanner; /* 求两个字符串的最长公共子串*/ public class stringDemo {     public static void main(String[] args){      Scanner scanner = new Scanner(System.in);      System.out.println("请输入第一个字符串:");      String str1 = scanner.nextLine();     

后缀数组(多个字符串的最长公共子串)—— 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

求字符串的最长公共子串

找两个字符串的最长公共子串,这个子串要求在原字符串中是连续的.而最长公共子序列则并不要求连续. 代码如下: package string; import java.util.ArrayList; import java.util.List; public class Main { // 求最长公共子串长度 public int getMaxLen(String s1, String s2){ if(s1 == null || s2 == null){ return 0; } int m = s1