题目链接: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(字典树)的更多相关文章

  1. UVA Phone List (字典树)(查询是否有前缀或自身是其他的前缀)

    Phone List Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16341   Accepted: 5228 Descr ...

  2. UVA5913 Dictionary Sizes(字典树)(转载)

    题目大意:给出n个旧单词,要从这n个旧单词中构造新单词.构造条件是 S = Sa + Sb,其中Sa为某个旧单词的非空前缀,Sb为某个单词的非空后缀.求所有的新单词和旧单词中有多少个不同的单词. 思路 ...

  3. UVA 12333 大数,字典树

    题意:给一个数字,看他最小是第几个菲波那切数列的前缀. 分析: 大数模板就是吊哦. 将菲波那切数列前500个数字放到字典树上.注意插入的时候不能像普通一样,只在尾节点处标记,而是一路标记下去. #in ...

  4. UVA 11732 - strcmp() Anyone? 字典树

    传送门:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&a ...

  5. UVA 11732 strcmp() Anyone? (压缩版字典树)

    题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...

  6. UVA - 11488 字典树

    题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...

  7. UVA 11488 Hyper Prefix Sets (字典树)

    题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...

  8. uva 11488 - Hyper Prefix Sets(字典树)

    H Hyper Prefix Sets Prefix goodness of a set string is length of longest common prefix*number of str ...

  9. UVA - 12333 字典树+大数

    思路:用字典树将前40个数字记录下来,模拟大数加法即可. AC代码 #include <cstdio> #include <cmath> #include <algori ...

随机推荐

  1. MySQLSocketPHPvimApache

    原文:http://www.blogjava.net/asenyifei/articles/82575.html 自己装了一个MYSQL客户端,但却发现出现这样的错误: Can't connect t ...

  2. CF #261 div2 D. Pashmak and Parmida&#39;s problem (树状数组版)

    Parmida is a clever girl and she wants to participate in Olympiads this year. Of course she wants he ...

  3. C Tricks(十八)—— 整数绝对值的实现

    为正还是为负:(对 int 类型而言,第一位为符号位,其余为数值,则右移 31 位,再与 1 求与) 如果为正 ⇒ 返回原值 如果为负 ⇒ 对其二进制形式各位取反 + 1 int abs(int x) ...

  4. Android 继承framelayout,实现ScrollView 和 HorizontalScrollView 的效果

    有些项目,需要让控件或者布局进行水平和垂直同时能拖拽,当然,ScrollView 和 HorizontalScrollView 的结合写法是一种写法.但是,这么写用户体验效果不佳,会有迟钝感,因此推荐 ...

  5. BlobTracker

    Blob分析介绍 分类: CV相关2012-11-04 11:25 1929人阅读 评论(5) 收藏 举报 Blob翻译成中文,是“一滴”,“一抹”,“一团”,“弄脏”,“弄错”的意思.在计算机视觉中 ...

  6. Swift iOS tableView static cell动态计算高度

    TableView是iOS开发中经常使用的组件.有些表格由于UILabel包括的文本字数不一样,须要显示的高度也会不同,因此须要动态计算static cell的高度.我用的是static cell,注 ...

  7. POJ 3723 Conscription MST

    http://poj.org/problem?id=3723 题目大意: 需要征募女兵N人,男兵M人,没征募一个人需要花费10000美元,但是如果已经征募的人中有一些关系亲密的人,那么可以少花一些钱, ...

  8. Microsoft Bot Framework 链接至微信公共号

    如何将 Microsoft Bot Framework 链接至微信公共号   说到 Microsoft Bot Framework 其实微软发布了已经有一段时间了,有很多朋友可能还不太了解,微软Bot ...

  9. MySQL—Install/Remove of the Service Denied

    在Windos7下通过命令"mysqld --install"安装MySQL数据库时出现了"Install/Remove of the Service Denied&qu ...

  10. PHP unlink() 函数(删除文件)

    PHP unlink() 函数(删除文件) 一.总结 unlink() 函数删除文件. 1.实例 $file = "test.txt"; if (!unlink($file)) 2 ...