【字符串】【Trie】lcp 题解
来源:互联网 发布:java做爬虫 编辑:程序博客网 时间:2024/06/07 08:40
求
建立Trie树暴力跑一下,点上用组合数计算结果
#include <iostream>#include <sstream>#include <cstdio>#include <cstdlib>#include <ctime>#include <cmath>#include <cctype>#include <cstring>#include <algorithm>#include <map>#ifndef WIN32#define Auto "%lld"#else#define Auto "%I64d"#endifusing namespace std;typedef class TrieNode { public: map<int, TrieNode*> next; int val; int ends; TrieNode():val(0), ends(0) { }}TrieNode;#define lim 1000005TrieNode pool[lim];TrieNode* top = pool;inline TrieNode* newnode() { return top++;}typedef class Trie { public: TrieNode* root; Trie():root(newnode()) {} inline void insert(char *str) { TrieNode* p = root; for(char *ch = str; *ch; ch++) { int c = *ch - 'a'; p->val++; if(!p->next[c]) p->next[c] = newnode(); p = p->next[c]; } p->val++; p->ends++; }}Trie;long long res = 0;typedef map<int, TrieNode*>::iterator mii;void getRes(TrieNode*& node, int len) { for(mii i = node->next.begin(); i != node->next.end(); i++) for(mii j = i; j != node->next.end(); j++) if(j != i) res += i->second->val * 1LL * j->second->val * len; for(mii i = node->next.begin(); i != node->next.end(); i++) getRes(i->second, len + 1); if(node->ends) res += (node->val - node->ends) * 1LL * node->ends * len, res += node->ends * 1LL * (node->ends - 1) / 2 * len;}int n;char buf[lim];Trie trie;inline void init() { scanf("%d", &n); for(int i = 1; i <= n; i++) scanf("%s", buf), trie.insert(buf);}inline void solve() { getRes(trie.root, 0); printf(Auto, res);}int main() { init(); solve(); return 0;}
阅读全文
0 0
- 【字符串】【Trie】lcp 题解
- lcp计数 (Trie)
- codeforces367(字符串hash求lcp比较大小,Trie解最大异或问题)
- Trie(字典树):大LCP
- POJ 2001 Shortest Prefixes 【LCP(Trie)】
- zoj3818 字符串hash 求lcp
- 【Hash求LCP】HDU4080[Stammering Aliens]题解
- 【字符串】Trie
- LCP
- LCP
- 【Trie树】Poj2513题解
- hihocode Trie树题解
- 【hdu 5635】LCP Array 中文题意&题解&代码(C++)
- BestCoder Round #74 (div.2) ~' T1. LCP Array ' 题解
- 准备食物(trie) 题解+代码
- 字符串匹配-trie自动机
- 字符串处理----Trie树
- 第一字符串(Trie&&Topology)
- 用C语言写的一个简单的将数字金钱转换为文字
- java使用tar算法压缩解压缩文件、数据流、byte[]字节数组
- 36.笔记go语言——函数
- data augmentation and dropout
- 37.笔记go语言——包
- 【字符串】【Trie】lcp 题解
- 38.笔记go语言——进阶
- isabellezhou 图解二叉树遍历(递归调用)
- 39.笔记go语言——接口
- 【Linux入门学习之】vi/vim编辑器必知必会
- 机器学习BP神经网络,任意拓扑结构 (C++)
- 40.笔记go语言——并发
- 51nod 1534棋子游戏(分析)
- 41.笔记go语言——通讯