HDU 1800 Flying to the Mars // Trie

来源:互联网 发布:监控组态软件 编辑:程序博客网 时间:2024/06/05 22:45

题目描述

HDU 1800 Flying to the Mars

解题思路

题目大意:
每个士兵都有各自的能力值,能力强的可以教能力弱的人 “骑扫帚”,两人用的是同一把扫帚 (若A B 用 一把, B C 用一把, 则说明 A B C用同一把)。另外题目要求每个士兵最多只能有一个学生或最多只能有一个老师。最后输出最少需要多少扫帚~

不难知道,若n的士兵能力值各不相同的话,则他们只需要一把扫帚。因此,最终扫帚的数目等于 有相同能力值的人数中 取最大的那个即可。
比如能力值 1 2 3 4 5 只要1把; 而 2 2 5 5 5 则需要 3把。

另外还需注意的是,能力值的输入是有前导“0”的,即003 和 3 应认为是相同的。

参考代码

//**********************************************//  Author: @xmzyt1996//  Date:   2015-10-21//  Name:   HDU 1800.cpp//**********************************************#include <cstdio>#include <cmath>#include <cctype>#include <cstring>#include <cstdlib>#include <iostream>#include <algorithm>#include <string>#include <bitset>#include <vector>#include <stack>#include <queue>#include <map>using namespace std;#define clr(a, b) memset(a, b, sizeof(a))#define rep(i, a, b) for(int i = a; i < b; ++i)#define per(i, a, b) for(int i = a; i >= b; --i)#define pt(x) cout << #x << " = " << x << endl#define ps puts("debug~~")#define all(x) (x).begin(),(x).end()#define mp make_pair#define pb push_backtypedef __int64 ll;typedef pair<int, int> pii;const double pi = acos(-1.0);const double eps = 1e-6;const int inf = 0x3f3f3f3f;const int MOD = 1e9+7;const int MAX_N = 3010;struct Trie {    Trie* next[10];    int cnt;} node[MAX_N];int num, ans;char s[35];void init () {    num = ans = 0;    clr(node, 0);}int getid(char ch) { return ch - '0'; }char* check (char* s) { // 除掉前导“0”    int i = 0;    for (; s[i+1] && s[i] == '0'; ++i);    return s+i;}void insert (char* s) {    Trie* head = &node[0];    while (*s) {        int id = getid(*s++);        if (head->next[id] == NULL)            head->next[id] = &node[++num];        head = head->next[id];    }    (head->cnt)++;    if ((head->cnt) > ans) ans = (head->cnt);}int main() {    int n;    while (~scanf("%d", &n)) {        init();        rep (i, 0, n) {            clr(s, 0);            scanf("%s", s);            insert(check(s));        }        printf("%d\n", ans);    }    return 0;}
0 0
原创粉丝点击