不会字符串处理
我大概是个废鱼
其实这道题不是学了二分(二分水题)
实际是学了字符串和对应数字的储存(趴。
#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\n", word[r].s); } }