hdu2594 Simpsons’ Hidden Talents LCS--扩展KMP

Homer: Marge, I just figured out a way to discover some of the talents we weren’t aware we had.
Marge: Yeah, what is it?
Homer: Take me for example. I want to find out if I have a talent in politics, OK?
Marge: OK.
Homer: So I take some politician’s name, say Clinton, and try to find the length of the longest prefix
in Clinton’s name that is a suffix in my name. That’s how close I am to being a politician like Clinton
Marge: Why on earth choose the longest prefix that is a suffix???
Homer: Well, our talents are deeply hidden within ourselves, Marge.
Marge: So how close are you?
Homer: 0!
Marge: I’m not surprised.
Homer: But you know, you must have some real math talent hidden deep in you.
Marge: How come?
Homer: Riemann and Marjorie gives 3!!!
Marge: Who the heck is Riemann?
Homer: Never mind.
Write a program that, when given strings s1 and s2, finds the longest prefix of s1 that is a suffix of s2.

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

扩展KMP裸题

 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<algorithm>
 4 using namespace std;
 5 
 6 const int maxn=5e4+5;
 7 
 8 char s[maxn],t[maxn];
 9 int nxt[maxn],ext[maxn];
10 
11 void EKMP(char s[],char t[],int lens,int lent){
12     int i,j,p,l,k;
13     nxt[0]=lent;j=0;
14     while(j+1<lent&&t[j]==t[j+1])j++;
15     nxt[1]=j;
16     k=1;
17     for(i=2;i<lent;i++){
18         p=nxt[k]+k-1;
19         l=nxt[i-k];
20         if(i+l<p+1)nxt[i]=l;
21         else{
22             j=max(0,p-i+1);
23             while(i+j<lent&&t[i+j]==t[j])j++;
24             nxt[i]=j;
25             k=i;
26         }
27     }
28 
29     j=0;
30     while(j<lens&&j<lent&&s[j]==t[j])j++;
31     ext[0]=j;k=0;
32     for(i=1;i<lens;i++){
33         p=ext[k]+k-1;
34         l=nxt[i-k];
35         if(l+i<p+1)ext[i]=l;
36         else{
37             j=max(0,p-i+1);
38             while(i+j<lens&&j<lent&&s[i+j]==t[j])j++;
39             ext[i]=j;
40             k=i;
41         }
42     }
43 }
44 
45 int main(){
46     while(scanf("%s%s",s,t)!=EOF){
47         EKMP(t,s,strlen(t),strlen(s));
48         int l=strlen(t);
49         int i;
50         for(i=0;i<l;++i){
51             if(ext[i]==l-i){
52                 printf("%s %d\n",t+i,l-i);
53                 break;
54             }
55         }
56         if(i==l)printf("0\n");
57     }
58     return 0;
59 }
View Code

 

转载于:https://www.cnblogs.com/cenariusxz/p/6592457.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
毕业设计,基于SpringBoot+Vue+MySQL开发的精简博客系统,源码+数据库+毕业论文+视频演示 当下,正处于信息化的时代,许多行业顺应时代的变化,结合使用计算机技术向数字化、信息化建设迈进。以前企业对于博客信息的管理和控制,采用人工登记的方式保存相关数据,这种以人力为主的管理模式已然落后。本人结合使用主流的程序开发技术,设计了一款基于Springboot开发的精简博客系统,可以较大地减少人力、财力的损耗,方便相关人员及时更新和保存信息。本系统主要使用B/S开发模式,在idea开发平台上,运用Java语言设计相关的系统功能模块,MySQL数据库管理相关的系统数据信息,SpringBoot框架设计和开发系统功能架构,最后通过使用Tomcat服务器,在浏览器中发布设计的系统,并且完成系统与数据库的交互工作。本文对系统的需求分析、可行性分析、技术支持、功能设计、数据库设计、功能测试等内容做了较为详细的介绍,并且在本文中也展示了系统主要的功能模块设计界面和操作界面,并对其做出了必要的解释说明,方便用户对系统进行操作和使用,以及后期的相关人员对系统进行更新和维护。本系统的实现可以极大地提高企业的工作效率,提升用户的使用体验,因此在现实生活中运用本系统具有很大的使用价值。 关键词:博客管理;Java语言;B/S结构;MySQL数据库
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值