题目链接:https://vjudge.net/contest/158125#problem/A

题意:

系统中,strcmp函数是这样执行的,给定 n 个字符串,求两两比较时,strcmp函数要比较多少次?

如:

t  h  a  n  \n       t  h  e  r  e  \n

t  h  a  t  \n        t  h  e  \n

2  2  2  1            2  2  2  1

直接两两比较是超时的。

可以这样建立一个字典树:

可以发现一直要比较到交叉处,和这个交叉点的深度有关(2*depth+1)。

1、这个字典序每一个交叉点都是要计算的,所以采用邻接表的形式存图;

2、每个结点都要计算他的叶子结点的个数,当分叉的时候,那么说明这里有两个(多个)不同的单词,从中要选择两个单词,计算有多少种搭配,比较次数就是这些搭配*(2*depth+1);

3、递归找下一个结点。

 #include <bits/stdc++.h>

 using namespace std;

 const int maxnode =  *  + ;
const int sigma_size = ; struct Trie
{
int head[maxnode];
int next[maxnode];
char ch[maxnode];
int tot[maxnode];
int sz;
long long ans;
void clear()
{
sz = ;
tot[] = head[] = next[] = ;
} void insert(const char *s)
{
int u = , v, n = strlen(s);
tot[]++;
for(int i = ; i <= n; i++)
{
// 找字符a[i]
bool found = false;
for(v = head[u]; v != ; v = next[v])
if(ch[v] == s[i]) // 找到了
{
found = true;
break;
}
if(!found)
{
v = sz++; // 新建结点
tot[v] = ;
ch[v] = s[i];
next[v] = head[u];
head[u] = v; // 插入到链表的首部
head[v] = ;
}
u = v;
tot[u]++;
}
} void dfs(int depth,int u)
{
if(head[u]==) //叶子节点
ans+=tot[u]*(tot[u]-)*depth;
else
{
int sum =;
for(int v=head[u]; v!=; v=next[v])
{
sum+=tot[v]*(tot[u]-tot[v]);
}
ans+=sum/*(*depth+);
for(int v=head[u]; v!=; v=next[v])
dfs(depth+,v);
}
} long long count()
{
ans = ;
dfs(,);
return ans;
} } trie; char word[]; int main()
{
int n;
int kase = ;
while(scanf("%d",&n),n)
{
trie.clear();
for(int i=; i<n; i++)
{
scanf("%s",word);
trie.insert(word);
}
printf("Case %d: %lld\n",kase++,trie.count());
} return ;
}

Uva 11732 strcmp()函数的更多相关文章

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

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

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

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

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

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

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

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

  5. UVA - 11732 "strcmp()" Anyone? (trie)

    https://vjudge.net/problem/UVA-11732 题意 给定n个字符串,问用strcmp函数比较这些字符串共用多少次比较. strcmp函数的实现 int strcmp(cha ...

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

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

  7. Uva 11732 strcmp() Anyone?

    strcmp() Anyone? Time Limit: 2000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu [Subm ...

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

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

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

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

随机推荐

  1. 【ACM】懒省事的小明

    懒省事的小明 时间限制:3000 ms  |  内存限制:65535 KB 难度:3   描述       小明很想吃果子,正好果园果子熟了.在果园里,小明已经将所有的果子打了下来,而且按果子的不同种 ...

  2. 导入AppiumLibrary报错: ImportError: cannot import name 'InvalidArgumentException

    导入AppiumLibrary报错: ImportError: cannot import name 'InvalidArgumentException报错原因 selenium.common.exc ...

  3. 多线程编程_控制并发线程数的Semaphore

    简介 Semaphore(信号量)是用来控制同时访问特定资源的线程数量,它通过协调各个线程,以保证合理的使用公共资源.很多年以来,我都觉得从字面上很难理解Semaphore所表达的含义,只能把它比作是 ...

  4. 性能测试工具LoadRunner27-LR之读取Excel数据

    为何要读取Excel数据? 很多用户喜欢用Excel来统计数据,比如学生成绩表.个人信息等.有时需要把Excel中的数据来进行参数化,数据量比较多时,一个个在LR里输入是不现实的,因此需要用LR来导入 ...

  5. AndroidStudio项目提交到github最详细步骤

    在使用studio开发的项目过程中有时候我们想将项目发布到github上,以前都是用一种比较麻烦的方式(cmd)进行提交,最近发现studio其实是自带这种功能的,终于可以摆脱命令行了. 因为自己也没 ...

  6. 基于Python实现邮件发送

    import smtplibfrom email.mime.text import MIMETextemail_host = 'smtp.163.com' # 邮箱地址email_user = 'sz ...

  7. SmartRF Flash Programmer突然打不开显示界面的办法【亲测有效】

    在尝试打开任务管理器结束任务之后重新打开依然无果,在尝试了SmartRF Flash Programmer卸载重装无数次之后依然无果的况状,我被SmartRF Flash Programmer存在界面 ...

  8. Linux下jdk下载

    wget --no-check-certificate --no-cookies --header "Cookie: oraclelicense=accept-securebackup-co ...

  9. hduoj 2602Bone Collector

    Bone Collector Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  10. 如何解读IL代码

    如何解读IL代码 关于IL代码,我有将从三个方面去揭开它神秘的面纱.IL代码是什么?我们为什么要去读懂IL代码?我们如何去读懂IL代码?这三个问题的解答,将是我解读IL代码的整体思路. IL代码是什么 ...