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 = 4000000; char s[MAXM], t[MAXM]; int next[MAXM], n; int shuchu[MAXM]; void get_next() { next[0] = -1; int i = 0, j = -1; while (t[i] != '