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 ...
随机推荐
- Arch Linux下配置Samba
本文记录笔者配置Samba的过程,供用于自用. sudo pacman -S samba sudo vim /etc/samba/smb.conf 添加以下内容 [global] dns pro ...
- Windows 共享无线上网 无法启动ICS服务解决方法(WIN7 ICS服务启动后停止)
Windows 共享无线上网 无法启动ICS服务解决方法(WIN7 ICS服务启动后停止) ICS 即Internet Connection Sharing,internet连接共享,可以使局域网上其 ...
- gerrit-申请id跟本地配置
OpenID 是一个以用户为中心的数字身份识别框架,它具有开放.分散.自由等特性. 什么是gerrit? 看 了网上的介绍,感觉所谓的gerrit就是一个基于web实现代码管理的服务器.Gerrit ...
- Node组装启动过程
elasticsearch的启动过程是根据配置和环境组装需要的模块并启动的过程.这一过程就是通过guice注入各个功能模块并启动这些模块,从而得到一个功能完整的node.正如之前所说elasticse ...
- 四种布局JS
现代 Web 开发在将体验和功能做到极致的同时,对于美观的追求也越来越高.在推荐完图形库之后,再来推荐一些精品的独立 UI 组件.这些组件可组合在一起,形成美观而交互强大的 Web UI . 给 We ...
- Log4net.confager配置官方文档
http://logging.apache.org/log4net/release/config-examples.html
- Centos 6 DNS Server 配置
安装bind yum install -y bind bind-chroot bind-utis 如果是Centos 5 # yum -y install bind caching-nameserve ...
- SQL Server 2008 Tempdb 数据库迁移
1.首先检查数据文件位置及名称 SELECT name,physical_name FROM sys.database_files 2.迁移 USE master; GO ALTER DATABASE ...
- hdu 5375 Gray code dp
#include<stdio.h> #include<string.h> #include<algorithm> using namespace std; cons ...
- nodejs+express4.0+mongodb安装方法 for Linux, Mac
废话不多说 1:下载nodejs包 下载地址例如以下:http://www.nodejs.org/download/ 下载source code版本号须要解压后到其文件夹运行./configure,然 ...