当前位置:网站首页>08_ strand
08_ strand
2022-07-02 15:13:00 【Listen to the rain】
strand
Empty string :“” —>“\0”
Space string :" " —>" \0"
Substring :
True string :


Suitable for the following string :
Not suitable for the following string :
1.BF Algorithm ( Simple algorithm )

// Simple algorithm BF Algorithm :1. Run backwards at the same time 2. If equal i++ j++ 3. If it's not equal i=i-j+1 j=0
int BF_Search(const char* str, const char* sub, int pos)// From the main string pos The subscript starts looking back for the substring
{
assert(str != NULL && sub != NULL && pos >= 0 && pos < strlen(str));
if (str == NULL || sub == NULL || pos < 0 || pos >= strlen(str))
{
// \0
return -1;
}
int lenstr = strlen(str);//lenstr Save the length of the main string
int lensub = strlen(sub);//lensub Save the length of the substring
int i = pos; //i Save the subscript of the main string matching
int j = 0; //j Save the subscript of substring matching
while (i < lenstr && j < lensub)// When the main string and string are not out of range , The cycle continues
{
if (str[i] == sub[j])// If equal be i and j meanwhile ++, Judge the next character
{
i++;
j++;
}
else // Representative mismatch If it doesn't match Then let i Go back to the next position before this match and j Zeroing
{
i = i - j + 1;//
j = 0;// The order of these two lines of code cannot be reversed
}
}
// here while The loop ends There are only two possibilities You need to judge i and j Who walked to the tail
if (j >= lensub)// If the string j Go to the tail It means that the match is successful
{
return i - j;// If the match is successful Then return the position before this successful match
}
// If the string j Did not go to the tail That means the main string doesn't have what the string wants Then return to -1
return -1;
}

2.KMP Algorithm ( Only related to substring )

Substring mismatch 2 In this case :

seek next Array :

Code implementation :
int* Get_next(const char* sub)//next Arrays are only related to substrings
{
//assert
int lensub = strlen(sub);//lensub Save the length of the substring
int* next = (int*)malloc(sizeof(int) * lensub);// Apply for an equal length next Array to hold each of them K value
assert(next != NULL);
next[0] = -1;//next The first two spaces of the array have fixed values -1 0
next[1] = 0;
int j = 1;//j Saved known k The subscript
int k = 0;
// Push through the unknown j Is known be j+1 It's unknown and j+1 To judge the legitimacy of ( Give Way j+1<lensub that will do )
while (j + 1 < lensub)// to next Each space of the array is assigned an appropriate value
{
if ((k == -1) || sub[j] == sub[k])// Two equal or k Back to -1
{
next[++j] = ++k;
/*k++; j++; next[j] = k;*/
}
else
{
k = next[k];//k Back to the right place
}
}
return next;// take malloc Applied next Throw out the array
}
int KMP_Search(const char* str, const char* sub, int pos)// From the main string pos The subscript starts looking backwards for the string
{
assert(str != NULL && sub != NULL && pos >= 0 && pos < strlen(str));
if (str == NULL || sub == NULL || pos < 0 || pos >= strlen(str))
{
return -1;
}
int lenstr = strlen(str);//lenstr Save the length of the main string
int lensub = strlen(sub);//lensub Save the length of the string
int i = pos; //i Save the subscript when the main string matches
int j = 0; //j Save the subscript when the substring matches
int* next = Get_next(sub);//next Arrays are only related to substrings
while (i < lenstr && j < lensub)// When the main string and string are not out of range , The cycle continues
{
if ((j == -1) || str[i] == sub[j])// If equal be i and j meanwhile ++, Judge the next character
{
i++;
j++;
}
else // Representative mismatch If it doesn't match Then let i Go back to the next position before this match and j Zeroing
{
//i = i-j+1;//
//j = 0;// The order of these two lines of code cannot be reversed
j = next[j];//j Return to the right place next[j]
}
}
// here while The loop ends There are only two possibilities You need to judge i and j Who walked to the tail
if (j >= lensub)// If the string j Go to the tail It means that the match is successful
{
return i - j;// If the match is successful Then return the position before this successful match
}
// If the string j Did not go to the tail That means the main string doesn't have what the string wants Then return to -1
return -1;
}
边栏推荐
- CTO如何帮助业务?
- N皇后问题的解决
- 2021-2022学年编译原理考试重点[华侨大学]
- 08_ 串
- Learn the method code example of converting timestamp to uppercase date using PHP
- 编译原理课程实践——实现一个初等函数运算语言的解释器或编译器
- 电脑怎么设置扬声器播放麦克风的声音
- Error: NPM warn config global ` --global`, `--local` are deprecated Use `--location=global` instead.
- Kityformula editor configure font size and spacing
- Leetcode - Search 2D matrix
猜你喜欢

Practical debugging skills

【NOI模拟赛】刮痧(动态规划)
![[noi Simulation Competition] scraping (dynamic planning)](/img/ee/27a07f80207a2925f5065e633eb39f.png)
[noi Simulation Competition] scraping (dynamic planning)

N皇后问题的解决

vChain: Enabling Verifiable Boolean Range Queries over Blockchain Databases(sigmod‘2019)

MathML to latex

关于网页中的文本选择以及统计选中文本长度

GeoServer offline map service construction and layer Publishing

c语言入门--数组

Learn the method code example of converting timestamp to uppercase date using PHP
随机推荐
info [email protected] : The platform “win32“ is incompatible with this module.
电脑怎么设置扬声器播放麦克风的声音
vChain: Enabling Verifiable Boolean Range Queries over Blockchain Databases(sigmod‘2019)
04_ 栈
Base64 编码原来还可以这么理解
【C语言】详解指针的初阶和进阶以及注意点(1)
Practice of compiling principle course -- implementing an interpreter or compiler of elementary function operation language
使用 TiUP 部署 TiDB 集群
geoserver离线地图服务搭建和图层发布
Implementation of n queen in C language
学习使用php实现公历农历转换的方法代码
Tidb data migration scenario overview
871. 最低加油次数 : 简单优先队列(堆)贪心题
实用调试技巧
Li Chuang EDA learning notes 15: draw border or import border (DXF file)
Wechat applet uses towxml to display formula
Have you learned the wrong usage of foreach
[noi Simulation Competition] scraping (dynamic planning)
Add vector formula in rich text editor (MathType for TinyMCE, visual addition)
How to conduct TPC-C test on tidb