Time Limit: 2000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu

[Submit]   [Go Back]   [Status]

Description

 

J

“strcmp()” Anyone?

Input: Standard Input

Output: Standard Output

strcmp() is a library function in C/C++ which compares two strings. It takes two strings as input parameter and decides which one is lexicographically larger or smaller: If the first string is greater then it returns a positive value, if the second string is greater it returns a negative value and if two strings are equal it returns a zero. The code that is used to compare two strings in C/C++ library is shown below:

int strcmp(char *s, char *t)
{
    int i;
    for (i=0; s[i]==t[i]; i++)
        if (s[i]=='\0')
            return 0;
    return s[i] - t[i];
}

Figure: The standard strcmp() code provided for this problem.

The number of comparisons required to compare two strings in strcmp() function is never returned by the function. But for this problem you will have to do just that at a larger scale. strcmp() function continues to compare characters in the same position of the two strings until two different characters are found or both strings come to an end. Of course it assumes that last character of a string is a null (‘\0’) character. For example the table below shows what happens when “than” and “that”; “therE” and “the” are compared using strcmp() function. To understand how 7 comparisons are needed in both cases please consult the code block given above.

t

h

a

N

\0

 

t

h

e

r

E

\0

 

=

=

=

 

=

=

=

 

 

t

h

a

T

\0

t

h

e

\0

 

 

Returns negative value

7 Comparisons

Returns positive value

7 Comparisons

Input

The input file contains maximum 10 sets of inputs. The description of each set is given below:

Each set starts with an integer N (0<N<4001) which denotes the total number of strings. Each of the next N lines contains one string. Strings contain only alphanumerals (‘0’… ‘9’, ‘A’… ‘Z’, ‘a’… ‘z’) have a maximum length of 1000, and a minimum length of 1.

Input is terminated by a line containing a single zero. Input file size is around 23 MB.

Output

For each set of input produce one line of output. This line contains the serial of output followed by an integer T. This T denotes the total number of comparisons that are required in the strcmp() function if all the strings are compared with one another exactly once. So for N strings the function strcmp() will be called exactly  times. You have to calculate total number of comparisons inside the strcmp() function in those  calls. You can assume that the value of T will fit safely in a 64-bit signed integer. Please note that the most straightforward solution (Worst Case Complexity O(N2 *1000)) will time out for this problem.

Sample Input                              Output for Sample Input

2

a

b

4

cat

hat

mat

sir

0

Case 1: 1

Case 2: 6


Problem Setter: Shahriar Manzoor, Special Thanks: Md. Arifuzzaman Arif, Sohel Hafiz, Manzurur Rahman Khan

[Submit]   [Go Back]   [Status]

Trie,因为结点种类太多,所以要改用左儿子右兄弟的Trie,不然会TLE。

判断比较几次的时候,要考虑字符串完全相同的情况。

1:规律是 sum( node *(node-1))   +  n*(n-1)/2  + sum ( flag*(flag-1)/2 )

即  区分开的次数+每个经过的结点比较的次+重复的串在结尾比较了2次

2:也可以每个节点分开来考虑,累计单词在每个节点分开的时候,所比较的次数(第一个节点分开的单词要比较1次,第二个节点分开的要比较3次。。。。)

 #include <iostream>
#include <cstdio>
#include <cstring> using namespace std; const int maxnode=*; typedef long long int LL; struct Trie
{
int left[maxnode],right[maxnode],val[maxnode],cnt;
char ch[maxnode];
LL ans;
Trie(){}
void init()
{
cnt=;
left[]=right[]=val[]=ch[]=ans=;
}
void insert(const char * str)
{
int len=strlen(str);
int u=,j;
for(int i=;i<=len;i++)
{
for(j=left[u];j;j=right[j])
{
if(ch[j]==str[i]) break;
}
if(j==)
{
j=cnt++;
right[j]=left[u];
left[u]=j;
left[j]=;ch[j]=str[i];
val[j]=;
}
// printf("%d:%c %d * %d = %d\n",i,str[i],val[u]-val[j],(i<<1|1),(val[u]-val[j])*(i<<1|1));
ans+=(val[u]-val[j])*(i<<|);
if(i==len)
{
// printf("%d:%c %d * %d = %d\n",i,str[i],val[j],(i+1)<<1,val[j]*((i+1)<<1));
ans+=val[j]*((i+)<<);
val[j]++;
}
val[u]++;u=j;
}
}
}tree; int main()
{
char dic[];
int n,cas=;
while(scanf("%d",&n)!=EOF&&n)
{
tree.init();
while(n--)
{
scanf("%s",dic);
tree.insert(dic);
}
printf("Case %d: %lld\n",cas++,tree.ans);
}
return ;
}

Uva 11732 strcmp() Anyone?的更多相关文章

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

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

  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)

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

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

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

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

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

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

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

  7. UVA - 11732 "strcmp()" Anyone?左兄弟右儿子trie

    input n 2<=n<=4000 s1 s2 ... sn 1<=len(si)<=1000 output 输出用strcmp()两两比较si,sj(i!=j)要比较的次数 ...

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

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

  9. Uva 11732 strcmp()函数

    题目链接:https://vjudge.net/contest/158125#problem/A 题意: 系统中,strcmp函数是这样执行的,给定 n 个字符串,求两两比较时,strcmp函数要比较 ...

随机推荐

  1. 利用线上数据验证系统 Gor

    Web 应用性能和压力测试工具 Gor - 运维生存时间 http://hao.jobbole.com/gorhttp/ 要使用线上引流到测试环境的作用,需要做到以下几点: 1.新搭建一套测试环境,连 ...

  2. Re 模块

    re模块提供方法如compile, search, findall, match和其他的方法.这些函数是使用REGEX语法建立了一个模式来处理文本的. 第一个方法:search. 一个基本的搜索工作原 ...

  3. struts-tags通用标签基本用法

    2017-01-07 16:02:46 <s:debug></s:debug><!-- 标签库中实现的debug --> ${name }<!-- 是从对象栈 ...

  4. BZOJ 1415 【NOI2005】 聪聪和可可

    题目链接:聪聪和可可 一道水题--开始还看错题了,以为边带权--强行\(O(n^3)\)预处理-- 首先,我们显然可以预处理出一个数组\(p[u][v]\)表示可可在点\(u\),聪聪在点\(v\)的 ...

  5. [LeetCode] Total Hamming Distance 全部汉明距离

    The Hamming distance between two integers is the number of positions at which the corresponding bits ...

  6. [LeetCode] Insert Delete GetRandom O(1) 常数时间内插入删除和获得随机数

    Design a data structure that supports all following operations in average O(1) time. insert(val): In ...

  7. HDU3068 回文串 Manacher算法

    好久没有刷题了,虽然参加过ACM,但是始终没有融会贯通,没有学个彻底.我干啥都是半吊子,一瓶子不满半瓶子晃荡. 就连简单的Manacher算法我也没有刷过,常常为岁月蹉跎而感到后悔. 问题描述 给定一 ...

  8. Redis集群(九):Redis Sharding集群Redis节点主从切换后客户端自动重新连接

    上文介绍了Redis Sharding集群的使用,点击阅读 本文介绍当某个Redis节点的Master节点发生问题,发生主从切换时,Jedis怎样自动重连新的Master节点 ​一.步骤如下: 1.配 ...

  9. git提示:Fatal:could not fetch refs from ....

    在git服务器上新建项目提示: Fatal:could not fetch refs from git..... 百度搜索毫无头绪,最后FQgoogle,找到这篇文章http://www.voidcn ...

  10. jdbc读取数据库,表相关信息(含注释)

    读取数据库中的所有的表名 private Set<String> getTableNameByCon(Connection con) { Set<String> set = n ...