Skip to content

Commit

Permalink
kmp
Browse files Browse the repository at this point in the history
  • Loading branch information
he3210 committed Dec 7, 2021
1 parent b530cf0 commit b2f3981
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 139 deletions.
1 change: 1 addition & 0 deletions algorithm notebook/.#二分查找.org
1 change: 1 addition & 0 deletions algorithm notebook/algorithm.org
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,4 @@
- [[./二叉排序树.org][二叉排序树]]
- [[./平衡二叉树.org][平衡二叉树]]
- [[./红黑树.org][红黑树]]
- 线段树
164 changes: 66 additions & 98 deletions algorithm notebook/kmp.org
Original file line number Diff line number Diff line change
Expand Up @@ -7,124 +7,92 @@
* BF 解法

#+BEGIN_SRC c
#include<assert.h>
int BF(const char *str, const char *substr)
{
assert(str && substr);
int lenStr = strlen(str);
int lenSubstr = strlen(substr);
if(lenStr < lenSubstr)
return -1;

for(int i = 0; i != lenStr-lenSubstr+1; ++i)
{
int j;
for(j = 0; j != lenSubstr; ++j)
if(str[i+j] != substr[j])
break;
if(j == lenSubstr)
return i;
}
return -1;
}
int BF(const char *str, const char *substr) {
assert(str && substr);
int n = strlen(str);
int m = strlen(substr);
for(int i = 0; i < n - m + 1; ++i) {
int j;
for(j = 0; j < m; ++j)
if(str[i+j] != substr[j])
break;
if(j == m)
return i;
}
return -1;
}
#+END_SRC

* KMP 解法
#+BEGIN_SRC c
#include<stdio.h>
#include<string.h>

// 获取指针数组 previous
void getPrevious(const char *pattern, int lenPattern, int *previous)
{
int curPos = 0;
previous[curPos] = -1;
int flagPos = -1;
while(curPos < lenPattern-1)
{
if(-1 == flagPos || pattern[curPos] == pattern[flagPos])
{
++curPos;
++flagPos;
previous[curPos] = flagPos;
#include<stdlib.h>

void get_next(const char *pattern, int plen, int *next) {
int i = 0;
next[i] = -1; // 用来保存主串和模式串失配时,模式串应向前回溯的位置
int j = -1;
while(i < plen-1) {
if(j == -1 || pattern[i] == pattern[j]) { // 例如 "xyxa" 中的两个 'x'
++i;
++j;
next[i] = j; // 例如 "xyxa",主串和 'a' 失配后,应该回溯到 'y',主串再和 'y' 比较一下
} else {
j = next[j];
}
else
flagPos = previous[flagPos];
}
}

int KMP(const char *str, int lenStr, const char *pattern, int lenPattern, const int *previous, int pos)
{
int strPos = pos;
int patternPos = 0;
while(strPos != lenStr && patternPos != lenPattern)
{
if(-1 == patternPos || str[strPos] == pattern[patternPos])
{
++strPos;
++patternPos;
// 改进版本
void get_next_v2(const char *pattern, int plen, int *next) {
int i = 0;
next[i] = -1;
int j = -1;
while(i < plen-1) {
if(j == -1 || pattern[i] == pattern[j]) { // 模式串中位置连续的两个子串,上一个字符匹配上了。例如 "xyxy" 中下标 0 的 'x' 和下标 2 的 'x'
++i;
++j;
if(pattern[i] == pattern[j]) // 模式串中的两个子串的下一个字符也匹配上了,例如 "xyxy" 的子串 "xy" "xy" 中 'y'。如果主串和模式串下标 3 的 'y' 失配的话,肯定也和下标 1 的 'y' 失配,所以可以向前回溯两次
next[i] = next[j];
else
next[i] = j; // 例如 "xyxa" 中的 'y' 和 'a'。如果主串和模式串中 'a' 失配的话,'a' 又和 'y' 不相等,所以主串不一定和 'y' 失配,所以应该回溯到 'y'
} else {
j = next[j];
}
else
patternPos = previous[patternPos];
}
if(patternPos == lenPattern)
return strPos-lenPattern;
else
return -1;
}

int main(void)
{
const char *str = "How are you";
const char *substr = "are";
printf("%d\n", BF(str, substr));

const char *str2 = "001012012301234";
const char *substr2 = "0120123";
int previous[1024];
getPrevious(substr2, strlen(substr2), previous);
printf("%d\n", KMP(str2, strlen(str2), substr2, strlen(substr2), previous, 0));
int KMP(const char *str, int slen, const char *pattern, int plen, const int *next) {
int i = 0, j = 0;
while(i < slen && j < plen) {
if(j == -1 || str[i] == pattern[j]) {
++i;
++j;
} else {
j = next[j];
}
}
if(j == plen)
return i - plen;
return -1;
}

return 0;
int main(int argc, char **argv) {
char str[] = "abxyxyxyyxxyxadf";
char pattern[] = "xyxyyxxyx";
int slen = strlen(str);
int plen = strlen(pattern);
int *next = (int*)calloc(plen, sizeof(int));
get_next_v2(pattern, plen, next);
printf("%d\n", KMP(str, slen, pattern, plen, next));
}
#+END_SRC

* 优化

#+BEGIN_SRC c
void getPrevious(const char *pattern, int lenPattern, int *previous)
{
int curPos = 0;
previous[curPos] = -1;
int flagPos = -1;
while(curPos < lenPattern-1)
{
if(-1 == flagPos || pattern[curPos] == pattern[flagPos])
{
++curPos;
++flagPos;
if(pattern[curPos] != pattern[flagPos])
previous[curPos] = flagPos;
else
/* 此时 pattern[curPos-1] 和 pattern[flagPos-1] 相等、
pattern[curPos] 和 pattern[flagPos] 相等。
这样的字符串形式为:"...xy...xy...",
而 previous 数组存在的意义是源串和模式串不匹配时模式串应该回溯的下标
第一个 "xy" 和第二个 "xy" 相同,由于源串和第二个 "xy" 不匹配,所以肯定和第一个 "xy" 也不匹配(例如:源串为 xyxx,模式串为 xyxy)
故向前回溯时,应跳过第一个 "xy",使用 previous[curPos] = previous[flagPos] 语句
这样生成的 previous 数组可以使 KMP 函数中少一些不必要的比较
*/
previous[curPos] = previous[flagPos];
}
else
flagPos = previous[flagPos];
}
}
#+END_SRC

* previous 数组
* next 数组

对于模式串"xyxyyxxyx",previous数组为
对于模式串"xyxyyxxyx",next 数组为
| 下标 | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
|-------+----+---+---+---+---+---+---+---+---|
| value | -1 | 0 | 0 | 1 | 2 | 0 | 1 | 1 | 2 |
Expand Down
Binary file removed images/jetbrains-agent.jar.zip
Binary file not shown.
Binary file added images/个人收入证明.pdf
Binary file not shown.
Binary file added images/时中贺_应聘后台开发岗.pdf
Binary file not shown.
Binary file added images/离职证明.pdf
Binary file not shown.
41 changes: 0 additions & 41 deletions qu/clion 注册码.org
Original file line number Diff line number Diff line change
@@ -1,46 +1,5 @@
#+TITLE: clion 注册码

* 方法一
#+BEGIN_QUOTE
点击下方链接进去下载(普通下载)

1月激活码---
https://www.lanzous.com/b0ev9wx1g?
密码:77ab

激活码必须下载到电脑在复制,不然失效,如何激活:软件菜单-帮助-注册,把激活码全部复制到软件激活窗口中的Activation code

激活不成功 请看这里:?https://shimo.im/docs/8icBv925HHESLZ1w/?
感谢您的光临~

或者使用以下九个账号之一 密码都是Aa123456
2020141
2020142
2020143
2020144
2020145
2020146
2020147
2020148
2020149
#+END_QUOTE

* 方法二
[[../images/jetbrains-agent.jar.zip][jetbrains-agent.jar.zip]]

- 点击试用进入 IDE
- 点击 Help -> Edit Custom VM Options ...
- 在打开的vmoptions编辑窗口末行添加 jetbrains-agent.jar 文件的绝对路径
#+BEGIN_QUOTE
-javaagent:/Users/he/Downloads/crack_file/jetbrains-agent/jetbrains-agent.jar
#+END_QUOTE
- 重启 IDE。点击 Help -> Register...
- 选择License server方式,地址填入:http://fls.jetbrains-agent.com
- 点击激活
* 激活码
#+BEGIN_QUOTE
3AGXEJXFK9-eyJsaWNlbnNlSWQiOiIzQUdYRUpYRks5IiwibGljZW5zZWVOYW1lIjoiaHR0cHM6Ly96aGlsZS5pbyIsImFzc2lnbmVlTmFtZSI6IiIsImFzc2lnbmVlRW1haWwiOiIiLCJsaWNlbnNlUmVzdHJpY3Rpb24iOiIiLCJjaGVja0NvbmN1cnJlbnRVc2UiOmZhbHNlLCJwcm9kdWN0cyI6W3siY29kZSI6IklJIiwiZmFsbGJhY2tEYXRlIjoiMjA4OS0wNy0wNyIsInBhaWRVcFRvIjoiMjA4OS0wNy0wNyJ9LHsiY29kZSI6IkFDIiwiZmFsbGJhY2tEYXRlIjoiMjA4OS0wNy0wNyIsInBhaWRVcFRvIjoiMjA4OS0wNy0wNyJ9LHsiY29kZSI6IkRQTiIsImZhbGxiYWNrRGF0ZSI6IjIwODktMDctMDciLCJwYWlkVXBUbyI6IjIwODktMDctMDcifSx7ImNvZGUiOiJQUyIsImZhbGxiYWNrRGF0ZSI6IjIwODktMDctMDciLCJwYWlkVXBUbyI6IjIwODktMDctMDcifSx7ImNvZGUiOiJHTyIsImZhbGxiYWNrRGF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IGWMAkGA1UdEwQCMAAwHQYDVR0OBBYEFGEpG9oZGcfLMGNBkY7SgHiMGgTcMEgGA1UdIwRBMD+AFKOetkhnQhI2Qb1t4Lm0oFKLl/GzoRykGjAYMRYwFAYDVQQDDA1KZXRQcm9maWxlIENBggkA0myxg7KDeeEwEwYDVR0lBAwwCgYIKwYBBQUHAwEwCwYDVR0PBAQDAgWgMA0GCSqGSIb3DQEBCwUAA4ICAQBonMu8oa3vmNAa4RQP8gPGlX3SQaA3WCRUAj6Zrlk8AesKV1YSkh5D2l+yUk6njysgzfr1bIR5xF8eup5xXc4/G7NtVYRSMvrd6rfQcHOyK5UFJLm+8utmyMIDrZOzLQuTsT8NxFpbCVCfV5wNRu4rChrCuArYVGaKbmp9ymkw1PU6+HoO5i2wU3ikTmRv8IRjrlSStyNzXpnPTwt7bja19ousk56r40SmlmC04GdDHErr0ei2UbjUua5kw71Qn9g02tL9fERI2sSRjQrvPbn9INwRWl5+k05mlKekbtbu2ev2woJFZK4WEXAd/GaAdeZZdumv8T2idDFL7cAirJwcrbfpawPeXr52oKTPnXfi0l5+g9Gnt/wfiXCrPElX6ycTR6iL3GC2VR4jTz6YatT4Ntz59/THOT7NJQhr6AyLkhhJCdkzE2cob/KouVp4ivV7Q3Fc6HX7eepHAAF/DpxwgOrg9smX6coXLgfp0b1RU2u/tUNID04rpNxTMueTtrT8WSskqvaJd3RH8r7cnRj6Y2hltkja82HlpDURDxDTRvv+krbwMr26SB/40BjpMUrDRCeKuiBahC0DCoU/4+ze1l94wVUhdkCfL0GpJrMSCDEK+XEurU18Hb7WT+ThXbkdl6VpFdHsRvqAnhR2g4b+Qzgidmuky5NUZVfEaZqV/g==
#+END_QUOTE
* 2021.02.25已激活clion
- 链接 https://tech.souyunku.com/?p=16189
- 修改 reset_jetbrains_eval_mac_linux.sh 脚本变量 JB_PRODUCTS,并执行
Expand Down

0 comments on commit b2f3981

Please sign in to comment.