Kattis - String Matching(kmp)
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 |
2 4 5 |
题意
多组输入,每组两行,模式串和对比串,输出上面的模式串在下面的字符串中的所有位置下标,下标从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)的更多相关文章
- Binary String Matching(kmp+str)
Binary String Matching 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 Given two strings A and B, whose alp ...
- string matching(拓展KMP)
Problem Description String matching is a common type of problem in computer science. One string matc ...
- 【ACM】Binary String Matching
Binary String Matching 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 Given two strings A and B, whose alp ...
- nyoj 题目5 Binary String Matching
Binary String Matching 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 Given two strings A and B, whose alp ...
- Binary String Matching
问题 B: Binary String Matching 时间限制: 3 Sec 内存限制: 128 MB提交: 4 解决: 2[提交][状态][讨论版] 题目描述 Given two strin ...
- NYOJ之Binary String Matching
Binary String Matching 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 Given two strings A and B, whose a ...
- ACM Binary String Matching
Binary String Matching 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 Given two strings A and B, whose alp ...
- 南阳OJ----Binary String Matching
Binary String Matching 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 Given two strings A and B, whose alp ...
- Aho - Corasick string matching algorithm
Aho - Corasick string matching algorithm 俗称:多模式匹配算法,它是对 Knuth - Morris - pratt algorithm (单模式匹配算法) 形 ...
随机推荐
- Java中方法重载
方法重载:指在同一个类中,允许存在一个以上的同名方法,只要它们的参数列表不同即可,与修饰符和返回值类型无关. 参数列表:个数不同,数据类型不同,顺序不同. 重载方法调用:JVM通过方法的参数列表,调用 ...
- centos7常见的操作
centos7的网络IP地址配置文件在 /etc/sysconfig/network-scripts 文件夹下, 查看当前网卡名称 ip ad li ens33网卡对应的配置文件为ifcfg-ens ...
- vc++如何创建程序-构造函数02
1.若忘记了赋值,出现运行结果是很大的负值(因为我们定义的x与y这两个成员变量存储在内存中是一个随机的值) 当我们调用时,随机输出. //包含输入输出的头文件#include<iostream. ...
- anaconda下jieba和wordcloud安装
1.在anaconda交互环境下安装jieba,输入命令: pip install jieba 2.在https://pypi.python.org/pypi/wordcloud下载wordclou ...
- 网络教程(8)IP协议
两台计算机间不是直接连接的,所以不能使用PPP协议,那么它们是怎么链接的呢? host A想把信息传给host B ethernet帧中的目标mac地址传到SFO后开始使用PPP协议 [Media ...
- 网上的CSS例子编写都不太严谨,如*{ margin:0;padding:0;}
margin:0;padding:0; 一般情况下不可以用 *{margin:0;padding:0;} 来适配. 保证自己的严谨代码编写风格.
- [总结-动态规划]经典DP状态设定和转移方程
马上区域赛,发现DP太弱,赶紧复习补上. #普通DP CodeForces-546D Soldier and Number Game 筛法+动态规划 待补 UVALive-8078 Bracket S ...
- [2] day 02
1. df.memory_usage()将返回每列占用多少 要包含索引,请传递index=True所以要获得整体内存消耗: 2. numpy.iinfo 3. shift函数 https://blog ...
- [noip2011 luogu1312] Mayan游戏(模拟)
原题:传送门 大模拟- 两个剪枝: 1.如果左边不为空就不往左边走(因为一定不如左边的移到右边优) 2.如果相邻两颜色相同不需移动 当然也有别的小剪枝(我没写)比如如果当前某一颜色剩余块数满足1< ...
- [bzoj 2726] 任务安排 (斜率优化 线性dp)
3月14日第三题!!!(虽然是15号发的qwq) Description 机器上有N个需要处理的任务,它们构成了一个序列.这些任务被标号为1到N,因此序列的排列为1,2,3-N.这N个任务被分成若干批 ...