Sagheer and Nubian Market

本题不使用二分查找必然TLE。 注意二分查找的使用!

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
28
29
30
31
32
33
34
35
#include <iostream>
#include <algorithm>
using namespace std;
long long pri[100005], now[100005];
int main()
{
long long n, mon;
cin >> n >> mon;
for (int i = 0; i < n; i++)
cin >> pri[i];
long long l = 1, h = n, res = 0, s = 0, mid;
while (l <= h)
{
mid = (l + h) / 2;
long long sum = 0;
for (int j = 0; j < n; j++)
{
now[j] = pri[j] + (j + 1) * mid;
}
sort(now, now + n);
for (int j = 0; j < mid; sum += now[j++]);
if (sum <= mon)
{
res = mid;
s = sum;
l = mid + 1;
}
else
{
h = mid - 1;
}
}
cout << res << ' ' << s << endl;
return 0;
}

Sagheer and Crossroads

我以为一次要输入很多组样例,然后读取EOF终止,结果读一个就行了。。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <algorithm>
using namespace std;

int main()
{
int l[4], s[4], r[4], p[4];
int flag = 1;
for (int i = 0; i < 4; i++)
{
cin >> l[i] >> s[i] >> r[i] >> p[i];
}
for (int i = 0; i < 4; i++)
{
if (p[i] && (l[(i + 1) % 4] || s[(i + 2) % 4] || r[(i + 3) % 4] || l[i] || r[i] || s[i]))
flag = 0;
}
cout << (flag ? "NO" : "YES") << endl;
return 0;
}

Throwing cards away UVa10935

水题,输出的 remaining 巧妙地打错了导致wa。

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
#include <iostream>
#include <vector>
using namespace std;
vector<int> deck;
int main()
{
int n;
while (cin >> n && n)
{
deck.clear();
for (int i = 0; i <= n; i++)
deck.push_back(i);
int tot = 1, kase = 0;
cout << "Discarded cards:";
while (tot < n)
{
if (kase++)
cout << ",";
cout << ' ' << deck[tot];
tot++;
deck.push_back(deck[tot++]);
n++;
}
cout << endl << "Remaining card: " << deck[tot] << endl;
}
return 0;
}

Ducci Sequence UVa1594

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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <iostream>
#include <cmath>
#include <map>
#include <vector>
using namespace std;
vector<int> seq, seq2;
map<vector<int>, int> check;
int main()
{
int T;
cin >> T;
while (T--)
{
check.clear();
seq.clear();
int n;
cin >> n;
while (n--)
{
int x;
cin >> x;
seq.push_back(x);
}
check[seq] = 1;
while (true)
{
for (int i = 1; i < seq.size(); i++)
seq2.push_back(abs(seq[i] - seq[i - 1]));
seq2.push_back(abs(seq[seq.size() - 1] - seq[0]));
seq = seq2;
seq2.clear();
int flag = 1;
for (int i = 0; i < seq.size(); i++)
if (seq[i] != 0)
flag = 0;
if (flag)
{
cout << "ZERO" << endl;
goto ends;
}
if (check.count(seq))
{
cout << "LOOP" << endl;
goto ends;
}
check[seq] = 1;
}
ends: ;
}
return 0;
}

PHP文件和目录 笔记补充

笔记补充。

  1. 打开文件

语法:fopen(文件名或url,打开模式)

打开模式:

只读 r 以只读方式打开文件,指针指向文件头

读写 r+ 以读写方式打开文件,指针指向文件头

写入 w 以写入方式打开文件,指针指向文件头,若文件存在则清空,若不存在则尝试创建

读写 w+ 以读写方式打开文件,指针指向文件头,若文件存在则清空,若不存在则尝试创建

添加 a 以写入方式打开文件,指针指向文件尾,若不存在则尝试创建

添加 a+ 以读写方式打开文件,指针指向文件尾,若不存在则尝试创建

谨慎写入 x 以写入方式打开文件,指针指向文件头,若文件存在则返回false并警告

谨慎写入 x+ 以写入方式打开文件,指针指向文件头,若文件存在则返回false并警告

创建并写入 c 以写入方式打开文件,指针指向文件头,即使文件存在也不会失败或清空

创建并读写 c+ 以读写方式打开文件,指针指向文件头,即使文件存在也不会失败或清空

  1. 检查文件末尾

语法:feof(文件指针)

如果文件指针到了EOF(end of file)或出错就返回true,否则返回一个错误(包括socket超时),其他情况返回false

例:

1
2
3
4
5
$file = fopen('new.txt','r');
if( feof($file) )
{
echo '文件指针在结束位置了';
}

  1. 读取文件

语法:fread(文件指针,读取长度)

返回读取的字符串,若出错则返回false

如果读取长度比指定长度短(遇到EOF)则自动停止,返回读取的字符串

  1. 关闭文件

语法:fclose(文件指针)

执行成功返回true,否则返回false

将文件读入字符串

语法:file_get_contents(文件名)

成功则返回文件内容,失败则返回false

  1. 字符串写入文件

语法:file_put_contents(写入文件,要写入数据,打开/写入文件方式)

若写入文件不存在,则创建一个新文件。

打开/写入文件方式(可选参数):FILE_USE_INCLUDE_PATH 在include_path目录搜索要输入的文件名

FILE_APPEND 若文件已存在,则追加而非覆盖

LOCK_EX 在写入时获得一个独占锁

  1. 将整个文件读入数组

语法:file(文件名,读取参数)

成功则返回数组,元素以换行键分隔,失败则返回false

读取参数:FILE_USE_INCLUDE_PATH 在include_path中查找文件

FILE_IGNOE_NEW_LINES 每个元素末尾不添加换行符

FILE_SKIP_EMPTY_LINES 跳过空行

无需打开/读取/关闭文件就可以读取整个文件,但会大量占用系统内存

  1. 复制文件

语法:copy(源文件,目标文件)

成功返回true,失败返回false

注意:若文件已存在,则完全覆盖!

  1. 删除文件

语法:unlink(文件名)

成功返回true,失败返回false

  1. 检查文件是否正常

语法: is_file(文件名)

存在且正常则返回true,否则返回false

is_file只检查文件,而file_exists既检查文件也检查目录

  1. 返回文件信息

语法:stat(文件名)

成功则返回包含文件信息的数组,否则返回false

  1. 打开目录

语法:opendir(目录)

成功返回目录句柄,否则返回false

若目录路径不合法或没有权限,返回false及一条错误信息,若要隐藏则在opendir()前加@

  1. 关闭目录

语法:closedir(目录句柄)

若句柄未指定,系统默认为opendir打开的最后一个句柄

  1. 读取目录

语法:readdir(目录句柄)

例:

1
2
3
4
5
6
7
8
9
10
<?php
$dir = opendir('.');
while( $file = read($dir))
{
if(if_fie($file))
{
ehco "文件:$file\n";
}
}
?>

  1. 创建目录

语法:mkdir(目录名,目录模式,是否创建父目录)

成功返回true,失败返回false

目录模式:在非Windows系统上默认值0777(八进制表示的整数),Windows系统无效。

是否创建父目录:true则可以用来建立,false不建立

  1. 删除目录

语法:rmdir(目录名称)

成功返回true,失败返回false

注意:目录必须是空的,而且有权限,否则产生E_WARNING级别的错误

  1. 重命名文件或目录

语法:rename(旧文件名,新文件名)

成功返回true,失败返回false

  1. 检查文件或目录是否存在

语法:file_exists(检查目录或文件名)

目录或文件存在则返回true,否则返回false

Alignment of Code UVa1593

水题,注意stringstream和vector的使用。

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
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
using namespace std;
vector<vector<string>> code;
vector<string> line;
int maxLen[200] = { 0 };
int main()
{
string str, s;
while (getline(cin, s))
{
stringstream ss(s);
while (ss >> str)
line.push_back(str);
code.push_back(line);
line.clear();
}
for (int i = 0; i < 200; i++)
{
for (int j = 0; j < code.size(); j++)
if (i < code[j].size() && code[j][i].size() > maxLen[i])
maxLen[i] = code[j][i].size();
}
for (int i = 0; i < code.size(); i++)
{
for (int j = 0; j < code[i].size(); j++)
{
cout << code[i][j];
if (j < code[i].size() - 1)
for (int k = code[i][j].size(); k < maxLen[j] + 1; k++)
cout << ' ';
}
cout << endl;
}
return 0;
}

Database UVa1592

本题很迷,并不知道错在哪,以下给出AC代码。 去掉memset后wa,我写的输入数据库,使用getline()函数,并用‘,’作为分隔符,提交WA。 不做以上修改仍然可过udebug,很迷,搞不懂。

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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include <iostream>
#include <map>
#include <vector>
#include <string>
#include <cstring>
using namespace std;
map<string, int> ID;
vector<string> getStr;
//int table[10010][15];
vector<int> table[10010];
int getID(string& x)
{
if (ID.count(x))
return ID[x];
getStr.push_back(x);
return ID[x] = getStr.size() - 1;
}
struct point {
int x; int y;
point(int x1, int x2) :x(x1), y(x2) {};
};
bool operator<(const point& x1, const point& x2)
{
return x1.x < x2.x || x1.x == x2.x && x1.y < x2.y;
}
map<point, int> check;
void read(int n)
{
string str;
char ch = getchar();
for(int i = 0; i < n; i++)
while (true)
{
ch = getchar();
if (ch == '\n' || ch == '\r')
{
if (!str.empty())
table[i].push_back(getID(str));
str.clear();
break;
}
if (ch == ',')
{
table[i].push_back(getID(str));
str.clear();
}
else
str += ch;
}
}
int main()
{
//freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
int n, m;
while (cin >> n >> m)
{
/*for (int i = 0; i < n; i++)
{
cin.get();
for (int j = 0; j < m - 1; j++)
{
string str;
getline(cin, str, ',');
table[i][j] = getID(str);
}
string str;
cin >> str;
table[i][m - 1] = getID(str);
}*/
read(n);
int flag = 1;
for (int c1 = 0; c1 < m; c1++)
for (int c2 = c1 + 1; c2 < m; c2++)
{
if (flag)
{
check.clear();
for (int i = 0; i < n; i++)
{
point x(table[i][c1], table[i][c2]);
if (check.count(x))
{
flag = 0;
cout << "NO" << endl << check[x] + 1 << ' ' << i + 1 << endl << c1 + 1 << ' ' << c2 + 1 << endl;
break;
}
check[x] = i;
}
}
}
if (flag)
cout << "YES" << endl;
memset(table, 0, sizeof table);
ID.clear();
getStr.clear();
}
return 0;
}

The Setstack Computer UVa12096

本题的思想很重要,题意中的集合概念较抽象,因此对每种集合编号。 stack为题意所描述的stack computer,存放着抽象集合对应的唯一编号,而编号所对应的集合里面也存放着集合的编号,形成了抽象集合的嵌套关系。 函数getID(set):set ==> 编号,通过映射ID将每个集合对应唯一编号,以此保证集合元素的互异性,接口返回编号或者分配新的编号。 向量getSet:编号 ==> set,通过编号找到对应set,获取集合大小。

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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <iostream>
#include <map>
#include <vector>
#include <set>
#include <stack>
#include <iterator>
#include <algorithm>
using namespace std;
map<set<int>, int> ID;
vector<set<int>> getSet;

int getID(set<int> x)
{
if (ID.count(x))
return ID[x];
getSet.push_back(x);
return ID[x] = getSet.size() - 1;
}
int main()
{
int T;
cin >> T;
while (T--)
{
stack<int> s;
int n;
cin >> n;
while (n--)
{
char cmd[10];
cin >> cmd;
if (cmd[0] == 'P')
s.push(getID(set<int>()));
else if (cmd[0] == 'D')
s.push(s.top());
else
{
set<int> x1 = getSet[s.top()];
s.pop();
set<int> x2 = getSet[s.top()];
s.pop();
set<int> x;
if (cmd[0] == 'U')
set_union(x1.begin(), x1.end(), x2.begin(), x2.end(), inserter(x,x.begin()));
else if (cmd[0] == 'I')
set_intersection(x1.begin(), x1.end(), x2.begin(), x2.end(), inserter(x, x.begin()));
else if (cmd[0] == 'A')
{
x = x2;
x.insert(getID(x1));
}
s.push(getID(x));
}
cout << getSet[s.top()].size() << endl;
}
cout << "***" << endl;
}
return 0;
}

How to install sqli-labs on Windows 10

As a platform to help people learn SQL injection attack , sqli-labs provides challenges of 4 levels: basic challenges,advanced injections ,stacked injections and challenges ,which can be installed on the basis of XAMPP ,here is how:

  1. Although I had installed XAMPP on my computer, the version of it is not supported due to a PHP function used in sqli-labs ,so i had to reinstall a XAMPP whose php version is 5.6.33.
  2. Download the compressed file from github:https://github.com/Audi-1/sqli-labs
  3. Unzip the file in htdocs folder
  4. Visit localhost/sqli-labs-master using browser( remember to stop the proxy settings of Windows)
  5. Click Setup/reset database for labs ,if there’s no error shown on the page ,sqli-labs is successfully installed.
  6. Enjoy!

XAMPP+DVWA on Windows 10 installation guide

因为学习PHP时已经在电脑上安装了xampp,所以决定在此基础上安装dvwa(Damn Vulnerable Web Application靶场),结果error百出,重装了xampp四五遍才装好了dvwa,在此记录下过程。

  1. 百度下载xampp安装包,一键安装很方便。
  2. 下载dvwa压缩包
  3. 解压到xampp的htdocs目录(网页文件目录)
  4. 按照https://www.cnblogs.com/layerlearner/archive/2016/09/13/layerlearner.html中的错误解决办法全部操作一遍。
  5. 访问localhost/dvwa(解压目录),注意在系统设置中关闭所有代理选项,否则页面全部加载不出来!
  6. 在安装页面createdatabase
  7. 登录:默认用户admin,密码password
  8. 注意修改security level,默认impossible(不可能攻破),可以在每个漏洞页面查看不同难度等级的PHP代码,也有对应的参考答案,确实很有帮助。

总结:安装过程不复杂,但是各种配置文件、系统设置的问题容易让人找不到问题根源所在,因此比较耗费时间。每次访问页面都要关闭系统代理,比较麻烦。但是dvwa的内容很丰富,可用性很强。此外,在xampp基础上安装dvwa占用资源很少,安装完成后xampp文件夹只占半个G,相比较于虚拟机还是很有优势的。