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. Ecshop 扯淡问题

    1:解决 :在 temp 文件下创建 backup文件夹   修改权限 2:待补充...

  2. SQL数据查询

    CREATE TABLE class0328( id INT, cname ), sex ), age INT, birthday DATE, html DOUBLE, js DOUBLE, scor ...

  3. JS 封装一个求圆面积的函数 传值:半径

    y(6) var s = ""; function y (r){ s = Math.PI*r*r; alert(s); }

  4. elasticsearch聚合函数

    计算每个tag下的商品数量 GET /ecommerce/product/_search { "aggs": {  //聚合 "group_by_tags": ...

  5. python笔记3----第一个小爬虫

    1.先看看要爬的网站有没有爬虫协议,可以看该网站有没有robots.txt,如豆瓣的: 2.requests模块:[requests是第三方,代码比python自带的urllib模块简单] 先加载re ...

  6. Django入门--模板路径配置及渲染

    模板就是前端的页面,Django把html源码写到模板文件中,然后通过特定方法渲染后交给客户端. 模板路径设置方法有两种,分别是在项目目录下设置以及在应用目录下设置. 模板查找顺序:优先在DIRS设置 ...

  7. 深入了解Spring中的容器

    1.创建Bean的3种方式 1.1使用构造器创建bean实例 这是最常见的方式,如果不采用构造注入,bean类需要有默认构造函数.如果采用构造注入,则需要配置xml文件的<constructor ...

  8. oracle 禁用外键约束

    1.ORACLE数据库中的外键约束名都在表user_constraints中可以查到.其中constraint_type='R'表示是外键约束.2.启用外键约束的命令为:alter table tab ...

  9. redis 在 Linux 和 Windows 上的安装配置

    最近需要在服务器上安装 redis,虽然只是一个小事情,但这个过程中也遇到了不少的问题,所以做一个总结,也希望能给到其他人一些帮助. 本文记录了 linux 系统和 windows 系统的 redis ...

  10. MyBatis中动态SQL元素的使用

    掌握MyBatis中动态SQL元素的使用 if choose(when,otherwise) trim where set foreach <SQL>和<include> 在应 ...