uva 1519 - Dictionary Size(字典树)
题目链接:uva 1519 - Dictionary Size
题目大意:给出n个字符串组成的字典。如今要加入新的单词,从已有单词中选出非空前缀和非空后缀,组成新单词。
问说能组成多少个单词。
解题思路:建立一棵前缀树和一棵后缀树。有多少节点即为有多少个前缀。扣除中间的部分就可以加上长度为1的字符串就可以。
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 400005;
const int sigma_size = 26;
typedef long long ll;
struct Tire {
int sz, g[maxn][sigma_size];
int c[sigma_size];
void init ();
int idx(char ch);
void insert(char* s);
}pre, suf;
int main () {
int N;
while (scanf("%d", &N) == 1 && N) {
char word[45];
int vis[sigma_size];
memset(vis, 0, sizeof(vis));
pre.init();
suf.init();
for (int i = 0; i < N; i++) {
scanf("%s", word);
int n = strlen(word);
if (n == 1)
vis[word[0] - 'a'] = 1;
pre.insert(word);
reverse(word, word + n);
suf.insert(word);
}
ll ans = (ll)(pre.sz - 1) * (suf.sz - 1);
for (int i = 0; i < sigma_size; i++)
ans -= (ll)pre.c[i] * suf.c[i] - vis[i];
printf("%lld\n", ans);
}
return 0;
}
void Tire::init () {
sz = 1;
memset(g[0], 0, sizeof(g[0]));
memset(c, 0, sizeof(c));
}
int Tire::idx (char ch) {
return ch - 'a';
}
void Tire::insert(char* s) {
int u = 0, n = strlen(s);
for (int i = 0; i < n; i++) {
int v = idx(s[i]);
if (g[u][v] == 0) {
memset(g[sz], 0, sizeof(g[sz]));
g[u][v] = sz++;
if (i)
c[v]++;
}
u = g[u][v];
}
}
uva 1519 - Dictionary Size(字典树)的更多相关文章
- UVA Phone List (字典树)(查询是否有前缀或自身是其他的前缀)
Phone List Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16341 Accepted: 5228 Descr ...
- UVA5913 Dictionary Sizes(字典树)(转载)
题目大意:给出n个旧单词,要从这n个旧单词中构造新单词.构造条件是 S = Sa + Sb,其中Sa为某个旧单词的非空前缀,Sb为某个单词的非空后缀.求所有的新单词和旧单词中有多少个不同的单词. 思路 ...
- UVA 12333 大数,字典树
题意:给一个数字,看他最小是第几个菲波那切数列的前缀. 分析: 大数模板就是吊哦. 将菲波那切数列前500个数字放到字典树上.注意插入的时候不能像普通一样,只在尾节点处标记,而是一路标记下去. #in ...
- UVA 11732 - strcmp() Anyone? 字典树
传送门:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&a ...
- UVA 11732 strcmp() Anyone? (压缩版字典树)
题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...
- UVA - 11488 字典树
题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...
- UVA 11488 Hyper Prefix Sets (字典树)
题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...
- uva 11488 - Hyper Prefix Sets(字典树)
H Hyper Prefix Sets Prefix goodness of a set string is length of longest common prefix*number of str ...
- UVA - 12333 字典树+大数
思路:用字典树将前40个数字记录下来,模拟大数加法即可. AC代码 #include <cstdio> #include <cmath> #include <algori ...
随机推荐
- 建立一个 Openshift "Do-It-Yourself" 应用
建立一个 Openshift "Do-It-Yourself" 应用 Openshift 的 "Do-It-Yourself" 就是自己可以编译定制 WEB ...
- Mongodb总结6-数据库启动、停止、备份等命令
#启动Mongodb默认启动,需要在/data/db,Windows下对应的目录是Mongod.exe所在磁盘分区的根目录,例如Mongodb存放在D:/Mongodb,那么对应的路径就是D:/dat ...
- winedt (latex 编译器)解决中文的问题(CJK & CTEX)
主要是导入相关的库支持: 1. CJK \usepackage{CJK}:CJK,是中日韩的英文首字母的组合,处理中文需要先导入这个包: \begin{CJK*}{GBK}{song}:默认句式,表示 ...
- Des 加密cbc模式 padding
using System; using System.Collections.Generic; using System.Linq; using System.Net.Http; using Syst ...
- Codeforces Round #100 E. New Year Garland (第二类斯特林数+dp)
题目链接: http://codeforces.com/problemset/problem/140/E 题意: 圣诞树上挂彩球,要求从上到下挂\(n\)层彩球.已知有\(m\)种颜色的球,球的数量不 ...
- Oracle中暂时表空间的清理
作者:iamlaosong Oracle暂时表空间主要用来做查询和存放一些缓冲区数据. 暂时表空间消耗的主要原因是须要对查询的中间结果进行排序.暂时表空间的主要作用: 索引create或rebuild ...
- [D3] Create DOM Elements with D3 v4
Change is good, but creating from scratch is even better. This lesson shows you how to create DOM el ...
- 算法-对分查找(二分查找)C++实现
这个是个主要的查找算法.由于仅仅是把数读入就须要(N)的时间量,因此我们在说这类问题的时候都是如果读入过的. 在算法经常使用的时间.将问题缩小为一部分(大约1/2),那么我们就觉得这个算法是O(log ...
- 【AtCoder ABC 075 B】Minesweeper
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 模拟,把#换成1 八个方向加一下就好. [代码] #include <bits/stdc++.h> using name ...
- imresize() 函数——matlab
功能:改变图像的大小. 用法:B = imresize(A,m)B = imresize(A,m,method)B = imresize(A,[mrows ncols],method) B = imr ...