hdu1841 Find the Shortest Common Superstring-----KMP

Find the Shortest Common Superstring

Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 971 Accepted Submission(s): 243

Problem Description

The shortest common superstring of 2 strings S1 and S2 is a string S with the minimum number of characters which contains both S1 and S2 as a sequence of consecutive characters. For instance, the shortest common superstring of “alba” and “bacau” is “albacau”.
Given two strings composed of lowercase English characters, find the length of their shortest common superstring.

Input

The first line of input contains an integer number T, representing the number of test cases to follow. Each test case consists of 2 lines. The first of these lines contains the string S1 and the second line contains the string S2. Both of these strings contain at least 1 and at most 1.000.000 characters.

Output

For each of the T test cases, in the order given in the input, print one line containing the length of the shortest common superstring.

Sample Input

2

alba

bacau

resita

mures

 

Sample Output

7

8

题意:求两字符串的最短和串。

首先我们设S1这个字符串比S2短,那么S2里包含S1是一种情况。

第二种情况:S1的尾覆盖S2的头

第三种情况:S2的尾覆盖S1的头

对于这两种情况,根据next函数的性质,我们只需要求出串C=S1S2的next[len1+len2],next[len1+len2]即为覆盖的长度,

同理求出C=S2S1的next[len1+len2]。笔记哦一下即可求得答案。

#include"stdio.h"
#include"string.h"
#include<iostream>
#include<cstdlib>
using namespace std;
#define M 1000001
int next[2*M];
char c[2*M];
char a[2][M];
void get_next(char *s,int len)
{
 int i,j;
 i=0;next[0]=-1;j=-1;
 while(i<len)
 {
  if(j==-1||s[i]==s[j])
  {
   i++;j++;
   next[i]=j;
  }
  else
  {
   j=next[j];
  }
 }
}
int index_KMP(char *s1,char *s2,int l1,int l2)
{
 int i,j;
 i=0;j=0;
 while(i<l2&&j<l1)
 {
  if(j==-1||s2[i]==s1[j])
  {
   i++;j++;
  }
  else
  {
   j=next[j];
  }
 }
 if(j>=l1)
  return l2;
 else
  return 0;
}
int main()
{
 int n,len1,len2,temp,min,mat;
 while(scanf("%d",&n)!=EOF)
 {
  getchar();
  while(n--)
  {
   gets(a[0]);
   gets(a[1]);
   len1=strlen(a[0]);
   len2=strlen(a[1]);
   if(len1>len2)
   {
    temp=len1;
    len1=len2;
    len2=temp;
    strcpy(c,a[0]);
    strcpy(a[0],a[1]);
    strcpy(a[1],c);
   }
   get_next(a[0],len1);
   min=index_KMP(a[0],a[1],len1,len2);
   if(min>0)
   {
    printf("%d\n",min);
   }
   else
   {
    strcpy(c,a[0]);
    strcat(c,a[1]);
    memset(next,0,sizeof(next));
    get_next(c,len1+len2);
    min=len1+len2-next[len1+len2];
    //cout<<next[len1+len2]<<"*"<<endl;
    strcpy(c,a[1]);
    strcat(c,a[0]);
    memset(next,0,sizeof(next));
    get_next(c,len1+len2);
    mat=len1+len2-next[len1+len2];
    // cout<<next[len1+len2]<<"*"<<endl;
    if(min<=mat)
    {
     printf("%d\n",min);
    }
    else
    {
     printf("%d\n",mat);
    }
   }
  }
 }
 return 0;
}


 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值