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. 【XSY2693】景中人 区间DP

    题目描述 平面上有\(n\)个点,你要用一些矩形覆盖这些点,要求: 每个矩形的下边界为\(y=0\) 每个矩形的大小不大于\(s\) 问你最少要用几个矩形. \(n\leq 100,1\leq y\l ...

  2. hihocoder#1333 : 平衡树·Splay2 (区间操作)

    题面: #1333 : 平衡树·Splay2 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Ho:好麻烦啊~~~~~ 小Hi:小Ho你在干嘛呢? 小Ho:我在干活啊! ...

  3. HDOJ 5666//快速积,推公式

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=5666 题意:给一条直线x+y=q,在(0,0)往x+y=q上面的整数点连线,x+y=q与x,y轴截成的三角 ...

  4. 洛谷P3222 [HNOI2012]射箭(计算几何,半平面交,双端队列)

    洛谷题目传送门 设抛物线方程为\(y=ax^2+bx(a<0,b>0)\),我们想要求出一组\(a,b\)使得它尽可能满足更多的要求.这个显然可以二分答案. 如何check当前的\(mid ...

  5. Leetcode 75.颜色分类 By Python

    给定一个包含红色.白色和蓝色,一共 n 个元素的数组,原地对它们进行排序,使得相同颜色的元素相邻,并按照红色.白色.蓝色顺序排列. 此题中,我们使用整数 0. 1 和 2 分别表示红色.白色和蓝色. ...

  6. TCP/IP 和 HTTP 的区别和联系是什么?

    作者:车小胖链接:https://www.zhihu.com/question/38648948/answer/240006409来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注 ...

  7. 【bfs】Knight Moves

    [题目描述] 输入nn代表有个n×nn×n的棋盘,输入开始位置的坐标和结束位置的坐标,问一个骑士朝棋盘的八个方向走马字步,从开始坐标到结束坐标可以经过多少步. [输入] 首先输入一个nn,表示测试样例 ...

  8. ACM-ICPC 2015 ChangChun

    比赛链接 :点击这里 大概会写 F G J L 吧 F 给你一个序列 最多删除一个数使他构成 最长不上升或者不下降子序列 这题不会不会on的算法只能 t*n*logn 了 还是压常过 求两次 LIS ...

  9. springboot 通过 hibernate 连接sqlserver 空间数据 位置数据

    示例代码:https://github.com/bigben0123/spring-boot-spatial-example 1,配置application.properties #sqlserver ...

  10. 任意模数NTT

    任意模数\(NTT\) 众所周知,为了满足单位根的性质,\(NTT\)需要质数模数,而且需要能写成\(a2^{k} + r\)且\(2^k \ge n\) 比较常用的有\(998244353,1004 ...