ARTS-week09

Algorithms

本周算法题:
Implement strStr()

思路:匹配就行了

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
class Solution {
public:
int strStr(string haystack, string needle) {
int hstr = haystack.size();
int nstr = needle.size();

if(nstr == 0) {
return 0;
}
if(nstr > hstr) {
return -1;
}

int j;
for(int i = 0; i <= hstr - nstr; ++i) {
for(j = 0; j < nstr; ++j) {
if(haystack[i + j] != needle[j]) {
break;
}
}
if(j == nstr) {
return i;
}
}
return -1;
}
};

Search Insert Position

解法一:

1
2
3
4
5
6
7
8
9
10
11
class Solution {
public:
int searchInsert(vector<int>& nums, int target) {
for(int i = 0; i < nums.size(); ++i) {
if(nums[i] >= target) {
return i;
}
}
return nums.size();
}
};

解法二:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
class Solution {
public:
int searchInsert(vector<int>& nums, int target) {
int n = nums.size();
if(n == 0 || nums[n - 1] < target) {
return n;
}
if(nums[0] >= target) {
return 0;
}

int left = 0;
int right = n - 1;
while(right - left > 1) {
int mid = left + (right - left) / 2;
if(nums[mid] < target) {
left = mid;
}
else {
right = mid;
}
}
return right;
}
};

Review

本周阅读英文文章 Why All Programmers Are Creatives

Software is poetry. It’s the expression of ideas in their most elegant form.

The largest and smallest of scales collide and you are lost within it, completely absorbed by — and it’s electric.

Technique

Bro version:2.5.5
之前使用Bro分析SMB流量时,只产生了部分的log文件,例如smb_cmd.log就没有产生。

查询原因,在官方文档中看到这样一句话:
https://www.bro.org/sphinx/scripts/policy/protocols/smb/main.bro.html

1
Optionally write out the SMB commands log. This is primarily useful for debugging so is disabled by default.

通过以下两个链接找到了解决办法:
https://bro-tracker.atlassian.net/browse/BIT-1815
http://mailman.icsi.berkeley.edu/pipermail/bro/2017-May/012024.html

在之前说过在该版本需要手动在/opt/bro/share/bro/site/local.bro中启用加载SMB analyzer,之后需要在/opt/bro/share/bro/policy/protocols/smb/main.bro文件中将

1
const write_cmd_log = F &redef;

这一行中的F改为T

1
const write_cmd_log = T &redef;

然后在root用户下执行命令:

1
bro  /opt/bro/share/bro/site/local.bro -r /bropcap/smb.pcap

可以看到产生了15个log文件,其中就包含smb_cmd.log。

Share

《Linux系统编程手册(下册)》 小心拒绝服务攻击
1.服务器应该执行负载控制。当负载超过预先设定的限制之后就丢弃请求。这可能会导致丢弃合法的请求,但能够防止服务器和主机机器的负载过大。资源限制和磁盘限额的使用也有助于限制过量的负载。
2.服务器应该为与客户端的通信设置超时时间,这样如果客户端不响应(可能是故意的),那么服务器也不会永远等待客户端。
3.在发生超负荷时,服务器应该记录下合适的信息以便管理员得知这个问题。(但日志记录应该也是有限制的,这样日志记录本身不会给系统增加过量的负载)。
4.服务器程序在碰到预期之外的负载时不应该崩溃。如应该严格进行边界检查以确保过多的请求不会造成数据结构溢出。
5.设计的数据结构应该能够避免算法复杂度攻击。如二叉树应该是平衡的,在常规负载下应该能提供可接受的性能。但攻击者可能会构造一组会导致树不平衡(在最坏情况下等价于一个链表)的输入,从而降低性能。

0%