UVA - 11732 "strcmp()" Anyone? (trie)
https://vjudge.net/problem/UVA-11732
题意
给定n个字符串,问用strcmp函数比较这些字符串共用多少次比较。
strcmp函数的实现
int strcmp(char *s, char *t)
{
int i;
for (i=; s[i]==t[i]; i++)
if (s[i]=='\0')
return ;
return s[i] - t[i];
}
分析
建trie树,把‘\0’也加进去,记录以每个节点为子树包含的单词节点。
然后dfs计数,遇到单词节点,说明可能存在相同的字符串,于是此时ans+=tot[u]*(tot[u]-1)*dep;
否则就是(2*dep+1)*sum,sum为所有选法。
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <ctime>
#include <vector>
#include <queue>
#include <map>
#include <stack>
#include <set>
#include <bitset>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define ms(a, b) memset(a, b, sizeof(a))
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
//#define eps 0.0000000001
#define IOS ios::sync_with_stdio(0);cin.tie(0);
#define random(a, b) rand()*rand()%(b-a+1)+a
#define pi acos(-1)
//const ll INF = 0x3f3f3f3f3f3f3f3fll;
const int inf = 0x3f3f3f3f;
const int maxn = * + ;
const int maxm = 4e5 +;
const int mod = ;
const int sigma_size = ; struct Trie{
int head[maxn];// head[i]为第i个结点的左儿子编号
int nxt[maxn];// next[i]为第i个结点的右兄弟编号
char ch[maxn];// ch[i]为第i个结点上的字符
int tot[maxn];// tot[i]为第i个结点为根的子树包含的叶结点总数
int sz;
ll ans;// 答案
void init(){
sz=;
tot[]=head[]=nxt[]=;
}
// 插入字符串s(包括最后的'\0'),沿途更新tot
void insert(char* s){
int u=,v,n=strlen(s);
tot[]++;
for(int i=;i<=n;i++){
// 找字符a[i]
bool f=false;
for(v=head[u];v;v=nxt[v]){
if(ch[v]==s[i]){// 找到了
f=true;
break;
}
}
if(!f){
v=sz++;// 新建结点
tot[v]=;
ch[v]=s[i];
nxt[v]=head[u];
head[u]=v;// 插入到链表的首部
head[v]=;
}
u = v;
tot[u]++;
}
}
// 统计LCP=u的所有单词两两的比较次数之和
void dfs(int dep,int u){
// 叶结点
if(head[u]==) ans+=tot[u]*(tot[u]-)*dep;
else{
int sum=;
for(int v=head[u];v;v=nxt[v]){
sum+=tot[v]*(tot[u]-tot[v]);
// 子树v中选一个串,其他子树中再选一个
}
// 除以2是每种选法统计了两次
ans+=sum/*(*dep+);
for(int v=head[u];v;v=nxt[v])
dfs(dep+,v);
}
}
ll cal(){
ans=;
dfs(,);
return ans;
}
};
Trie trie;
char tmp[];
int main() {
#ifdef LOCAL
freopen("in.txt", "r", stdin);
// freopen("input.txt", "w", stdout);
#endif
int cas=;
int T;
while(~scanf("%d",&T)&&T){
trie.init();
for(int i=;i<T;i++){
scanf("%s",tmp);
trie.insert(tmp);
}
printf("Case %d: %lld\n",cas++,trie.cal());
}
return ;
}
UVA - 11732 "strcmp()" Anyone? (trie)的更多相关文章
- UVA 11732 strcmp() Anyone?(Trie的性质)
strcmp() Anyone? strcmp() is a library function in C/C++ which compares two strings. It takes two st ...
- UVA 11732 strcmp() Anyone (Trie+链表)
先两两比较,比较次数是两者相同的最长前缀长度*2+1,比较特殊的情况是两者完全相同时候比较次数是单词长度*2+2, 两个单词'末尾\0'和'\0'比较一次,s尾部'\0'和循环内'\0'比较一次. 因 ...
- UVa 11732 "strcmp()" Anyone? (左儿子右兄弟前缀树Trie)
题意:给定strcmp函数,输入n个字符串,让你用给定的strcmp函数判断字符比较了多少次. 析:题意不理解的可以阅读原题https://uva.onlinejudge.org/index.php? ...
- UVA 11732 strcmp() Anyone? (压缩版字典树)
题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...
- UVa 11732 strcmp()函数(左孩子右兄弟表示法)
#include<iostream> #include<algorithm> #include<string> #include<cstring> #i ...
- UVA 11488-Hyper Prefix Sets(Trie)
题意: 给一个01串的集合,一个集合的幸运值是串的个数*集合中串的最大公共前缀 ,求所有子集中最大幸运值 分析: val[N]表示经过每个节点串的个数求幸运值 求就是每个节点值*该节点的深度 搜一遍树 ...
- UVA 11732 - strcmp() Anyone?(Trie)
UVA 11732 - strcmp() Anyone? 题目链接 题意:给定一些字符串,要求两两比較,须要比較的总次数(注意.假设一个字符同样.实际上要还要和'\0'比一次,相当比2次) 思路:建T ...
- 左儿子右兄弟Trie UVA 11732 strcmp() Anyone?
题目地址: option=com_onlinejudge&Itemid=8&category=117&page=show_problem&problem=2832&qu ...
- CJOJ 1071 【Uva】硬币问题(动态规划)
CJOJ 1071 [Uva]硬币问题(动态规划) Description 有n种硬币,面值分别为v1, v2, ..., vn,每种都有无限多.给定非负整数S,可以选用多少个硬币,使得面值之和恰好为 ...
随机推荐
- 【BZOJ3132】【TYVJ1716】上帝造题的七分钟 二维树状数组
题目大意 维护一个\(n\times m\)的矩阵,有两种操作: \(1~x_1~y_1~x_2~y_2~v\):把\((a,b),(c,d)\)为顶点的矩形区域内的所有数字加上\(v\). \(2~ ...
- opencv图像融合(大头)
单纯的变大再覆盖上去,头部检测信息不够全,效果实在是太差,就不多说了,只是按照自己的思路玩一玩,没有达到抖音上那么好的效果 import cv2 as cv import numpy as np im ...
- Qt Creator 搭配Git 版本控制
再次介绍一下Git的使用,这次是在Coding.net上部署项目的.这个是写给大作业合作的小伙伴们(我和我的A奶朋友们和某A的男朋友)看的. 安装Git 首先安装Git(msysGit) 下载地址 h ...
- dll 修复....
之前在安装时总是会碰到缺少什么dll文件,总是头疼的要命,这次很幸运的在网上搜到了这个神奇的小玩意,只需要运行就能够修复缺少的所有的dll文件,所以在这小小的分享一下. 链接:https://pan. ...
- 转----ui输入测试数据
jin'tHackChecker黑测工作室 - 专注于软件安全测试技术研究!(www.AutomationQA.com)常用安全测试用例 建立整体的威胁模型,测试溢出漏洞.信息泄漏.错误处理.SQL ...
- docker-compose.yml(4)
实例3:version: '3'services: mysql: image: mysql network_mode: "host" environment: - MYSQL_RO ...
- centos7破解安装confluence5.9.11
应用环境:Confluence是一个专业的企业知识管理与协同软件,也可以用于构建企业wiki.通过它可以实现团队成员之间的协作和知识共享. 安装环境:centos7.3 Java环境 1.7.0_79 ...
- Python学习day2 while循环&格式化输出&运算符
day2 运算符-while循环 1.while循环 while循环基本结构; while 条件: 结果 # 如果条件为真,那么循环则执行 # 如果条件为假,那么循环不执行 de ...
- A1076. Forwards on Weibo
Weibo is known as the Chinese version of Twitter. One user on Weibo may have many followers, and may ...
- JavaScript原型详解
1,前言 下面是2008年Github创建以来,各种编程语言的排名情况 排名其中JavaScript自2015年之后就盘踞第一名,成为github上被使用最多的语言,早期,JS的使用还主要集中于浏览器 ...