String Matching

Input

The input consists of several test cases. Each test case consists of two lines, first a non-empty pattern, then a non-empty text. Input is terminated by end-of-file. The input file will not be larger than 5 Mb.

Output

For each test case, output one line containing the positions of all the occurences of pattern in text, from first to last, separated by a single space.

Sample Input 1 Sample Output 1
p
Popup
helo
Hello there!
peek a boo
you speek a bootiful language
anas
bananananaspaj
2 4

5
7

题意

多组输入,每组两行,模式串和对比串,输出上面的模式串在下面的字符串中的所有位置下标,下标从0开始

思路1

KMP算法,套个模版就可以了

思路2

用string的find,str2.find(str1,x),关于这个函数的用法http://www.cplusplus.com/reference/string/string/find/

代码1

#include<stdio.h>
#include<string.h>
using namespace std;
int const MAXM = ;
char s[MAXM], t[MAXM];
int next[MAXM], n;
int shuchu[MAXM];
void get_next()
{
next[] = -;
int i = , j = -;
while (t[i] != '\0')
{
if (j == - || t[i] == t[j])
{
i++;
j++;
next[i] = j;
}
else
j = next[j];
}
} int KMP()
{
get_next();
int i = , j = , ans = , len = strlen(t);
while (s[i])
{
if (j == - || s[i] == t[j])
{
i++;
j++;
}
else
j = next[j];
if (j == len)
{
j = next[j];
shuchu[ans++] = i;
}
}
return ans;
} int main()
{ while (gets(t)) {
gets(s);
int ss = KMP();
for (int i = ; i < ss; i++)
{
if (i)printf(" ");
printf("%d", shuchu[i]-strlen(t));
}
puts("");
}
}

代码2

#include<bits/stdc++.h>
using namespace std;
int main(){
string str1,str2;
int cnt[];
while(getline(cin,str1)){
getline(cin,str2);
int pos=,x=;
while(str2.find(str1,x)!=-&&x<str2.size()){
cnt[pos++]=str2.find(str1,x);
x=cnt[pos-]+;
}
for(int i=;i<pos;i++){
printf("%d%c",cnt[i],i==pos-?'\n':' ');
}
}
return ;
}

Kattis - String Matching(kmp)的更多相关文章

  1. Binary String Matching(kmp+str)

    Binary String Matching 时间限制:3000 ms  |  内存限制:65535 KB 难度:3   描述 Given two strings A and B, whose alp ...

  2. string matching(拓展KMP)

    Problem Description String matching is a common type of problem in computer science. One string matc ...

  3. 【ACM】Binary String Matching

    Binary String Matching 时间限制:3000 ms  |  内存限制:65535 KB 难度:3   描述 Given two strings A and B, whose alp ...

  4. nyoj 题目5 Binary String Matching

    Binary String Matching 时间限制:3000 ms  |  内存限制:65535 KB 难度:3   描述 Given two strings A and B, whose alp ...

  5. Binary String Matching

    问题 B: Binary String Matching 时间限制: 3 Sec  内存限制: 128 MB提交: 4  解决: 2[提交][状态][讨论版] 题目描述 Given two strin ...

  6. NYOJ之Binary String Matching

    Binary String Matching 时间限制:3000 ms  |  内存限制:65535 KB 难度:3 描述     Given two strings A and B, whose a ...

  7. ACM Binary String Matching

    Binary String Matching 时间限制:3000 ms  |  内存限制:65535 KB 难度:3   描述 Given two strings A and B, whose alp ...

  8. 南阳OJ----Binary String Matching

    Binary String Matching 时间限制:3000 ms  |  内存限制:65535 KB 难度:3   描述 Given two strings A and B, whose alp ...

  9. Aho - Corasick string matching algorithm

    Aho - Corasick string matching algorithm 俗称:多模式匹配算法,它是对 Knuth - Morris - pratt algorithm (单模式匹配算法) 形 ...

随机推荐

  1. Asp.net Core 源码-PagedList<T>

    using System.Collections.Generic; using System.Linq; using System; using System.Linq.Expressions; us ...

  2. immutable-js基础

    Immutable.js(和原生方法不同): 用于深层次的数组和对象的比较   数据结构:Map Set Seq List Rang(和原生不同)   首先:先忘记es5 es6的数组对象方法   官 ...

  3. Django入门--模型系统(二):常用查询及表关系的实现

    1.常用查询 模型类上的管理器: ** 模型类.objects ** (1)常用一般查询 rs = Student.objects.all() # 查询所有记录,返回Queryset print(rs ...

  4. 最快理解 - IO多路复用:select / poll / epoll 的区别.

    目录 第一个解决方案(多线程) 第二个解决方案(select) 第三个解决方案(poll) 最终解决方案(epoll) 客栈遇到的问题 从开始学习编程后,我就想开一个 Hello World 餐厅,由 ...

  5. ACdream 1229 Data Transmission

    Data Transmission Special JudgeTime Limit: 12000/6000MS (Java/Others)Memory Limit: 128000/64000KB (J ...

  6. BA-DDC与PLC的比较(转载)

    1.概念 DDC(Direct Digital Control ) ,根据字面的意思为直接的数据控制,它包含了对采集数据的数字化以及对数据的数字处理.DDC的产生式随着楼宇控制.能源管理的需求,以及为 ...

  7. 第二篇:SpringBoot配置详解

    SpringBoot 是为了简化 Spring 应用的创建.运行.调试.部署等一系列问题而诞生的产物,自动装配的特性让我们可以更好的关注业务本身而不是外部的XML配置,我们只需遵循规范,引入相关的依赖 ...

  8. jdk环境变量设置理解

    1.系统变量→新建 JAVA_HOME 变量 . 变量值填写jdk的安装目录(本人是 E:\Java\jdk1.7.0) 2.系统变量→寻找 Path 变量→编辑 在变量值最后输入 %JAVA_HOM ...

  9. 【智能家居篇】wifi网络结构(上)

    转载请注明出处:http://blog.csdn.net/Righthek 谢谢! WIFI是什么.相信大家都知道,这里就不作说明了. 我们须要做的是深入了解其工作原理,包含软硬件.网络结构等.先说明 ...

  10. 畅通project续HDU杭电1874【dijkstra算法 || SPFA】

    http://acm.hdu.edu.cn/showproblem.php?pid=1874 Problem Description 某省自从实行了非常多年的畅通project计划后.最终修建了非常多 ...