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;
}
相关推荐
aaaweiaaaaaa1 小时前
c++基础学习(学习蓝桥杯 ros2有C基础可看)
c++·学习·蓝桥杯·lambda·ros2·智能指针·c++类
汉克老师2 小时前
第十四届蓝桥杯青少组C++选拔赛[2023.2.12]第二部分编程题(4、最大空白区)
c++·算法·蓝桥杯·蓝桥杯c++·c++蓝桥杯
scx201310041 天前
P13929 [蓝桥杯 2022 省 Java B] 山 题解
c++·算法·蓝桥杯·洛谷
闻缺陷则喜何志丹2 天前
【数论】P10580 [蓝桥杯 2024 国 A] gcd 与 lcm|普及+
c++·数学·蓝桥杯·数论·洛谷
汉克老师3 天前
第十四届蓝桥杯青少组C++选拔赛[2023.2.12]第二部分编程题(1、求和)
c++·蓝桥杯·蓝桥杯c++·c++蓝桥杯
汉克老师4 天前
第十四届蓝桥杯青少组C++国赛[2023.5.28]第二部分编程题(4、 数独填数)
c++·蓝桥杯·蓝桥杯c++·c++蓝桥杯
闻缺陷则喜何志丹4 天前
【 线段树】P12347 [蓝桥杯 2025 省 A 第二场] 栈与乘积|普及+
数据结构·c++·蓝桥杯·线段树·洛谷
古译汉书5 天前
蓝桥杯算法之基础知识(6)
数据结构·算法·蓝桥杯
古译汉书6 天前
蓝桥杯算法之基础知识(4)
开发语言·python·算法·蓝桥杯
古译汉书6 天前
蓝桥杯算法之基础知识(5)
数据结构·算法·蓝桥杯