洛谷 P3375 【模板】KMP字符串匹配

本文详细介绍了KMP算法的基本原理及其应用,并通过一道洛谷平台的编程题进行实战演练,帮助读者掌握KMP算法的核心思想及其实现细节。

我这段时间因为字符串太差而被关了起来了(昨晚打cf不会处理字符串现场找大佬模板瞎搞,差点就凉了),所以决定好好补一下字符串的知识QAQ,暂时先学习kmp算法吧~

题目链接:https://www.luogu.org/problemnew/show/P3375

题目:

思路:本题是kmp模板题,不会kmp的看官可以看本题在洛谷的题解,题解区有大量的讲解,我顺便在这里推荐一篇大佬的文章,帮助大家理解kmp算法,http://www.ruanyifeng.com/blog/2013/05/Knuth%E2%80%93Morris%E2%80%93Pratt_algorithm.html。

代码实现如下:

 1 #include <set>
 2 #include <map>
 3 #include <queue>
 4 #include <stack>
 5 #include <cmath>
 6 #include <bitset>
 7 #include <cstdio>
 8 #include <string>
 9 #include <vector>
10 #include <cstdlib>
11 #include <cstring>
12 #include <iostream>
13 #include <algorithm>
14 using namespace std;
15 
16 typedef long long ll;
17 typedef pair<ll, ll> pll;
18 typedef pair<ll, int> pli;
19 typedef pair<int, ll> pil;;
20 typedef pair<int, int> pii;
21 typedef unsigned long long ull;
22 
23 #define lson i<<1
24 #define rson i<<1|1
25 #define bug printf("*********\n");
26 #define FIN freopen("D://code//in.txt", "r", stdin);
27 #define debug(x) cout<<"["<<x<<"]" <<endl;
28 #define IO ios::sync_with_stdio(false),cin.tie(0);
29 
30 const double eps = 1e-8;
31 const int mod = 10007;
32 const int maxn = 1e6 + 7;
33 const double pi = acos(-1);
34 const int inf = 0x3f3f3f3f;
35 const ll INF = 0x3f3f3f3f3f3f3f;
36 
37 int lens1, lens2;
38 string s1, s2;
39 int nex[maxn];
40 
41 void get_next() {
42     nex[0] = -1;
43     for(int i = 0, k = -1; i < lens2; ) {
44         if(k == -1 || s2[i] == s2[k]) {
45             ++k;++i;
46             nex[i] = k;
47         } else k = nex[k];
48     }
49 }
50 
51 void kmp() {
52     get_next();
53     int i = 0, j = 0;
54     while(i < lens1 && j < lens2) {
55         if(j == -1 || s1[i] == s2[j]) {
56             i++,j++;
57             if(j == lens2) {
58                 printf("%d\n", i - j + 1);
59                 j = nex[j];
60             }
61         } else j = nex[j];
62     }
63 }
64 
65 int main() {
66     //FIN;
67     cin >>s1 >>s2;
68     lens1 = s1.size(), lens2 = s2.size();
69     kmp();
70     for(int i = 1; i <= lens2; i++) {
71         printf("%d%c", nex[i], i == lens2 ? '\n' : ' ');
72     }
73     return 0;
74 }

 

转载于:https://www.cnblogs.com/Dillonh/p/9419912.html

### 关于平台上的相似字符串题目算法平台上,“相似字符串”相关的题目通常涉及字符串匹配、编辑距离或字符比较等算法。根据提供的引用内容[^1],判断两个字符串是否相似可以通过以下方法实现: #### 1. 字符串长度相等的情况 如果两个字符串的长度相等,则可以通过统计不同字符的个数来判断它们是否相似。如果不同字符的个数不超过1,则认为一个字符串可以通过修改一个字符变成另一个字符串,从而判定为相似[^1]。 #### 2. 字符串长度差为1的情况 当两个字符串的长度差为1时,较长的字符串通过删除一个字符可以变成较短的字符串。具体实现方法是从左到右同时遍历两个字符串,找到第一个不同的字符位置。如果从该位置开始,较长字符串的剩余部分较短字符串的剩余部分相同,或者较长字符串的剩余部分较短字符串的下一个位置开始的剩余部分相同,则它们相似[^1]。 #### 3. 字符串长度差大于1的情况 如果两个字符串的长度差大于1,则直接判定它们不相似[^1]。 #### 平台上的相关题目 在平台上,字符串相似性相关的题目可能包括但不限于以下几种类型: - **KMP算法**:KMP算法是一种高效的字符串匹配算法,其核心是利用匹配失败后的信息减少不必要的匹配操作[^2]。上有一道经典的KMP模板题P3375模板KMP字符串匹配[^3]。 - **编辑距离问题**:这类问题通常要求计算将一个字符串转换为另一个字符串所需的最少操作次数(如插入、删除或替换字符)。虽然这不是严格意义上的“相似字符串”问题,但字符串相似性的概念密切相关。 - **字符比较问题**:类似于引用中的描述,这类问题可能要求判断两个字符串是否可以通过特定的操作(如修改、删除或插入字符)变得相似。 #### 示例代码:KMP算法实现 以下是一个基于C++的KMP算法实现示例,用于解决字符串匹配问题: ```cpp #include <bits/stdc++.h> using namespace std; char A[1000005], B[1000005]; int nxt[1000005]; int main() { scanf("%s%s", A, B); int i = 0, j = 0; int n = strlen(A), m = strlen(B); nxt[0] = 0; i++; while (i < n) { j = nxt[i - 1]; while (j && B[i] != B[j]) j = nxt[j - 1]; if (B[i] == B[j]) nxt[i] = j + 1; else nxt[i] = 0; i++; } i = 0; while (i < n) { while (j && A[i] != B[j]) j = nxt[j - 1]; if (A[i] == B[j]) j++; if (j == m) { int ans = i - j + 1; j = nxt[j - 1]; cout << ans << endl; } i++; } for (int i = 0; i < m; i++) printf("%d ", nxt[i]); return 0; } ``` 此代码实现了KMP算法的核心逻辑,并输出了模式串的部分匹配表(next数组)。 ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值