bzoj11741174: [Balkan2007]Toponyms 字典树

来源:互联网 发布:淘宝视频收费标准 编辑:程序博客网 时间:2024/05/21 22:51
貌似是裸的。。。。。。上vfk大神的码。
#include <iostream>#include <cstdio>using namespace std;inline int getint(char *p){while ('0' > *p || *p > '9')p++;int res = *p++ - '0';while ('0' <= *p && *p <= '9')res = res * 10 + *p++ - '0';return res;}template <class T>inline void relax(T &a, const T &b){if (b > a)a = b;}const int MaxNNode = 3000000;inline bool isLetter(char c){return ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z') || c == ' ';}struct node{node *son, *brother;int size;char val;inline node *next(char c){for (node *i = son; i; i = i->brother)if (i->val == c)return i;return NULL;}};node pool[MaxNNode];node *tail;node *root;inline node *addNext(node *p, char c){node *q;if (tail != pool + MaxNNode)q = tail++;elseq = new node;q->son = NULL, q->size = 0;q->val = c, q->brother = p->son, p->son = q;return q;}inline void trie_init(){tail = pool;root = tail++;}inline char *trie_insert(char *s){node *p = root;root->size++;while (isLetter(*s)){node *next = p->next(*s);if (!next)next = addNext(p, *s);p = next;p->size++;s++;}return s;}int best = 0;void dfs(node *p, const int &depth){relax(best, p->size * depth);for (node *i = p->son; i; i = i->brother)dfs(i, depth + 1);}int main(){//freopen("1174.in", "r", stdin);//freopen("1174.out", "w", stdout);const int BufferSize = 10 * 1024 * 1024;static char buffer[BufferSize + 1];int bufferLen = fread(buffer, 1, BufferSize, stdin);buffer[bufferLen] = '\0';char *pRead = buffer;int n = getint(pRead);trie_init();for (int i = 0; i < n; i++){while (!isLetter(*pRead))pRead++;pRead = trie_insert(pRead);}dfs(root, 0);cout << best << endl;return 0;}

0 0
原创粉丝点击