1140:验证子串--next.data()、KMP和find

1140:验证子串--KMP

题目

解析

对于字符串的匹配常见的KMP算法【面试常考】

KMP中需要注意的是:应该从下标1开始遍历,因为下标0前面无值,不能匹配next

固在循环外应初始next[0]=0;

cpp 复制代码
next[0]=0;
//i需从1开始遍历,因为下标0前面无值,不能匹配next
	for(int i=1;i<s.size();i++)

便于使用的find代码也在下面了

next.data()

next.data()是std::vector容器的一个成员函数,它的作用是获取底层数组的首地址

cpp 复制代码
vector<int> next(10); // 创建一个有10个int的数组
int* ptr = next.data(); // 获取底层数组的首地址

KMP代码

cpp 复制代码
#include <iostream>
#include <vector>
#include<set>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <climits>  // 包含INT_MAX常量
#include <cctype>
#include<map>
using namespace std;

void getNext(string s,int *next){
	int j=0;
	next[0]=0;
//i需从1开始遍历,因为下标0前面无值,不能匹配next
	for(int i=1;i<s.size();i++){
		while(j>0&&s[i]!=s[j]) j=next[j-1];
		if(s[i]==s[j]) j++;
		next[i]=j;
	}
}

bool check(string s1,string s2){
	if(s2.size()==0) return true;
	vector<int>next(s2.size());
	getNext(s2,next.data());
	int j=0;
	for(int i=0;i<s1.size();i++){
		while(j>0&&s1[i]!=s2[j]) j=next[j-1];
		if(s1[i]==s2[j]) j++;
		if(j==s2.size()) return true;
	}
	return false;
}
int main(){
	string s1,s2;
	cin>>s1>>s2;
	if(check(s2,s1)){
		cout<<s1 << " is substring of " << s2 << endl;
		return 0;
	}
	if(check(s1,s2)){
		cout<< s2 << " is substring of " << s1 << endl;
		return 0;
	}
	cout<<"No substring" << endl;
return 0;
}

Find代码

cpp 复制代码
#include<iostream>
#include<string>
#include<map>
#include<algorithm>
using namespace std;
const int N = 1e2 + 10;
int a[N];
int cnt;
int main()
{
	string s1, s2;
	cin >> s1 >> s2;
	if (s1.length() > s2.length())
	{
		if (s1.find(s2) != -1)
			cout << s2 << " is substring of " << s1;
		else
			cout << "No substring";
	}

//这里的else覆盖率长度相等的情况
	else
		if (s2.find(s1) != -1)
			cout << s1 << " is substring of " << s2;
	else
			cout << "No substring";
}

交不了了,明天再试试

cpp 复制代码
#include <iostream>
#include <vector>
#include<set>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <climits>  // 包含INT_MAX常量
#include <cctype>
#include<map>
using namespace std;
void getNext(string s,int *next){
	int j=0;
	for(int i=1;i<s.size();i++){
		while(j>0&&s[i]!=s[j]) j=next[j-1];
		if(s[i]==s[j]) j++;
		next[i]=j;
	}
}

bool check(string s1,string s2){
	vector<int>next(s2.size());
	getNext(s2,next.data());
	int j=0;
	for(int i=1;i<s1.size();i++){
		while(j>0&&s1[i]!=s2[j]) j=next[j-1];
		if(s1[i]==s2[j]) j++;
		if(j==s2.size()) return true;
	}
	return false;
}

int main(){
	string s1,s2;
	cin>>s1>>s2;
	if(check(s2,s1)) cout<<s1<< " is substring of " << s2 << endl;
	else if(check(s2,s1)) cout<<s2<<" is substring of " << s1 << endl;
	else cout<<"No substring" << endl;
return 0;
}
相关推荐
小臭希8 小时前
python蓝桥杯备赛常用算法模板
开发语言·python·蓝桥杯
mldl_9 小时前
(个人题解)第十六届蓝桥杯大赛软件赛省赛C/C++ 研究生组
c语言·c++·蓝桥杯
刃神太酷啦20 小时前
基础算法篇(5)(蓝桥杯常考点)—动态规划(C/C++)
数据结构·c++·算法·leetcode·蓝桥杯·动态规划·蓝桥杯c++组
Jerry说前后端1 天前
2025年第十六届蓝桥杯省赛C++ 研究生组真题
c++·算法·蓝桥杯
Jerry说前后端1 天前
2025年第十六届蓝桥杯省赛C++ A组真题
java·c++·蓝桥杯
a东方青1 天前
[16届蓝桥杯 2025 c++省 B] 移动距离
c++·算法·蓝桥杯
学c++的一天1 天前
蓝桥杯备战
算法·职场和发展·蓝桥杯
zero.cyx1 天前
蓝桥杯 DFS
算法·蓝桥杯·深度优先
ChoSeitaku2 天前
NO.91十六届蓝桥杯备战|图论基础-图的存储和遍历|邻接矩阵|vector|链式前向星(C++)
c++·蓝桥杯·图论
ChoSeitaku2 天前
NO.88十六届蓝桥杯备战|动态规划-多重背包|摆花(C++)
c++·蓝桥杯·动态规划