Simpsons’ Hidden Talents
文章目录
- 题目描述
- 知识点
- 实现
- 码前思考
- 代码实现
- 码后反思
题目描述
知识点
KMP
实现
码前思考
- 这道题就是进行字符串匹配问题。但是需要特别注意需要防止在
text
中找到pattern
匹配的情况
代码实现
//此题应该就是字符串的匹配问题
#include <cstdio>
#include <cstring>
const int maxn = 5e4+10;int nex[maxn];
char text[maxn];
char pattern[maxn];void getNex(){int len = strlen(pattern);nex[0] = -1;int j = -1;for(int i=1;i<len;i++){while(j!=-1&&pattern[i]!=pattern[j+1]){j = nex[j];}if(pattern[i] == pattern[j+1]){j++;}nex[i]=j;}
}int main(){while(scanf("%s",pattern)!=EOF && scanf("%s",text)){getNex();//开始进行匹配int m = strlen(text);int n = strlen(pattern);int j = -1;for(int i=0;i<m;i++){if(j == m-1){//如果完全匹配,那么需要进行回退 j=nex[j];}while(j != -1 && text[i] != pattern[j+1]){j = nex[j];}//需要进行特殊的操作 if(text[i] == pattern[j+1]){j++;}}//得到最后的jif(j>=0){for(int i=0;i<=j;i++){printf("%c",pattern[i]);} printf(" %d\n",j+1); }else{printf("%d\n",0);}}return 0;
}
码后反思
- 需要特别注意下面代码的书写:
if(j == m-1){//如果完全匹配,那么需要进行回退 j=nex[j]; }
一定要考虑这种情况,而且这个得写在最前面,不能写成求解次数那也写在后面!因为要考虑
abc
与abc
这种情况。
Simpsons’ Hidden Talents相关推荐
- HDU 2594 Simpsons’ Hidden Talents (字符串-KMP)
Simpsons' Hidden Talents Problem Description Homer: Marge, I just figured out a way to discover some ...
- kmp总结(相关例题1. Simpsons’ Hidden Talents 2.Oulipo)
kmp相关及相关例题 文章目录 kmp相关及相关例题 一.kmp算法最常规使用方法 二.相关例题 1. Simpsons' Hidden Talents 2.Oulipo 一.kmp算法最常规使用方法 ...
- HDU2594(Simpsons’ Hidden Talents)
Simpsons' Hidden Talents Problem Description Homer: Marge, I just figured out a way to discover some ...
- B - Simpsons’ Hidden Talents
B - Simpsons' Hidden Talents Homer: Marge, I just figured out a way to discover some of the talents ...
- HDU-2594 Simpsons’ Hidden Talents
HDU-2594 Simpsons' Hidden Talents 题目链接:HDU-2594 题目大意:给定两个字符串 问第一个字符串前缀与第二个字符串的后缀的最大的重复部分有多长 不为0的话将他们 ...
- Simpsons’ Hidden Talents (HDU-2594)
Simpsons' Hidden Talents (HDU-2594) Homer: Marge, I just figured out a way to discover some of the t ...
- Simpsons’ Hidden Talents(KMP ,两个串的前后缀匹配)
Simpsons' Hidden Talents 题目 给两个串,求S1的前缀和S2的后缀的最大匹配 思路 拼接两个串,处理出nxt数组,nxt[k] 即为所求,因为它们的最大匹配不能超过原串的长度, ...
- Simpsons’ Hidden Talents(辛普森一家的隐藏天赋 )(kmp经典模板题) HDU - 2594
题目:Simpsons' Hidden Talents(辛普森一家的隐藏天赋 ) 中文大意 Homer: Marge, I just figured out a way to discover som ...
- HDU2594——Simpsons’ Hidden Talents
Problem Description Homer: Marge, I just figured out a way to discover some of the talents we weren' ...
- kuangbin专题十六 KMP扩展KMP HDU2594 Simpsons’ Hidden Talents
Homer: Marge, I just figured out a way to discover some of the talents we weren't aware we had. Marg ...
最新文章
- 学计算机必懂的53个单词缩写
- POJ2942 Knights of the Round Table 点双连通分量 二分图判定
- C语言二叉树的逆向有序遍历(附完整源码)
- 数据库MySQL/mariadb知识点——函数
- codeforces438 D. The Child and Sequence
- 微信 手机 网站 开发 签名 signature node (在更新中)
- Android studio Github 断开连接
- 信息学奥赛一本通(1040:输出绝对值)
- Maven手工管理项目
- 线程安全和线程不安全的简述及辅助记忆
- JS手动实现一个new操作符
- with as 语句真的会把数据存内存嘛?(源码剖析)
- 各种交换机端口安全总结(配置实例)
- 游戏必备组件有哪些_微信广告将升级小程序、小游戏开发者收入方案
- java飞机大战强化版_java飞机大战升级版源码(全彩,背景音乐,各种音效,不同僚机,子弹特效以及技能系统)...
- mac安装教程 sqlyog_Mac开发环境配置:Homebrew的安装
- 以淘宝为例,解析大型电商服务端架构!
- 「程序猿 DD」星球活动第一期正式开启!
- java正则表达式 手机_Java正则表达式
- JPEG格式压缩算法