hduoj------2594 Simpsons’ Hidden Talents
Simpsons’ Hidden Talents
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2875 Accepted Submission(s): 1095
Problem Description
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
Source
HDU 2010-05 Programming Contest
简单的kmp。。。
代码:
1 #include<iostream>
2 #include<cstring>
3 #include<cstdlib>
4 using namespace std;
5 const int maxn=50050;
6 char str[maxn],st[maxn];
7 int next[maxn];
8 int main()
9 {
10 while(gets(str)!=NULL)
11 {
12 gets(st);
13 int lena=strlen(str);
14 int lenb=strlen(st);
15 int i=0,j=-1;
16 next[0]=-1;
17 while(i<lena)
18 {
19 if(j==-1||str[i]==str[j])
20 next[++i]=++j;
21 else j=next[j];
22 }
23 i=j=0;
24 while(i<lenb)
25 {
26 if(j==-1||str[j]==st[i])
27 {
28 i++;
29 j++;
30 }
31 else j=next[j];
32 }
33 if(j==0) printf("0n");
34 else
35 printf("%s %dn",st+lenb-j,j);;
36 }
37
38 }
- 用一个命令使用 Docker Compose 安装Mesos
- C语言/C加加新手入门学习经验资料分享,基础知识大汇总!
- 云数据服务蜂拥而至...好难选呀
- ASP.NET MVC的Razor引擎:View编译原理
- ASP.NET Core中的依赖注入(4): 构造函数的选择与服务生命周期管理
- 通过Knockout.js + ASP.NET Web API构建一个简单的CRUD应用
- 自动驾驶车辆在2017年的七大光辉历史时刻
- 简单的实现Javascript的MVC
- 谷歌再出黑科技!人工智能模拟的人声和真人几乎难以分辨
- 【Scikit-Learn 中文文档】高斯混合模型 - 无监督学习 - 用户指南 | ApacheCN
- “E+单拼”组合域名eqi.com小六位结拍
- ASP.NET Core中的依赖注入(3): 服务的注册与提供
- 【Scikit-Learn 中文文档】使用 scikit-learn 介绍机器学习 | ApacheCN
- 哈萨比斯:2018年AI最大突破在生物或化学领域
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- Redis中string、list的底层数据结构原理
- Redis中字符串的表示
- Redis分布式锁背后的原理
- 解析Transformer模型
- 这5个常问的Redis面试题你答得出来吗?(详细剖析)
- 性能最佳实践:MongoDB索引
- Python基本数据类型-list-tuple-dict-set
- 深度学习应用的服务端部署
- MongoDB中的CURD操作
- 高可用的Redis主从复制集群,从理论到实践
- SpringBoot实战(一):使用Lombok简化你的代码
- Kubernetes Ingress入门指南和实践练习
- [译]Go语言常用文件操作汇总
- Redis常用数据类型对应的数据结构
- 详解卷积中的Winograd加速算法