SDUTOJ 2772--KMP简单应用
来源:互联网 发布:第三方 windows 系统 编辑:程序博客网 时间:2024/06/06 02:57
Description
给定两个字符串string1和string2,判断string2是否为string1的子串。
Input
输入包含多组数据,每组测试数据包含两行,第一行代表string1(长度小于1000000),第二行代表string2(长度小于1000000),string1和string2中保证不出现空格。
Output
对于每组输入数据,若string2是string1的子串,则输出string2在string1中的位置,若不是,输出-1。
Sample Input
abca12345645abcddd
Sample Output
14-1
#include <stdio.h>#include <string.h>char s[1000050],t[1000050];int next[1000050];void getnext(){ int i=0,j=-1; next[0]=-1; while(t[i]!='\0') { if(j==-1||t[i]==t[j]) { i++; j++; next[i]=j; }else j=next[j]; }}void kmp(){ int i=0,j=0; getnext(); int len1=strlen(s),len2=strlen(t); while(i<len1&&j<len2) { if(j==-1||s[i]==t[j]) { i++; j++; }else j=next[j]; } if(j>=len2) printf("%d\n",i-len2+1); else printf("-1\n");}int main(){ while(~scanf("%s%s",s,t)) { kmp(); } return 0;}
0 0
- SDUTOJ 2772--KMP简单应用
- SDUTOJ 2772 KMP简单应用
- SDUTOJ KMP简单应用 【KMP】
- 【2772】KMP的简单应用 sdutOJ
- KMP的简单应用(SDUT 2772)
- KMP简单应用
- SDUT KMP简单应用
- kmp简单应用
- KMP简单应用
- KMP简单应用
- KMP简单应用
- KMP简单应用
- KMP简单应用
- KMP简单应用
- KMP简单应用
- KMP简单应用
- KMP简单应用
- KMP简单应用
- 【最大流+最短路+二分】POJ-2112 Optimal Milking
- 每天学习一点编程(15)(HWOJ字符串排序)
- 安装eclipse中html/jsp/xml editor插件以及修改html页面的字体
- IT忍者神龟之关于oracle with as用法终结
- 线程管理四种方法
- SDUTOJ 2772--KMP简单应用
- IT忍者神龟之Oracle DBA常用查询吐血列举
- 搭建我的CentOS7
- eclipse 使用Metrics 测量复杂度
- IT忍者神龟之oracle中斜杠(/)的含义吐血
- iOS 蓝牙使用小结 bluetooth
- EPOLL两种模式详解
- 条件注释判断浏览器<!--[if !IE]><!--[if IE]><!--[if lt IE 6]><!--[if gte IE 6]>
- rapidjson::Document _doc解析流程