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;
}
相关推荐
菜还不练就废了2 天前
7.24 C/C++蓝桥杯 | 排序算法
c语言·c++·蓝桥杯
Aurora_wmroy2 天前
算法竞赛备赛——【图论】拓扑排序
数据结构·c++·算法·蓝桥杯·图论
嗨信奥编程3 天前
第十四届蓝桥杯青少Scratch国赛真题——太空大战
青少年编程·蓝桥杯
竹照煜_ysn3 天前
蓝桥杯51单片机
单片机·算法·蓝桥杯·51单片机
Aurora_wmroy3 天前
算法竞赛备赛——【图论】求最短路径——Bellman-Ford & SPFA
数据结构·c++·算法·蓝桥杯·图论
落笔映浮华丶7 天前
蓝桥杯零基础到获奖-第3章 C++ 变量和常量
c++·蓝桥杯
Aurora_wmroy7 天前
算法竞赛备赛——【图论】求最短路径——小结
数据结构·c++·算法·蓝桥杯·图论
Aurora_wmroy7 天前
算法竞赛备赛——【图论】求最短路径——Floyd算法
数据结构·c++·算法·蓝桥杯·图论
ヾChen8 天前
13届蓝桥杯省赛程序设计试题
物联网·学习·蓝桥杯·c#
闻缺陷则喜何志丹9 天前
【带权的并集查找】 P9235 [蓝桥杯 2023 省 A] 网络稳定性|省选-
数据结构·c++·蓝桥杯·洛谷·并集查找