当前位置:网站首页>KMP string
KMP string
2022-06-28 06:25:00 【MITBlick】
Given a pattern string S, And a template string P, All strings contain only uppercase and lowercase letters and Arabic numerals .
Template string P In mode string S Appears as a substring many times in .
Find the template string P In mode string S The starting subscript of all positions appearing in .
Input format
Enter the integer in the first line N, Representation string P The length of .
The second line of input string P.
In the third line, enter the integer M, Representation string S The length of .
On the fourth line, enter the string S.
Output format
All in one line , Output the starting subscript of all occurrences ( Subscript from 0 Start counting ), Integers are separated by spaces .
Data range
1≤N≤1e5
1≤M≤1e6
sample input :
3
aba
5
ababa
sample output :
0 2
Code:
#include <iostream>
#include <stdio.h>
#include <vector>
#include <cmath>
#include <algorithm>
using namespace std;
typedef long long LL;
typedef int Status;
const int N = 10000010;
int n, m;
int ne[N];
char s[N], p[N];
signed main()
{
cin >> n >> p + 1 >> m >> s + 1;
for(int i = 2, j = 0; i <= n; i ++ )
{
while(j && p[i] != p[j + 1]) j = ne[j];
if(p[i] == p[j + 1]) j ++;
ne[i] = j;
}
for(int i = 1, j = 0; i <= m; i ++ )
{
while(j && s[i] != p[j + 1]) j = ne[j];
if(s[i] == p[j + 1]) j ++;
if(j == n)
{
cout << i - n << ' ';
j = ne[j]; // j Point to the last letter of successful matching
}
}
}
边栏推荐
- Yygh-6-wechat login
- Linux Mysql 实现root用户不用密码登录
- 异常处理(一)——空指针和数组索引越界
- SQL and list de duplication
- 使用SQL select count distinct查询语句统计数据库中某个字段的唯一值总数量
- Alert pop-up processing in Web Automation
- Enum
- Freeswitch uses Mod_ Shot module plays mp3
- D3D11_ Chili_ Tutorial (3): design a bindable/drawable system
- Install redis on windows and permanently change the password, and integrate redis with the SSM framework
猜你喜欢
Speech enhancement - spectrum mapping
【Paper Reading-3D Detection】Fully Convolutional One-Stage 3D Object Detection on LiDAR Range Images
Alert pop-up processing in Web Automation
How to open UMD, KMD log and dump diagrams in CAMX architecture
YOLOv5增加小目标检测层
AutoCAD C polyline self intersection detection
Working principle of es9023 audio decoding chip
Sharing tips for efficient scripting
Caused by: com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance
Oracle condition, circular statement
随机推荐
Freeswitch uses origin to dialplan
Parsing ng template with let total in NZ Pagination
Yygh-7-user management
Failed to start component [StandardEngine[Catalina]. StandardHost[localhost]]
Configure multiple database connections using the SSM framework
Alibaba cloud SMS service (Complete Guide), SMS sending function implementation.
Enum
Note that JPA uses a custom VO to receive jpql query results
Batch import of pictures into WPS table by date
FPGA - 7 Series FPGA selectio -08- oserdese2 of advanced logic resources
sql及list去重操作
Caused by: com.fasterxml.jackson.databind.exc.InvalidFormatException:异常解决
Drop down list processing in Web Automation
Xcode13.3.1 error reported after pod install
自定义 cube-ui 弹出框dialog支持多个且多种类型的input框
Iframe switching in Web Automation
借助nz-pagination中的let-total解析ng-template
Oracle fundamentals summary
Tryout title code
Yolact++ Pytorch环境