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. Windows环境下制作MACOS X U盘安装盘

    前两天在朋友的MAC BOOK AIR上胡乱操作时把原来安装好的双系统搞坏了,一不小心又把硬盘格式化了,导致MAC系统也没了,于是只能重新安装MACOS系统,并根据网友提供的教程,在MACOS安装OK ...

  2. JS 100以内的质数、只能被1和自己整除

    for(var i = 2;i <= 100;i++){ var biao = 1; for(var j = 2;j < i;j++){ if(i%j == 0){ biao = 0; } ...

  3. The socket is closed!

    关闭mongodb    /usr/local/app/mongidb//bin/mongod   --shutdown  --dbpath /usr/local/data/mongo/data/ 然 ...

  4. sklearn学习1----sklearn.SVM.SVC

    1.SVM有两种作用:分类和回归,分类是用SVC,回归用SVR. 2.SVC:(中文官网) 重点在svm.SVC(),fit(X,Y),以及SVC中的参数. 3.SVC参数: ①C,C是控制软间隔中的 ...

  5. THUWC2019 划水记

    Day -2 在学校呆了inf天之后终于回家了! Day 0 在家无(tui)所(fei)事(mo)事(yu),顺便被拉出去剪了个头发,想写写thusc2017的题也写不动,一直在网上冲浪,到处乱翻以 ...

  6. [noip2011]计算系数+二项式定理证明

    大水题,二项式定理即可(忘得差不多了) 对于一个二项式,\((a+b)^n\)的结果为 \(\sum_{k=0}^{k<=n}C_{n}^{k}a^{n-k}b^k\) 证明: 由数学归纳法,当 ...

  7. Python全双工聊天

    全双工聊天 全双工聊天:服务端和客户端都可以发送并接收信息. 使用select模块中的select方法 select(rlist, wlist, xlist[, timeout]) -> (rl ...

  8. 【 ACM-ICPC 2018 沈阳赛区网络预赛 D】Made In Heaven

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 点可以重复走的k短路. [代码] #include <bits/stdc++.h> #define LL long lo ...

  9. Ubuntu 15.10配置OpenCV

    本系列文章由 @yhl_leo 出品,转载请注明出处. 文章链接: http://blog.csdn.net/yhl_leo/article/details/50635705 1 安装前准备 安装以下 ...

  10. BA--无风机冷却塔

    无风机冷却塔的优点 1.无运转振动噪音传统冷却塔噪声源为冷却风扇马达运转所产生,其诱发之塔体振动,具有噪音共振加强性,为有效抑制振动之传导,须加装防震铁架及避震器.因冷却方式不同,LFC-N型无风机科 ...