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;
}
相关推荐
爱吃生蚝的于勒3 天前
【Linux】深入理解进程(一)
java·linux·运维·服务器·数据结构·c++·蓝桥杯
Kent_J_Truman4 天前
【模拟散列表】
数据结构·算法·蓝桥杯·散列表·常识类
红糖生姜6 天前
P12874 [蓝桥杯 2025 国 Python A] 巡逻||题解||图论
c++·蓝桥杯·图论
爱吃生蚝的于勒7 天前
【Linux】零基础学会linux环境基础开发工具使用(yum,vim,makefile,gdb)
linux·服务器·数据结构·c++·蓝桥杯·编辑器·vim
旭意8 天前
C++蓝桥杯之函数与递归
开发语言·c++·蓝桥杯
wuqingshun31415911 天前
蓝桥杯 取球博弈
算法·职场和发展·蓝桥杯
wuqingshun31415911 天前
蓝桥杯 填字母游戏
游戏·职场和发展·蓝桥杯
闻缺陷则喜何志丹14 天前
【C++贪心】P8769 [蓝桥杯 2021 国 C] 巧克力|普及+
c++·算法·蓝桥杯·洛谷
旭意15 天前
C++微基础备战蓝桥杯之数组篇10.1
开发语言·c++·蓝桥杯
旭意16 天前
C++微基础备战蓝桥杯string篇10.5
开发语言·c++·蓝桥杯