Skip to content

Commit

Permalink
修改
Browse files Browse the repository at this point in the history
  • Loading branch information
arkingc committed Jul 7, 2018
1 parent 6faf320 commit e9e7445
Show file tree
Hide file tree
Showing 8 changed files with 125 additions and 2 deletions.
1 change: 1 addition & 0 deletions interview/操作系统.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
* 被同一进程的其它线程调用[pthread_cancel取消线程](https://github.com/arkingc/note/blob/master/%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F/UNIX%E7%8E%AF%E5%A2%83%E9%AB%98%E7%BA%A7%E7%BC%96%E7%A8%8B.md#7pthread_cancel%E5%87%BD%E6%95%B0)
* 进程main函数`return`或任何线程调用了`exit``_Exit``exit`
+ 12)[线程与信号](temp/信号.md#2线程与信号)
+ 13)[正确使用waitpid处理终止的子进程](https://github.com/arkingc/note/blob/master/%E8%AE%A1%E7%AE%97%E6%9C%BA%E7%BD%91%E7%BB%9C/UNIX%E7%BD%91%E7%BB%9C%E7%BC%96%E7%A8%8B%E5%8D%B71.md#12-%E4%BD%BF%E7%94%A8waitpid%E7%89%88sig_chld%E5%87%BD%E6%95%B0%E5%A4%84%E7%90%86%E5%AD%90%E8%BF%9B%E7%A8%8Bsigchld%E4%BF%A1%E5%8F%B7)
- **2.并发**
+ 1)[Linux上分析死锁的简单方法](http://blog.jobbole.com/109743/)
+ 2)进程间通信的主要方式([管道](https://github.com/arkingc/note/blob/master/%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F/UNIX%E7%8E%AF%E5%A2%83%E9%AB%98%E7%BA%A7%E7%BC%96%E7%A8%8B.md#1%E7%AE%A1%E9%81%93)[FIFO](https://github.com/arkingc/note/blob/master/%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F/UNIX%E7%8E%AF%E5%A2%83%E9%AB%98%E7%BA%A7%E7%BC%96%E7%A8%8B.md#3fifo)、信号、[消息队列](https://github.com/arkingc/note/blob/master/%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F/UNIX%E7%8E%AF%E5%A2%83%E9%AB%98%E7%BA%A7%E7%BC%96%E7%A8%8B.md#5%E6%B6%88%E6%81%AF%E9%98%9F%E5%88%97)[共享内存](https://github.com/arkingc/note/blob/master/%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F/UNIX%E7%8E%AF%E5%A2%83%E9%AB%98%E7%BA%A7%E7%BC%96%E7%A8%8B.md#7%E5%85%B1%E4%BA%AB%E5%AD%98%E5%82%A8)[信号量](https://github.com/arkingc/note/blob/master/%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F/UNIX%E7%8E%AF%E5%A2%83%E9%AB%98%E7%BA%A7%E7%BC%96%E7%A8%8B.md#8posix%E4%BF%A1%E5%8F%B7%E9%87%8F)、套接字)
Expand Down
Binary file added pic/leetcode-28-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added pic/leetcode-28-2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added pic/leetcode-28-3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added pic/leetcode-28-4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added pic/leetcode-28-5.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
117 changes: 116 additions & 1 deletion 数据结构与算法/算法题总结.md
Original file line number Diff line number Diff line change
Expand Up @@ -9390,7 +9390,122 @@ public:

#### 2)KMP算法

[KMP算法](https://blog.csdn.net/hyjoker/article/details/51190726)
[KMP算法——知乎海纳](https://www.zhihu.com/question/21923021)

**PMT(部分匹配表):PMT中的值是字符串的前缀集合与后缀集合交集中最长串的长度**(注意,这里的前缀和后缀都不包括字符串本身)

那么PMT如何在字符串查找中运用?在字符串查找中,我们**为模版字符串创建PMT表,若模版字符串长度len,就有len个PMT值**。假设有如下例子:

* 主字符串:`"ababababca"`
* 模版字符串:`"abababca"`

模版字符串`“abababca”`相应的PMT值如下表:

<div align="center"> <img src="../pic/leetcode-28-1.png"/> </div>

* 字符串`"a"`不包含前缀和后缀(因为前缀和后缀不包含字符串本身),所以PMT中相应的值为0
* 字符串`"ab"`包含前缀`{"a"}`,包含后缀`{"b"}`,没有交集,所以PMT中相应的值为0
* 字符串`"aba"`包含前缀`{"a","ab"}`,包含后缀`{"a","ba"}`交集为`"a"`,所以相应的PMT值为1
* ...

那么当字符串查找过程中在模板字符`j`位置开始不匹配时(如下图(a)),这意味着主字符串从 `i−j` 到 `i` 这一段是与模板字符串的 `0` 到 `j` 这一段是完全相同的(即字符串`"ababab"`)。通过查找PMT表我们知道,字符串`"ababab"`前缀集合与后缀集合交集中最长串(`"abab"`)的长度为4,这说明了主字符串中 `i` 指针之前的 `PMT[j −1](此处值为4)` 位就一定与模板字符串的第 `0` 位至第 `PMT[j−1](此处值为4)` 位是相同的。**这样一来,就可以将这些字符段的比较省略掉。具体的做法是,保持`i`指针不动,然后将`j`指针指向模式字符串的`PMT[j −1]`位即可**(**这也是KMP的核心:通过使用PMT省略不必要字符段的比较**),从而进入图(b)的状态:

<div align="center"> <img src="../pic/leetcode-28-3.png"/> </div>

如果是在 `j` 位失配,那么影响 `j` 指针回溯的位置的其实是第 `j − 1` 位的 PMT 值,所以为了编程的方便,我们不直接使用PMT数组,而是将PMT数组向后偏移一位。我们把新得到的这个数组称为next数组:

<div align="center"> <img src="../pic/leetcode-28-4.png"/> </div>

以下为KMP算法:

```c
int KMP(char *t, char *p)
{
int i = 0;
int j = 0;

while (i < strlen(t) && j < strlen(p))
{
if (j == -1 || t[i] == p[j])
{
i++;
j++;
}
else
j = next[j];
}

if (j == strlen(p))
return i - j;
else
return -1;
}
```

最后是如何求next数组?求next数组的过程完全可以看成字符串匹配的过程,即以模式字符串为主字符串,以模式字符串的前缀为目标字符串,一旦字符串匹配成功,那么当前的next值就是匹配成功的字符串的长度

具体来说,就是从模式字符串的第一位(注意,不包括第0位)开始对自身进行匹配运算。 在任一位置,能匹配的最长长度就是当前位置的next值。如下图所示:

<div align="center"> <img src="../pic/leetcode-28-5.png"/> </div>

求next数组值的代码如下:

```c
void getNext(char *p, int *next)
{
next[0] = -1;
int i = 0, j = -1;

while (i < strlen(p))
{
if (j == -1 || p[i] == p[j])
{
++i;
++j;
next[i] = j;
}
else
j = next[j];
}
}
```

该题使用KMP的解法:

> 测试用例为:`"mississippi" "issipi"`时会报:`free(): invalid next size (fast): 0x0000000002856e70 ***`错误,`return`前插入输出语句可以正常输出,本地运行也输出-1,上述错误的原因暂时不明

```c++
class Solution {
public:
int strStr(string haystack, string needle) {
int len1 = haystack.length(),len2 = needle.length();

/*以下计算next数组的值*/
vector<int> next(len2,-1);
int i = 0,j = -1;
while(i < len2){
if(j == -1 || needle[i] == needle[j]){
i++,j++;
next[i] = j;
}
else
j = next[j];
}

/*以下进行字符串查找*/
i = 0,j = 0;
while(i < len1 && j < len2){
if(j == -1 || haystack[i] == needle[j])
i++,j++;
else
j = next[j];
}

if(j == len2) return i - j;
else return -1;
}
};
```

<br>
<br>
Expand Down
9 changes: 8 additions & 1 deletion 计算机网络/UNIX网络编程卷1.md
Original file line number Diff line number Diff line change
Expand Up @@ -2915,4 +2915,11 @@ recvfrom返回的IP地址不是发送数据报的目的IP地址。当服务器
|ECONNRESET|[服务器主机崩溃重启](#5服务器主机崩溃后重启)后接收到客户端的请求,响应RST分节,客户端接收后设置套接字错误为该值|
|EHOSTUNREACH|目的主机不可达|
上面的错误会引起套接字被关闭
上面的错误会引起套接字被关闭
### 2)其它错误
| 错误 | 描述 |
|:--:|:--:|
|EAGAIN|通常发生在非阻塞I/O中,如果数据未准备好,I/O操作会返回这个错误,提示再试一次|
|EINTR|表示系统调用被一个捕获的信号中断,发生该错误可以继续读写套接字|

0 comments on commit e9e7445

Please sign in to comment.