J - Simpsons’ Hidden Talents

2024-03-20 22:38
文章标签 hidden simpsons talents

本文主要是介绍J - Simpsons’ Hidden Talents,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

J - Simpsons’ Hidden Talents

 

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.

Input

Input consists of two lines. The first line contains s1 and the second line contains s2. You may assume all letters are in lowercase.

Output

Output consists of a single line that contains the longest string that is a prefix of s1 and a suffix of s2, followed by the length of that prefix. If the longest such string is the empty string, then the output should be 0. 
The lengths of s1 and s2 will be at most 50000.

Sample Input

clinton
homer
riemann
marjorie

Sample Output

0
rie 3

题意:

            多组输入,给出两个字符串——a,b,求a的前缀和b的后缀最长公共串。

 

思路: 

            将a,b连接起来,利用KMP求最长相同前后缀。

注意:

next[la+llb] 不能大于 a或b 的长度。大于的话,应输出小的字符串。

代码:

 

#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define MOD 10007
const int MM=50010;
#define mem(a,b) memset(a,b,sizeof(a))int ne[MM<<1];
char mo[MM<<1];
int lm;
void Get_next()
{int i=0,j=-1;ne[0]=-1;while(i<lm){while(j!=-1&&mo[i]!=mo[j])j=ne[j];ne[++i]=++j;}
}
int main()
{char s[MM];while(~scanf("%s%s",mo,s)){lm=strlen(mo);int  ls=strlen(s);int lmin=min(lm,ls);strcat(mo,s);mem(ne,0);lm=strlen(mo);Get_next();if(ne[lm]==0)      //没有相同的printf("0\n");else if(ne[lm]>lmin)     //相同长度大于 a或b 的长度{if(lmin<ls)          //a<bprintf("%s %d\n",s+ls-lmin,lmin);else                //a>bprintf("%s %d\n",s,lmin);}elseprintf("%s %d\n",mo+lm-ne[lm],ne[lm]);}return 0;
}

 

这篇关于J - Simpsons’ Hidden Talents的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/830985

相关文章

display:none与visiblity:hidden的应用

display:none时,该对象不占用空间,也不可见 visibility:hidden时,该对象占据空间,仅不可见 JQuery中,.hiden(),.show()方法对应的时display的操作 要隐藏父元素的同时显示子元素该用visibility: 父元素:visibility:hidden子元素:visibility:visible

UniApp 中去掉 body 中的 style=“overflow: hidden;“ 样式

<!-- App.vue --><template><div><!-- 这里是你的页面内容 --><router-view/></div></template><style>/* 全局样式 */html,body {/* 去掉 overflow: hidden; */overflow: visible !important;}</style>

使用overflow:hidden;无法去掉滚动条问题

<div class="bg"><img src="@/assets/imgs/home-page.png" alt="首页图" style="width: auto; height: calc(100vh - 64px)"><div class="text"><h1>亿色科技 易于生活</h1></div></div> 用计算属性计算产生滚动条的一边,如高度

IE overflow:hidden失效的解决方法

position和float是两种布局模式,在IE6下共用有时会导致用position定位的元素节点消失。绝对定位的元素节点是参照离它最近的已定位元素,所以父节点定位relative、子节点定位absolute是很常见的。 overflow:hidden失效 当父元素的直接子元素或者下级子元素的样式拥有position:relative属性时,父元素的overflow:hid

display: none 和 visibility: hidden 的共性与区别

display: none 和 visibility: hidden 的共性与区别 共性:display: none 和 visibility: hidden 都是用于设置元素可见性的样式 区别 display: none 使元素及其占位完全消失:元素及其所有子元素将从文档流和布局中完全消失,就像它们不存在一样。触发回流:这种改变会触发页面上的布局重建,因为其他元素会填补被移除元素的空间。

题解:CF1370F2 The Hidden Pair (Hard Version)

题意 交互题。 给你一棵 n n n 个点的树,需要得出树上两个点 X , Y X,Y X,Y 的位置。你可以向评测机询问一个点集,评测机会回答点集中与 X , Y X,Y X,Y 距离和最小的点,以及这个距离和。询问不超过 11 11 11 次。 思路 询问次数不能超过 11 11 11 次,这个数字与 log ⁡ n \log n logn 的值很接近。考虑先对所有

composer Token(hidden)

使用 Composer 的时候提示输入Token (hidden): 标签:如果   set   code   复制   使用   ngs   。。   hub   limit    出现了Could not fetch https://api.github.com/ 。。。please create a GitHub OAuth token to go

CSS 的overflow:hidden 属性详细解释

百度经验:jingyan.baidu.com   overflow:hidden这个CSS样式是大家常用到的CSS样式,但是大多数人对这个样式的理解仅仅局限于隐藏溢出,而对于清除浮动这个含义不是很了解。一提到清除浮动,我们就会想到另外一个CSS样式:clear:both,我相信对于这个属性的理解大家都不成问题的。但是对于“浮动”这个词到底包含什么样的含义呢?我们下面来详细的阐述一

hidden与css中的display区别

display:none ---不为被隐藏的对象保留其物理空间,即该对象在页面上彻底消失,通俗来说就是看不见也摸不到。 visible:hidden--- 使对象在网页上不可见,但该对象在网页上所占的空间没有改变,通俗来说就是看不见但摸得到。 <html> <head> <title>display:none和visible:hidden的区别</t

display:none;visibility: hidden;opacity: 0; 重排 重绘

/* 1重排 2不会被子元素继承,但是父元素都不在了,子元素自然也就不会显示了 3无法触发绑定的事件 4transition无效 */display: none;/* 1重绘 2会被子元素继承,可以通过设置子元素visibility:visible 使子元素显示出来 3无法触发绑定的事件 4transition无效 */visibility: hidden;/* 1重绘 2也会被子元素继承