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;
}
相关推荐
DevangLic2 小时前
【3-14 STC-pair超级详细的解说】
c++·网络协议·算法·蓝桥杯
SKY YEAM2 小时前
蓝桥杯Python赛道备赛——Day3:排序算法(二)(归并排序、堆排序、桶排序)
python·蓝桥杯·排序算法
weixin_4684668511 小时前
C++蓝桥杯皮亚诺曲线距离求解
c++·算法·蓝桥杯·递归·皮亚诺·pow函数误差·皮亚诺曲线距离
桦012 小时前
每天一道算法题【蓝桥杯】【山脉数组的峰顶索引】
c++·算法·leetcode·蓝桥杯
BingLin-Liu16 小时前
蓝桥杯备考:并查集
职场和发展·蓝桥杯
苏言の狗17 小时前
R格式 | 第十五届蓝桥杯C++B组
c++·职场和发展·蓝桥杯
加油JIAX19 小时前
CT117E-M4 CubeMX与Keil5 MDK-ARM基础配置
arm开发·stm32·蓝桥杯·嵌入式
头发尚存的猿小二1 天前
2024年第十五届蓝桥杯软件C/C++大学A组——五子棋对弈
c语言·c++·蓝桥杯
煜3641 天前
stm32 蓝桥杯 物联网 独立键盘的使用
stm32·物联网·蓝桥杯