【POJ】3283 Card Hands

字典树。

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <stack>
using namespace std; #define TRIEN 56 typedef struct Trie {
Trie *next[TRIEN];
Trie() {
for (int i=; i<TRIEN; ++i)
next[i] = NULL;
}
} Trie; Trie *root;
int nn; Trie *create(const char str[], Trie *r) {
int i = , id;
Trie *p = r, *q; if (str[i] == 'A')
id = ;
else if (str[i] == 'J')
id = ;
else if (str[i] == 'Q')
id = ;
else if (str[i] == 'K')
id = ;
else if (str[i]=='' && ++i)
id = ;
else
id = str[i] - '';
if (str[i+]=='D')
id += ;
if (str[i+]=='H')
id += ;
if (str[i+]=='S')
id += ;
if (p->next[id] == NULL) {
q = new Trie();
p->next[id] = q;
++nn;
}
p = p->next[id]; return p;
} void del(Trie *t) {
if (t == NULL)
return ;
for (int i=; i<TRIEN; ++i)
del(t->next[i]);
delete t;
} int main() {
int n, m;
string tmp;
stack<string> st;
Trie *r; while (scanf("%d", &n)!=EOF && n) {
root = new Trie();
nn = ;
while (n--) {
scanf("%d", &m);
while (m--) {
cin >> tmp;
st.push(tmp);
}
r = NULL;
while (!st.empty()) {
tmp = st.top();
st.pop();
if (r == NULL)
r = create(tmp.data(), root);
else
r = create(tmp.data(), r);
}
}
printf("%d\n", nn);
del(root);
} return ;
}
上一篇:mac OS.NE开发环境搭建


下一篇:统计"1"个数问题