原文请訪问我的博客:xiaoshig.sinaapp.com
KMP模式匹配 三(串)

Time Limit:1000MS     Memory Limit:131072KB     64bit IO Format:%lld
& %llu

Description

输入一个主串和一个子串,若匹配成功,则找出匹配的趟数和在子串在主串中的位置,若匹配不成功,则输出0

Input

输入两个字符串

Output

输出匹配的趟数和位置

Sample Input

ababcabcacbab
abcac

Sample Output

3 6
#include<iostream>
#include<cstring>
using namespace std;
int a[100000];
char c[10000];
char b[100000];
int main()
{
int j,n,i,k;
cin>>c;
cin>>b; a[0]=-1; j=-1;
i=0;
while(b[i]!='\0')
{
if(j==-1||b[j]==b[i])
{
j++;i++;
// if(b[i]!=b[j])
a[i]=j;
// else
// {if(a[j]==-1)
// a[i]=0;
// else
// a[i]=a[j];}
}
else j=a[j];
}
i=0;
j=0;k=0;n=1;
while(c[i]!='\0')
{
if(c[i]==b[j])
{i++;j++;if(b[j]=='\0'){k=1;break;}}
else if(a[j]==-1){i++;j=0;n++;}
else {j=a[j];n++;}
}
i=i-strlen(b)+1;
if(k==0)
cout<<-1<<endl; else cout<<n<<' '<<i<<endl;
return 0;
}

版权声明:本文博主原创文章,博客,未经同意不得转载。

KMP模式匹配 三(弦)的更多相关文章

  1. YTU 2297: KMP模式匹配 三(串)

    2297: KMP模式匹配 三(串) 时间限制: 1 Sec  内存限制: 128 MB 提交: 25  解决: 16 [提交][状态][讨论版] [Edit] [TestData] 题目描述 输入一 ...

  2. KMP模式匹配_2

    http://blog.csdn.net/lin_bei/article/details/1252686 三. 怎么求串的模式值next[n] 定义: (1)next[0]= -1 意义:任何串的第一 ...

  3. 字符串的朴素模式和KMP模式匹配

    先复习一下字符串指针: #include <iostream> #include <string.h> using namespace std; int main() { ch ...

  4. YTU 2296: KMP模式匹配 二(串)

    2296: KMP模式匹配 二(串) 时间限制: 1 Sec  内存限制: 128 MB 提交: 29  解决: 17 题目描述 输入一个主串和一个子串,用KMP进行匹配,问进行几趟匹配才成功,若没成 ...

  5. YTU 2295: KMP模式匹配 一(串)

    2295: KMP模式匹配 一(串) 时间限制: 1 Sec  内存限制: 128 MB 提交: 32  解决: 22 题目描述 求子串的next值,用next数组存放,全部输出 输入 输入一个字符串 ...

  6. KMP算法 KMP模式匹配 一(串)

    A - KMP模式匹配 一(串) Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:131072KB  ...

  7. 2295: KMP模式匹配 一(串)

    2295: KMP模式匹配 一(串) 时间限制: 1 Sec  内存限制: 128 MB提交: 210  解决: 97[提交][状态][讨论版][命题人:外部导入] 题目描述 求子串的next值,用n ...

  8. KMP模式匹配

    http://www.cnblogs.com/wangguchangqing/archive/2012/09/09/2677701.html nextal[j+1]=next[j]+1 KMP算法的实 ...

  9. hdu 1686 & poj 2406 & poj 2752 (KMP入门三弹连发)

    首先第一题 戳我穿越;http://acm.hdu.edu.cn/showproblem.php?pid=1686 题目大意好理解,每组输入一个子串和一个母串,问在母串中有多少个子串? 文明人不要暴力 ...

随机推荐

  1. android 管理Bitmap内存 - 开发文档翻译

    由于本人英文能力实在有限,不足之初敬请谅解 本博客只要没有注明“转”,那么均为原创,转贴请注明本博客链接链接   Managing Bitmap Memory 管理Bitmap内存 In additi ...

  2. 【Java】使用Runtime执行其他程序

    public class ExecDemo{ public static void main(String[] args) { Runtime r = Runtime.getRuntime(); Pr ...

  3. 函数dirname--返回路径中的目录部分

    http://blog.chinaunix.net/uid-122937-id-142880.html dirname() 函数作用   返回路径中的目录部分. 语法   dirname(path) ...

  4. 1 & 167. Two Sum I & II ( Input array is sorted )

    Input array is sorted: Use binary search or two pointers Unsorted: Use hash map, key = target - a[i] ...

  5. ubuntu下google 拼音输入法的安装

    google拼音输入法安装 (1).获代替码:(没有git的先安装git:sudoapt-get install git-core) $>gitclone git://github.com/tc ...

  6. 【最小费用最大流模板】【Uva10806+Spring Team PK】Dijkstra, Dijkstra,

    题意:从1到n 再从n到1 不经过重复的边 ,(如果是点就是旅行商问题了),问最短路 建立一个超级源S S到1连一条费用为0,容量为2的边,求费用流即可 如果流<2 那么hehe 否则    输 ...

  7. React 组件开发初探

    react.js 在线地址:http://slides.com/yueyao/deck/#/ COMPONENT JSX 预编译语言, 一个基于ECMAscript 的xml-link 的语法扩展,最 ...

  8. Intent组件

    Android页面之间的跳转可以分为几类,比如有A和B两个页面,从A页面直接跳转到B页面,这样的跳转比较简单,或者从A页面跳转到B页面但是需要传送数据.在Android中,页面的跳转离不开Intent ...

  9. 玩转Android---事件监听篇---第2篇

    事件监听篇---第二篇 下面是各种常用控件的事件监听的使用 ①EditText(编辑框)的事件监听---OnKeyListener ②RadioGroup.RadioButton(单选按钮)的事件监听 ...

  10. Manifest merger failed : uses-sdk:minSdkVersion 9 cannot be smaller than version 10 declared in library

    Error:Execution failed for task ':app:processDebugManifest'. > Manifest merger failed : uses-sdk: ...