KMP(http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2772)
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char a[1000001],b[1000001];
int next[1000001];
int l,l2;
void Getnext()
{
int i=0;
int j=-1;
next[0]=-1;
while(i<l2)
{
if(-1==j||b[i]==b[j])
{
i++;
j++;
next[i]=j;
}
else j=next[j];
}
}
int main()
{
while(scanf("%s",a)!=EOF)
{
scanf("%*c%s",b);
l=strlen(a);
l2=strlen(b);
Getnext();
int i=0;
int j=0;
while(i<l&&j<l2)
{
if(a[i]==b[j]||j==-1)
{
i++;
j++;
}
else j=next[j];
}
if(j>=l2) printf("%d\n",i-l2+1);//判断条件
else printf("-1\n");
}
return 0;
}
KMP(http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2772)的更多相关文章
- KMP(http://acm.hdu.edu.cn/showproblem.php?pid=1711)
http://acm.hdu.edu.cn/showproblem.php?pid=1711 #include<stdio.h> #include<math.h> #inclu ...
- KMP应用http://acm.hdu.edu.cn/showproblem.php?pid=2594
riemann与marjorie拼接后riemannmarjorie前缀与后缀公共部分为 rie 长度为 3(即next[l] = next[14]的值,l为拼接后的长度)但:aaaa与aa拼接后aa ...
- C语言基础部分练习(http://acm.hgnu.edu.cn)
前言 最近有朋友和同学找我要c语言基础练习答案,为了方便分享,放在我的博客上了,如果对你确实有帮助,可以考虑点下赞或打赏哦(都能通过,没有专注于搞算法,所以有的地方可以优化,欢迎在评论区留言) A. ...
- 8.14比赛j题 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=87813#overview
就我个人来说我觉得这道题其实不用写题解,只是因为做的时候错了一次,如果不是队友细心,我根本会错下去,所以我感觉自己必须强大#include<stdio.h> #include<str ...
- (暂时弃坑)(半成品)ACM数论之旅18---反演定理 第二回 Mobius反演(莫比乌斯反演)((づ ̄3 ̄)づ天才第一步,雀。。。。)
莫比乌斯反演也是反演定理的一种 既然我们已经学了二项式反演定理 那莫比乌斯反演定理与二项式反演定理一样,不求甚解,只求会用 莫比乌斯反演长下面这个样子(=・ω・=) d|n,表示n能够整除d,也就是d ...
- HDU 5458 Stability(双连通分量+LCA+并查集+树状数组)(2015 ACM/ICPC Asia Regional Shenyang Online)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5458 Problem Description Given an undirected connecte ...
- Hdu OJ 5884-Sort (2016 ACM/ICPC Asia Regional Qingdao Online)(二分+优化哈夫曼)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5884 题目大意:有n个有序的序列,对于第i个序列有ai个元素. 现在有一个程序每次能够归并k个序列, ...
- HDU 5029 Relief grain(离线+线段树+启发式合并)(2014 ACM/ICPC Asia Regional Guangzhou Online)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5029 Problem Description The soil is cracking up beca ...
- kmp(前缀出现次数next应用)
http://acm.hdu.edu.cn/showproblem.php?pid=3336 Count the string Time Limit: 2000/1000 MS (Java/Other ...
随机推荐
- Android与互联网的交互方式有三种
数据下载:下载网络上的的数据,包括图片.代码文本.XML文件.JSON数据,音/视频等大文件,调用webservice. 数据上传:上传图片.文本.XML.JSON数据.音/视频文件,调用webser ...
- Sublime text3配置LiveReload 浏览器即时刷新
1.在sublime控件台 install livereload插件(缺点:每次重新打开Sublime都需要启动) 2.配置Preference > Package Settings > ...
- Unix/Linux 查看文件大小
ls -l help.html-rw-r--r-- 1 william wheel 40960 Jul 18 00:59 development.sqlite3 40960 就是文件的大小. d ...
- Docker制作私有的基础镜像
debootstrap是debian/ubuntu下的一个工具,用来构建一套基本的系统(根文件系统).生成的目录符合Linux文件系统标准(FHS),即包含了/boot./etc./bin./usr等 ...
- 【CF891E】Lust 生成函数
[CF891E]Lust 题意:给你一个长度为n的序列$a_i$,对这个序列进行k次操作,每次随机选择一个1到n的数x,令$res+=\prod\limits_{i!=x}a_i$(一开始res=0) ...
- 【CF932G】Palindrome Partition 回文自动机
[CF932G]Palindrome Partition 题意:给你一个字符串s,问你有多少种方式,可以将s分割成k个子串,设k个子串是$x_1x_2...x_k$,满足$x_1=x_k,x_2=x_ ...
- Coding 代码管理快速入门(转)
当项目创建好了之后,我们该如何上传代码到 coding 上呢? Coding 网站使用“ Git 仓库”(类似 github )来管理代码. 其操作原理在于:利用 git 服务,将本地的项目目录下的文 ...
- vim配置函数跳转(c/c++)
暂时草记一下,有时间好好整理 ctags 如果只是查看函数与变量是在哪里定义的,用ctags就可以了. ctrl+]跳到定义的地方,ctrl+t跳回来. 想要像IDE那样在旁边显示函数与变量列表,用t ...
- javaweb分页的后端实现
先上demo图 servlet实现部分: package servlet; import java.io.IOException; import java.util.List; import java ...
- Scala实现乘法口诀
object Test4 { def main(args: Array[String]) { for (i <- 1 to 9; j <- 1 to 9 if (j <= i ...