Leo's cottage.

0%

『生成可信的随机数』这个小问题都有如此复杂的信任问题，更不要说复杂问题以及完整的工程的信任问题了。如，现有的操作系统提供的对于应用程序安全性全部依赖于CPU硬件提供的内核态与应用态的隔离，但是如果CPU本身是不可信的呢？Intel x86前日爆出一个隐藏的，用于修改微码的指令[4]。该指令虽然对于现有系统的安全性没有危害，但是不禁引人思考：如果CPU中有一个隐藏的指令，可以不经授权直接从用户态提升到内核态，那么现代操作系统提供的所有安全保证都会像气球一样，一触即溃。

1. Why does rust compile a simple program 5-10 times slower than gcc/clang? - stack overflow[EB/OL]. https://stackoverflow.com/questions/37362640/why-does-rust-compile-a-simple-program-5-10-times -slower-than-gcc-clang/37365065. ↩︎

2. HU Y. The taichi programming language[C/OL]//SIGGRAPH ’20: ACM SIGGRAPH 2020 Courses. Association for Computing Machinery, 2020: 1–50. https://doi.org/10.1145/3388769.3407493. ↩︎

3. Randomness 101: Lavarand in production[EB/OL]. https://blog.cloudflare.com/randomness-101-lav arand-in-production/. ↩︎

4. Undocumented x86 instructions in intel cpus that can modify microcode | hacker news[EB/OL]. https: //news.ycombinator.com/item?id=26519693. ↩︎

# 随想 有关乱码

md5 378e3ce9f0c8243012cb32cedde1ad31
sha1 b4d254b6620924a05e95bf76f5dace64edcf9086
sha256 3e9818cc4bf74e65419e72f89104dca674dcb215c1487dd25fed541cd6363d72
base32 MNUGC3THNJUWC3TMOVQW43LBBI======
base64 Y2hhbmdqaWFubHVhbm1hCg==

# Changing problem statement during contest

It is well known that we should not change the problem statement during the contest. But, we also know that a company named “Hua***” liked to change problem statement during the contest .

There were some breathtaking errors in the problem statements during the 2021 ICPC Asia Regionals Online Contest (I). So, naturally, we needed to change these statements.

For example, problem A needs change. As we all know, the word “lexicographic” means "order of the dictionary(字典序), ‘a generalization of the alphabetical order of the dictionaries to sequences of ordered symbols.’ ", but somehow the person who wrote this problem misinterpreted the word as “ascending”.

Naturally, during reasonable contests, what he needs to do is to update the testcase data, changing it from ascending order to lexicographic order, matching the problem statement, then rejudge all submissions of
this problem. But, he doesn’t think it that way. “Why changing testcase data while we can change the problem statement?” Surely he didn’t know that submitting “Wrong Answer” would add penalty(罚时) to the team’s time cost. I mean, otherwise, why would he do this？

Can you help him change all “lexicographic” to “ascending”?

## Input

The first line of input contains a integer $n$. $(1 \leq n \leq 100)$.

In the following $n$ lines, each line contains a string. It is guaranteed that the length of the string is less than $100$ characters, and the string only contains a to z.

## Output

Output updated problem statements, replace all lexicographic by ascending.

## Examples

### Example 1

#### Input

14
print
the
labels
of
all
the
busiest
nodes
in
lexicographic
order
separated
by
spaces


#### Output

print
the
labels
of
all
the
busiest
nodes
in
ascending
order
separated
by
spaces


### Example 2

#### Input

1
whoalexicographic


#### Output

whoalexicographic


In the first example, we replace “lexicographic” on the 10th line to
“ascending”.

In the second example, we do nothing because we don’t need to replace
word containing “lexicographic”.

# 题解

#include
#include
int main() {
char input_string[101];
// 注意，存储长度为100的字符串，数组长度需要是101
const char *lexicographic = "lexicographic";
int n;
scanf("%d", &n);
for(int i = 0; i < n; ++ i) {
// scanf 传入的是地址
// input_string 是数组名字，本身就是地址，不需要加
// 取地址符 &
// 了解更多： https://zh.cppreference.com/w/c/language/array
scanf("%s", input_string);
if (strcmp(input_string, lexicographic) == 0) {
// 相等，输出ascending
printf("ascending\n");
} else {
// 不相等，原样输出
printf("%s\n", input_string);
}
}
// 别忘了return 0
return 0;
}

#include
#include
using namespace std;
int main() {
int n;
cin >> n;
while (n--) {
// 定义字符串 a
string a;
// 读入
cin >> a;
// 如果等于lexicographic，就替换为ascending
if (a == "lexicographic") {
a = "ascending";
}
// 输出
cout << a << endl;
}
return 0;
}


• 可能有回民
• 可能有人不吃辣、醋
• 可能有人不吃鱼羊海鲜

# 建议麦当劳。

PS: 发现过去一年我吃了至少5000元的麦当劳。

## 背景

1. 这段代码运行结果和本地不一样
while (head->next != nullptr) {
if (cnt == m) {
cout << p->data << ' ';

// del 是代码作者定义的一个函数，里面调用了
// delete p
del(p);
cnt = 0;
} else {
cnt += 1;
if (p->next != nullptr) {
p = p->next;
} else {
}
}
}



1. 这段更离谱：
template bool ArrayList::append(T const& value) {
if(_size >= _capacity){ // border check;
cout<<"SIZE "<<_size<<" CAP "<<_capacity;
cout << " The List is overflow!" << endl;
return false;
}
_elem[_size++] = value;
}


1. 这段代码很难找出错（所以建议跳过去不看）：
#include
using namespace std;
int main() {
int n, s, m, j, tempt, q, k, flag;
int count = 1;
int data[2020];
cin >> n >> s >> m;
tempt = s;
for (int i = 0; i < n; i++) {
data[i] = i + 1;
}
for (; count <= n; count++) { // 主要看这个for循环
for (k = 1, q = 0; k <= m; q++) {
if (data[(tempt + q - 1) % n] != 0)
k++;
}
if (count != n) {
cout << data[(tempt + q - 2) % n];
cout << ' ';
} else {
cout << data[(tempt + q - 2) % n];
return 0;
}
data[(tempt + q - 2) % n] = 0;
for (j = 1, flag = 0; flag != 1; j++) {
if (data[(tempt + q + j - 2) % n] != 0)
flag = 1;
else
flag = 0;
}
tempt = (tempt + q + j - 2) % n;
}
return 0;
}


$clang++ a.cpp -Wall --std=c++17 -fsanitize=undefined$ ./a.out
6 1 1 // 这行是输入的
a.cpp:14:9: runtime error: index -1 out of bounds for type 'int [2020]'
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior a.cpp:14:9 in
1 2 3 4 5 6


## 什么是未定义行为

1. 数组越界访问
2. 解引用空指针
3. 在对象的生命周期结束后访问对象
4. 有返回类型的函数没有从return结束

• 未定义行为（undefined behavior，UB）——对程序的行为无任何限制。未定义行为的例子是数组边界外的内存访问，有符号整数溢出，空指针的解引用，在一个表达式中对同一标量多于一次的中间无序列点 (C++11 前)无序 (C++11 起)的修改，通过不同类型的指针访问对象，等等。编译器不需要诊断未定义行为（尽管许多简单情形确实会得到诊断），而且所编译的程序不需要做任何有意义的事。

template bool ArrayList::append(T const& value) {
if(_size >= _capacity){ // border check;
cout<<"SIZE "<<_size<<" CAP "<<_capacity;
cout << " The List is overflow!" << endl;
return false;
}
_elem[_size++] = value;
}


template bool ArrayList::append(T const& value) {
cout<<"SIZE "<<_size<<" CAP "<<_capacity;
cout << " The List is overflow!" << endl;
return false;
}


1. if内条件不成立的话，行为未定义。
2. 未定义行为不好，写代码的人会避免未定义行为。
3. 因此，写代码的人会保证每次调用的时候if内条件均成立。
4. 因此可以去掉if

• 如果if内条件成立，这样的执行结果自然是正确的
• 如果if内条件不成立，那么行为未定义。既然行为未定义，那任何行为都是正确的，因此我执行if内条件成立的代码也是正确的行为。

## 未定义行为的危害

llvm的issue tracker中有关这个 “bug”的讨论还在进行中。一部分人认为应该“修复”：

This means UB is a potential safety/security problem, and we really should do something about it.

All sorts of UB manifests in lots of security issues, right? Buffer overruns and the like (I guess this is a buffer overrun, of sorts).

It’s not generally that simple - deciding where/how to “recover” from UB would be pretty difficult.

The Clang-advised way to deal with this would be to compile with -fsanitize=undefined

https://godbolt.org/z/3aW69c

example.cpp:4:29: runtime error: signed integer overflow: 2147483647 + 1 cannot be represented in type ‘int’
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior example.cpp:4:29 in

# 题目描述

Mr. Leo is a famous computer scientist, he invited you and your classmates to go on a vacation in his new house in the mountains. After a day of playing, many of you felt comfortable and sighed: “It’s quite nice to live in the mountains!”. Mr. Leo agreed with that, and he decided to play a game with you, to make the travel even more interesting.

There is an unmaintained lawn next to Mr. Leo’s house, he let you design a pattern for this lawn, and submit the pattern with a computer program. The lawn is 5 meters wide and 12 meters long, you are supposed to take one square meter as one unit, use “g” to indicate grass, and use “.” to indicate bare ground. For example, an unmaintained lawn can be described as the following:

.gggg
ggggg
gg.gg
ggggg
g..gg
ggggg
ggggg
ggggg
ggggg
ggggg
gggg.
gggg.


You had no idea about the pattern. However, your friend classmate T came to encourage and team up with you. “No matter what difficulties we meet, don’t be afraid, face it with a smile!” classmate T said, “Because the best way to overcome fear is to face it. Come on!”. After listening to the encouraging speech, you felt confident and asked him what could you do to help him. Classmate T explains that he had always believed that nobody knows pattern design better than him, however, he knew nothing about computer science. So he needed you to write his program to output his wonderful design.

You accepted the task at once, and ask him when can you get the final design. classmate T said that good designs need time to perfect, and he needs to find a great way to inform the Chinese character element in his design. Just like he always says: “Normal pretty design, not ok. Pretty design with Chinese character element, ok!”. After a night of work, classmate T finally gave you his design, please write a program to output it.

classmate T’s design:

.g.g.
ggggg
.g.g.
ggggg
g...g
ggggg
g...g
ggggg
..g..
ggggg
..g..
..g..


### Input

There isn’t any input for this problem.

### Output

Output classmate T’s design.

# 题解

## 题目解法

#include
using namespace std;
int main(){
cout<<".g.g.\n"
"ggggg\n"
".g.g.\n"
"ggggg\n"
"g...g\n"
"ggggg\n"
"g...g\n"
"ggggg\n"
"..g..\n"
"ggggg\n"
"..g..\n"
"..g..\n";
return 0;
}


## 彩蛋：

c / c++ 语言中， 如果两个字符串挨着， 中间没有代码， 如上面样例一样，就会合并为一个字符串。