当前位置:网站首页>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 2Code:
#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
}
}
}
边栏推荐
- Linux Mysql 实现root用户不用密码登录
- MySQL common functions
- Linked list (I) - remove linked list elements
- Iframe switching in Web Automation
- How to open UMD, KMD log and dump diagrams in CAMX architecture
- freeswitch设置最大呼叫时长
- ImportError: cannot import name 'ensure_ dir_ Possible solutions for exists'
- 报错--解决core-js/modules/es.error.cause.js报错
- ROS rviz_satellite功能包可视化GNSS轨迹,卫星地图的使用
- Failed to start component [StandardEngine[Catalina]. StandardHost[localhost]]
猜你喜欢

整型提昇和大小端字節序

语音增强-频谱映射

Apple MDM bypass jailfree bypass MDM configuration lock free

Caused by: com. fasterxml. jackson. databind. exc.InvalidDefinitionException: Cannot construct instance

Yygh-6-wechat login

AutoCAD C # Polyline Small Sharp angle Detection

Freeswitch sets the maximum call duration

How the third-party libraries in cocoapod reference local header files

Speech enhancement - spectrum mapping

Parsing ng template with let total in NZ Pagination
随机推荐
The custom cube UI pop-up dialog supports multiple and multiple types of input boxes
death_ satan/hyperf-validate
Configure redis from 0
4~20ma input /0~5v output i/v conversion circuit
AutoCAD C polyline small acute angle detection
Promotion intégrale et ordre des octets de fin de taille
Freeswitch使用originate转dialplan
Common basic functions of Oracle
Linked list (II) - Design linked list
Boost the rising point | yolov5 combined with alpha IOU
socke.io長連接實現推送、版本控制、實時活躍用戶量統計
借助nz-pagination中的let-total解析ng-template
AutoCAD C# 多段线自相交检测
ImportError: cannot import name 'ensure_ dir_ Possible solutions for exists'
FPGA - 7 Series FPGA selectio -08- oserdese2 of advanced logic resources
浮动与定位
Batch import of pictures into WPS table by date
【网络教程】IPtables官方教程--学习笔记1
Alert pop-up processing in Web Automation
Error reporting - resolve core JS / modules / es error. cause. JS error