不会字符串处理
我大概是个废鱼
其实这道题不是学了二分(二分水题)
实际是学了字符串和对应数字的储存(趴。
#include <bits/stdc++.h> using namespace std; struct node { int s; string str; } word[100001]; bool cmp(node x, node y) { if (x.str == y.str) return x.s < y.s; return x.str < y.str; } int main() { int n; cin >> n; for (int i = 1; i <= n; i++) cin >> word[i].str >> word[i].s; sort(word + 1, word + n + 1, cmp); int m; cin >> m; string ss; for (int i = 1; i <= m; i++) { cin >> ss; int l = 1, r = n; while (l < r) { int mid = (l + r) / 2; if (word[mid].str >= ss) r = mid; else l = mid + 1; } printf("%d ", word[r].s); } }
(2k ms左右)
同一个思路,不同的复杂度……
或许这就是大佬吧
(521 ms)
#include <bits/stdc++.h> const int N = 100009, L = 12; struct Word { char s[L]; int id; bool operator<(Word b) { int x = strcmp(s, b.s); //判断两字符串是否相等 if (x) return x < 0; return id < b.id; //排序:页码从大到小 } } a[N]; int n, m; char st[L]; int main() { scanf("%d", &n); for (int i = 0; i < n; ++i) scanf("%s%d", a[i].s, &a[i].id); std::sort(a, a + n); scanf("%d", &m); for (int i = 0; i < m; ++i) { scanf("%s", st); int l = 0, r = n - 1; while (l < r) { int m = l + r >> 1; if (strcmp(a[m].s, st) >= 0) r = m; else l = m + 1; } printf("%d ", a[r].id); } return 0; }
p.s.map会慢很hinnnnnnnnnnnn多 (2514 ms)