[IPUOJ22604]查字典 (二分)

不会字符串处理

我大概是个废鱼 

 

 

其实这道题不是学了二分(二分水题)

实际是学了字符串和对应数字的储存(趴。

#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);
    }
}
(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\n", a[r].id); } return 0; }

p.s.map会慢很hinnnnnnnnnnnn多 (2514 ms)

posted @ 2019-08-27 11:26  QUEKI嶺冬  阅读(131)  评论(0编辑  收藏  举报
/*! Color themes for Google Code Prettify | MIT License | github.com/jmblog/color-themes-for-google-code-prettify */ .pln{color:#4d4d4c}ol.linenums{margin-top:0;margin-bottom:0;color:#8e908c}li.L0,li.L1,li.L2,li.L3,li.L4,li.L5,li.L6,li.L7,li.L8,li.L9{padding-left:1em;background-color:#fff;list-style-type:decimal!important;}@media screen{.str{color:#718c00}.kwd{color:#8959a8}.com{color:#8e908c}.typ{color:#4271ae}.lit{color:#f5871f}.pun{color:#4d4d4c}.opn{color:#4d4d4c}.clo{color:#4d4d4c}.tag{color:#c82829}.atn{color:#f5871f}.atv{color:#3e999f}.dec{color:#f5871f}.var{color:#c82829}.fun{color:#4271ae}} /*下面是我设置背景色,字体大小和字体*/ .cnblogs-markdown code{ background:#fff!important; } .cnblogs_code,.cnblogs_code span,.cnblogs-markdown .hljs{ font-size:16px!important; } .syntaxhighlighter a, .syntaxhighlighter div, .syntaxhighlighter code, .syntaxhighlighter table, .syntaxhighlighter table td, .syntaxhighlighter table tr, .syntaxhighlighter table tbody, .syntaxhighlighter table thead, .syntaxhighlighter table caption, .syntaxhighlighter textarea { font-size: 16px!important; } .cnblogs_code, .cnblogs_code span, .cnblogs-markdown .hljs{ font-family:consolas, "Source Code Pro", monaco, monospace !important; } //以上是代码高亮 /* 文字特效 */