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. 用CSS来控制字符长度和显示长度

    在网页排版设计中,会遇到文本超过固定长度导致整体的网页变形的情况.程序员往往需要截取固定的长度来实现某些固定长度的控制.介绍一种直接采 用CSS的代码控制来实现文本截取的方法,与程序员的直接字符截取的 ...

  3. 51nod-活动安排问题之二

    有若干个活动,第i个开始时间和结束时间是[Si,fi),活动之间不能交叠,要把活动都安排完,至少需要几个教室? 分析:能否按照之一问题的解法,每个教室安排尽可能多的活动,即按结束时间排序,再贪心选择不 ...

  4. 训练1-J

    把一个偶数拆成两个不同素数的和,有几种拆法呢? Input 输入包含一些正的偶数,其值不会超过10000,个数不会超过500,若遇0,则结束. Output 对应每个偶数,输出其拆成不同素数的个数,每 ...

  5. [luogu3726 HNOI2017] 抛硬币 (拓展lucas)

    传送门 数学真的太优秀了Orz 数据真的太优秀了Orz 题目描述 小 A 和小 B 是一对好朋友,他们经常一起愉快的玩耍.最近小 B 沉迷于**师手游,天天刷本,根本无心搞学习.但是已经入坑了几个月, ...

  6. 【codeforces 799C】Fountains

    [题目链接]:http://codeforces.com/contest/799/problem/C [题意] 你有两种不同的货币; 余额分别为c和d 然后有n种商品; 每种商品只能由两种货币中的某一 ...

  7. MySQL 触发器 -1

    MySQL包含对触发器的支持.触发器是一种与表操作有关的数据库对象,当触发器所在表上出现指定事件时,将调用该对象,即表的操作事件触发表上的触发器的执行. 创建触发器 在MySQL中,创建触发器语法如下 ...

  8. GIT文件的4种状态

  9. F - Truck History

    F - Truck History #include<cstdio> #include<cstring> #include<iostream> #include&l ...

  10. HDU 2553 N皇后问题 (DFS_回溯)

    Problem Description 在N*N的方格棋盘放置了N个皇后,使得它们不相互攻击(即随意2个皇后不同意处在同一排,同一列,也不同意处在与棋盘边框成45角的斜线上. 你的任务是.对于给定的N ...