ARST打卡第130周[130/521]

Algorithm

lc500 键盘行

很简单,先建立每个字母对应的行数映射,然后直接遍历每个字符串,然后根据第一个所在的键盘行,
去判断剩下的键盘行是否在同一行,是的话加入到ans的vector中

注意大小写都一样…因为没看清,导致卡了15分钟

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
// g++ test.cpp -o test.out -std=c++11
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, a, b) for(int i = int(a); i <= int(b); ++i)
#define per(i, b, a) for(int i = int(b); i >= int(a); --i)
#define mem(x, y) memset(x, y, sizeof(x))
#define SZ(x) x.size()
#define mk make_pair
#define pb push_back
#define fi first
#define se second
const ll mod=1000000007;
const int inf = 0x3f3f3f3f;
inline int rd(){char c=getchar();int x=0,f=1;while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}return x*f;}
inline ll qpow(ll a,ll b){ll ans=1%mod;for(;b;b>>=1){if(b&1)ans=ans*a%mod;a=a*a%mod;}return ans;}

class Solution {
public:
vector<string> findWords(vector<string>& words) {
int word_to_line[128];
string str[] = {"qwertyuiopQWERTYUIOP", "asdfghjklASDFGHJKL", "zxcvbnmZXCVBNM"};
vector<string> lines(str, str+3);
int sz = lines.size();
for (int i = 0; i < sz; i++) {
for (char x : lines[i]) {
word_to_line[x] = i;
}
}
// for (int i = 0; i < 128; i++) {
// printf("%c %d\n", i, word_to_line[i]);
// }

vector<string> ans;
int param_word_sz;
bool flag;
for (auto word : words) {
param_word_sz = word.size();
flag = 1;
// puts(word.c_str());
for (int i = 0; i < param_word_sz - 1; i++) {
if (word_to_line[word[i]] !=
word_to_line[word[i + 1]]) {
// 尴尬,没有处理大小写,导致多调试了15分钟
// printf("word not in one line %c, %c\n",
// word[i], word[i + 1]);
flag = 0;
break;
}
}
if (flag) {
ans.push_back(word);
}
}

return ans;
}
};

int main(){
Solution sol;
string words_str[] = {"Hello","Alaska","Dad","Peace"};
vector<string> words(words_str, words_str + 4);
vector<string> ans;
ans = sol.findWords(words);
for (auto x : ans) {
puts(x.c_str());
}


return 0;
}

Review

readv的文档

Tips

C语言:在文件的指定位置实现局部修改,而无需重写文件的其他部分

Share

找到文本中某个字符串的上下几行中的含有的其他指定字符串的方法

eg:找result文件中,compound的上面十行里有create的文本块,并且向下显示10行(-B向上,-A向下)
grep compound_end result -B 10 | grep create -A 10

iov数组中间加值

iovec的结构

首先观察iovec的存储结构,就知道iovec数组一旦填充了值之后,就无法在iovec的base的尾部添加字符了
因为这样会破坏后面的len,所以想要加值,就要重新生成一个iovec数组然后复制,或者是在尾部添加

iovec结构

尾部添加演示

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
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/uio.h>

int main() {
struct iovec iov[4];
ssize_t nr;
int fd, i;

char *buf[] = {"The term buccaneer comes from the word boucan.\n",
"A boucan is a wooden frame used for cooking meat.\n",
"Buccaneer is the West Indies name for a pirate.\n"};

fd = open("buccaneer.txt", O_WRONLY | O_CREAT | O_TRUNC);
if (fd == -1) {
perror("open");
return 1;
}

for (i = 0; i < 3; i++) {
iov[i].iov_base = buf[i];
iov[i].iov_len = strlen(buf[i]);
}
char buf_tmp[2] = "2";
buf_tmp[0] = '1';
iov[3].iov_base = buf_tmp;
iov[3].iov_len = strlen(buf_tmp);
printf("len is %d\n", iov[3].iov_len);

nr = writev(fd, iov, 4);
if (nr == -1) {
perror("writev");
return 1;
}
printf("wrote %d bytes\n", nr);

if (close(fd)) {
perror("close");
return 1;
}
return 0;
}