intmain(int argc, char **argv){ int port = 27015; char *ip = "0.0.0.0"; for (int i = 1; i < argc;) { if (argv[i] == string("--port")) { port = atoi(argv[i + 1]); i += 2; continue; } if (argv[i] == string("--ip")) { ip = argv[i + 1]; i += 2; continue; } i++; }
$ ./run-server.sh do 1 1 finished do 2 2 finished do 3 3 finished do 4 4 finished .................. do 126 126 finished do 127 127 finished do 128 128 finished do 129 # block here 129 finished do 130 130 finished do 131 131 finished do 132 132 finished .................. do 255 255 finished do 256 256 finished
select ret: 0 if can read data from fd: 0 is false hello world select ret: 1 if can read data from fd: 0 is true success read 5 bytes, buf: hello select ret: 1 if can read data from fd: 0 is true success read 5 bytes, buf: worl select ret: 1 if can read data from fd: 0 is true success read 2 bytes, buf: d
select ret: 0 if can read data from fd: 0 is false
for (int i = 0; i < 5; i++) { fd_set rs_temp = rs; timeval tv = {.tv_sec=2, .tv_usec=0}; int ret = select(STDIN_FILENO + 1, &rs_temp, NULL, NULL, &tv); if (ret == -1) { fprintf(stderr, "select ret: %d, errno: %d, str: %s\n", ret, errno, strerror(errno)); return1; } printf("select ret: %d\n", ret); printf("if can read data from fd: %d is %s\n", STDIN_FILENO, FD_ISSET(STDIN_FILENO, &rs_temp) ? "true" : "false"); }
return0; }
执行结果:
1 2 3 4 5 6 7 8 9 10 11 12 13
n: -1, errno: 35, str: Resource temporarily unavailable select ret: 0 if can read data from fd: 0 is false select ret: 0 if can read data from fd: 0 is false select ret: 0 if can read data from fd: 0 is false select ret: 0 if can read data from fd: 0 is false select ret: 0 if can read data from fd: 0 is false
在本机的macOs上$ uname -a的结果为Darwin localhost 19.0.0 Darwin Kernel Version 19.0.0: Thu Oct 17 16:17:15 PDT 2019; root:xnu-6153.41.3~29/RELEASE_X86_64 x86_64
The behavior of these macros is undefined if a descriptor value is less than zero or greater than or equal to FD_SETSIZE, which is normally at least equal to the maximum number of descriptors supported by the system.2
$ ./dup file opened, fd[3] cur_pos of 3 is 0 cur_pos of 4 is 0 write msg[hello] to fd[3], n[5] cur_pos of 3 is 5 cur_pos of 4 is 5 write msg[hello] to fd[4], n[5] cur_pos of 3 is 10 cur_pos of 4 is 10 write msg[world] to fd[3], n[5] cur_pos of 3 is 15 cur_pos of 4 is 15 write msg[world] to fd[4], n[5] cur_pos of 3 is 20 cur_pos of 4 is 20 write msg[cpp] to fd[3], n[3] cur_pos of 3 is 23 cur_pos of 4 is 23 write msg[cpp] to fd[4], n[3] cur_pos of 3 is 26 cur_pos of 4 is 26
pair<string, string> get_kv(const string &str){ unsignedlong pos = 0; for (int i = 0; i < str.size(); i++) { if (str[i] == ']') { pos = i; break; } } auto k = str.substr(1, pos - 1); auto v = str.substr(pos + 2, str.size() - pos - 2); returnmake_pair(k, v); }
intmain(int argc, char **argv){ ios::sync_with_stdio(false); string s; while (true) { getline(cin, s); if (s == "@END@") break; auto kv = get_kv(s); key2val.insert(kv.first, kv.second); val2key.insert(kv.second, kv.first); } int n; cin >> n; cin.get(); for (int i = 0; i < n; i++) { getline(cin, s); if (s[0] == '[') { auto val = key2val.get(s.substr(1, s.size() - 2)); if (val == nullptr) { cout << "what?" << "\n"; } else { cout << *val << "\n"; } } else { auto key = val2key.get(s); if (key == nullptr) { cout << "what?" << "\n"; } else { cout << *key << "\n"; } } } return0; }
/** [expelliarmus] the disarming charm [rictusempra] send a jet of silver light to hit the enemy [tarantallegra] control the movement of one's legs [serpensortia] shoot a snake out of the end of one's wand [lumos] light the wand [obliviate] the memory charm [expecto patronum] send a Patronus to the dementors [accio] the summoning charm @END@ 4 [lumos] the summoning charm [arha] take me to the sky light the wand accio what? what? */
intkth(int k){ int span = 0; ListNode *p = header; for (int i = level - 1; i >= 0; i--) { while (p->levels[i].forward && span + p->levels[i].span <= k) { span += p->levels[i].span; p = p->levels[i].forward; } } return p->val; }
intrank(int x){ int ans = 0; ListNode *p = header; for (int i = level - 1; i >= 0; i--) { while (p->levels[i].forward && p->levels[i].forward->val < x) { ans += p->levels[i].span; p = p->levels[i].forward; } } return ans; }
} skip_list;
intmain(int argc, char **argv){ int n; while (scanf("%d", &n) != EOF) { skip_list.init(); for (int i = 0; i < n; i++) { char op[2]; int x; scanf("%s%d", op, &x); if (op[0] == 'I') { skip_list.insert(x); } elseif (op[0] == 'D') { skip_list.remove(x); } elseif (op[0] == 'K') { if (x > skip_list.length) { puts("invalid"); } else { printf("%d\n", skip_list.kth(x)); }; } elseif (op[0] == 'C') { printf("%d\n", skip_list.rank(x)); } } } return0; }
/** 8 I -1 I -1 I 2 C 0 K 2 D -1 K 1 K 2 1 2 2 invalid */
structManacher { staticconstint $ = -1; int len_b; int b[2 * MAX_N + 1]; int p[2 * MAX_N + 1];
voidinit(char a[], int n){ len_b = 2 * n + 1; for (int i = 0; i < n; i++) { b[2 * i] = $; b[2 * i + 1] = a[i]; } b[len_b - 1] = $;
int l = 0; int r = -1; for (int i = 0; i < len_b; i++) { if (i > r) { p[i] = 1; } else { p[i] = min(p[r - i + l], r - i + 1); } while (i + p[i] < len_b && i - p[i] >= 0 && b[i + p[i]] == b[i - p[i]]) { p[i]++; } if (i + p[i] - 1 > r) { l = i - p[i] + 1; r = i + p[i] - 1; } } }
intmax_length(){ int ans = 1; for (int i = 0; i < len_b; i++) { int temp = p[i]; if (b[i] == $ && p[i] % 2 == 1) temp--; if (b[i] != $ && p[i] % 2 == 0) temp--; ans = max(ans, temp); } return ans; } } manacher;
char s[MAX_N];
intmain(int argc, char **argv){ int cas = 1; while (true) { scanf("%s", s); if (s == string("END")) break; int len = strlen(s); manacher.init(s, len); printf("Case %d: %d\n", cas++, manacher.max_length()); } return0; }
/** abcbabcbabcba abacacbaaaab END Case 1: 13 Case 2: 6 */
structManacher { staticconstint $ = -1; int b[2 * MAX_N + 1]; int len_b; int p[2 * MAX_N + 1];
voidinit(int a[], int n){ len_b = 2 * n + 1; for (int i = 0; i < n; i++) { b[2 * i] = $; b[2 * i + 1] = a[i]; } b[len_b - 1] = $;
int l = 0; int r = -1; for (int i = 0; i < len_b; i++) { if (i > r) { p[i] = 1; } else { p[i] = min(p[l + r - i], r - i + 1); } while (0 <= i - p[i] && i + p[i] < len_b && b[i - p[i]] == b[i + p[i]]) { if (b[i - p[i]] != $ && i - p[i] + 2 <= i && b[i - p[i]] > b[i - p[i] + 2]) break; p[i]++; } if (i + p[i] - 1 > r) { l = i - p[i] + 1; r = i + p[i] - 1; } } }
intmax_length(){ int ans = 0; for (int i = 0; i < len_b; i++) ans = max(ans, p[i]); return ans - 1; } } manacher;
int a[MAX_N];
intmain(int argc, char **argv){ int T; scanf("%d", &T); while (T--) { int n; scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%d", &a[i]); manacher.init(a, n); printf("%d\n", manacher.max_length()); } return0; }
structManacher { staticconstint $ = -1; int b[2 * MAX_N + 1]; int len_b; int p[2 * MAX_N + 1];
voidinit(char a[], int n){ len_b = 2 * n + 1; for (int i = 0; i < n; i++) { b[2 * i] = $; b[2 * i + 1] = a[i]; } b[len_b - 1] = $;
int l = 0; int r = -1; for (int i = 0; i < len_b; i++) { if (i > r) { p[i] = 1; } else { p[i] = min(p[l + r - i], r - i + 1); } while (0 <= i - p[i] && i + p[i] < len_b && b[i - p[i]] == b[i + p[i]]) { p[i]++; } if (i + p[i] - 1 > r) { l = i - p[i] + 1; r = i + p[i] - 1; } } }
pair<int, int> max_length(){ int ans = 0; int l = -1; for (int i = 0; i < len_b; i++) { if (p[i] - 1 >= 2 && p[i] - 1 > ans) { ans = p[i] - 1; l = (i - p[i] + 1 + 1) / 2; } } returnmake_pair(ans, l); } } manacher;
char a[MAX_N]; char c[2];
intmain(int argc, char **argv){ while (scanf("%s", c) != EOF) { int delta = c[0] - 'a'; scanf("%s", a); int len = strlen(a); for (int i = 0; i < len; i++) { a[i] = (a[i] - 'a' - delta + 26) % 26 + 'a'; } manacher.init(a, len); pair<int, int> ans = manacher.max_length(); if (ans.first < 2) puts("No solution!"); else { printf("%d %d\n", ans.second, ans.second + ans.first - 1); for (int i = ans.second, j = 0; j < ans.first; j++) { printf("%c", a[i + j]); } puts(""); } } return0; }
structManacher { staticconstint $ = -1; int b[2 * MAX_N + 1]; int len_b; int p[2 * MAX_N + 1];
voidinit(char a[], int n){ len_b = 2 * n + 1; for (int i = 0; i < n; i++) { b[2 * i] = $; b[2 * i + 1] = a[i]; } b[len_b - 1] = $;
int l = 0; int r = -1; for (int i = 0; i < len_b; i++) { if (i > r) { p[i] = 1; } else { p[i] = min(p[l + r - i], r - i + 1); } while (0 <= i - p[i] && i + p[i] < len_b && b[i - p[i]] == b[i + p[i]]) { p[i]++; } if (i + p[i] - 1 > r) { l = i - p[i] + 1; r = i + p[i] - 1; } } }
intget_ans(){ int ans = 2 * len_b - 1; for (int i = len_b / 2; i < len_b; i++) { if (i + p[i] - 1 == len_b - 1) { int temp = (len_b + (i - len_b / 2) * 2) / 2; ans = min(ans, temp); } } return ans; } } manacher;
char a[MAX_N];
intmain(int argc, char **argv){ int T; scanf("%d", &T); for (int cas = 1; cas <= T; cas++) { scanf("%s", a); int len = strlen(a); manacher.init(a, len); printf("Case %d: %d\n", cas, manacher.get_ans()); } return0; }
/** 4 bababababa pqrs madamimadam anncbaaababaaa Case 1: 11 Case 2: 7 Case 3: 11 Case 4: 19 */
int dist[MAX_N]; bool vis[MAX_N]; priority_queue<Node, vector<Node>, greater<Node>> pq;
voidinit(int n, int s){ for (int i = 0; i <= n; i++) { dist[i] = INF; vis[i] = false; } dist[s] = 0; while (!pq.empty()) pq.pop(); pq.push(Node(s, dist[s])); }
voidwork(const Graph &g, int s){ init(g.n, s); while (!pq.empty()) { Node node = pq.top(); pq.pop(); int u = node.u; if (vis[u]) continue;
intmain(int argc, char **argv){ int n, m, s, t; while (scanf("%d%d%d%d", &n, &m, &s, &t) != EOF) { graph.init(n); for (int i = 0; i < m; i++) { int u, v, c; scanf("%d%d%d", &u, &v, &c); graph.add(u, v, c); graph.add(v, u, c); } dijkstra_premium.work(graph, s); printf("%d\n", dijkstra_premium.dist[t]); } return0; }
structHash { constunsignedlonglong KEY = 137; unsignedlonglong h[N], p[N]; int len; voidinit(constint a[], int len){ this->len = len; p[0] = 1; for (int i = 1; i <= len; i++) p[i] = p[i - 1] * KEY; h[len] = 0; for (int i = len - 1; i >= 0; i--) h[i] = h[i + 1] * KEY + a[i]; } unsignedlonglongget(int l, int r){ return h[l] - h[r + 1] * p[r - l + 1]; } unsignedlonglongget_for_len(int l, int len){ return h[l] - h[l + len] * p[len]; }
} hp, hs;
int a[N];
intget_ans(){ for (int i = 0; i + hs.len - 1 < hp.len; i++) if (hs.get_for_len(0, hs.len) == hp.get_for_len(i, hs.len)) return i + 1; return-1; }
intmain(int argc, char **argv){ int T; scanf("%d", &T); while (T--) { int n, m; scanf("%d%d", &n, &m); for (int i = 0; i < n; i++) scanf("%d", &a[i]); hp.init(a, n); for (int i = 0; i < m; i++) scanf("%d", &a[i]); hs.init(a, m); printf("%d\n", get_ans()); } return0; }