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)的更多相关文章

  1. UVA 11732 strcmp() Anyone?(Trie的性质)

    strcmp() Anyone? strcmp() is a library function in C/C++ which compares two strings. It takes two st ...

  2. UVA 11732 strcmp() Anyone (Trie+链表)

    先两两比较,比较次数是两者相同的最长前缀长度*2+1,比较特殊的情况是两者完全相同时候比较次数是单词长度*2+2, 两个单词'末尾\0'和'\0'比较一次,s尾部'\0'和循环内'\0'比较一次. 因 ...

  3. UVa 11732 "strcmp()" Anyone? (左儿子右兄弟前缀树Trie)

    题意:给定strcmp函数,输入n个字符串,让你用给定的strcmp函数判断字符比较了多少次. 析:题意不理解的可以阅读原题https://uva.onlinejudge.org/index.php? ...

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

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

  5. UVa 11732 strcmp()函数(左孩子右兄弟表示法)

    #include<iostream> #include<algorithm> #include<string> #include<cstring> #i ...

  6. UVA 11488-Hyper Prefix Sets(Trie)

    题意: 给一个01串的集合,一个集合的幸运值是串的个数*集合中串的最大公共前缀 ,求所有子集中最大幸运值 分析: val[N]表示经过每个节点串的个数求幸运值 求就是每个节点值*该节点的深度 搜一遍树 ...

  7. UVA 11732 - strcmp() Anyone?(Trie)

    UVA 11732 - strcmp() Anyone? 题目链接 题意:给定一些字符串,要求两两比較,须要比較的总次数(注意.假设一个字符同样.实际上要还要和'\0'比一次,相当比2次) 思路:建T ...

  8. 左儿子右兄弟Trie UVA 11732 strcmp() Anyone?

    题目地址: option=com_onlinejudge&Itemid=8&category=117&page=show_problem&problem=2832&qu ...

  9. CJOJ 1071 【Uva】硬币问题(动态规划)

    CJOJ 1071 [Uva]硬币问题(动态规划) Description 有n种硬币,面值分别为v1, v2, ..., vn,每种都有无限多.给定非负整数S,可以选用多少个硬币,使得面值之和恰好为 ...

随机推荐

  1. 【XSY2762】插线板 分块

    题目大意 有\(n\)个插线板,每个插线板会在\(l_i\)时刻初插入到队列中(队列是按插线板的编号排序的),\(r_i\)时刻末移除. 插入一个插线板时会对当前所有接在队列中这个插线的下一个插线板上 ...

  2. requests中 .text 和 .content区别

    import requests url = 'https://www.baidu.com' response = requests.get(url) 1.response.content: 这个是直接 ...

  3. Hdoj 2041.超级楼梯 题解

    Problem Description 有一楼梯共M级,刚开始时你在第一级,若每次只能跨上一级或二级,要走上第M级,共有多少种走法? Input 输入数据首先包含一个整数N,表示测试实例的个数,然后是 ...

  4. linux系统下FTP服务器的安装和配置

    FTP是File Transfer Protocol(文件传输协议),就是专门用来传输文件的协议.简单地说,支持FTP协议的服务器就是FTP服务器. PORT模式: 首先客户端开启一个非特权端口N(大 ...

  5. ⌈洛谷4735⌋⌈BZOJ3261⌋最大异或和【可持久化01Trie】

    题目链接 [BZOJ传送门] [洛谷传送门] 题解 终于学会了可持久化trie树了.感觉并不是特别的难. 因为可持久化,那么我们就考虑动态开点的trie树. 都知道异或操作是有传递性的,那么我们就维护 ...

  6. html内嵌框架

    html内嵌框架 <iframe>标签会创建包含另外一个html文件的内联框架(即行内框架),src属性来定义另一个html文件的引用地址,frameborder属性定义边框,scroll ...

  7. pip 安装第三方包提示Unknown or unsupported command 'install'

    Unknown or unsupported command 'install' Unknown or unsupported command 'show' Unknown or unsupporte ...

  8. 「ZJOI2016」旅行者 解题报告

    「ZJOI2016」旅行者 对网格图进行分治. 每次从中间选一列,然后枚举每个这一列的格子作为起点跑最短路,进入子矩形时把询问划分一下,有点类似整体二分 至于复杂度么,我不会阿 Code: #incl ...

  9. 「SCOI2015」小凸玩矩阵 解题报告

    「SCOI2015」小凸玩矩阵 我好沙茶啊 把点当边连接行和列,在外面二分答案跑图的匹配就行了 我最开始二分方向搞反了,样例没过. 脑袋一抽,这绝壁要费用流,连忙打了个KM 然后wa了,一想这个不是完 ...

  10. 汽车控制器LIMPHOME电路设计

    摘要:本文介绍汽车控制器上常用的3种LIMPHOME电路设计方法,用于在单片机复位重启期间仍能保证外部输出正确性,确保行车安全.    在电子电气领域,单片机使用非常广泛,单片机的复位重启是设计时必须 ...