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. html之select标签

    循环select标签 <select name="group_id"> {% for row in group_list %} <option value={{r ...

  2. [LeetCode] Flatten Nested List Iterator 压平嵌套链表迭代器

    Given a nested list of integers, implement an iterator to flatten it. Each element is either an inte ...

  3. [LeetCode] Search in Rotated Sorted Array 在旋转有序数组中搜索

    Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...

  4. NFS简单使用

    NFS网络文件系统(Network File System),由Sun公司开发,从名字上就能够知道这个服务是通过网络的方式来共享文件系统,目前RHEL 6上使用的版本为NFSv4,提供有状态的连接,追 ...

  5. 【转】javascript浏览器参数的操作,js获取浏览器参数

    原文地址:http://www.haorooms.com/post/js_url_canshu html5修改浏览器地址:http://www.cnblogs.com/JiangXiaoTian/ar ...

  6. php 设计模式--准备篇

    要了解设计模式 首先我们要先了解 php的命名空间和类的自动载入的功能 下面我们来说一下 命名空间 概念缘由:比如一个a.php的文章 但是我们需要两个 此时同一个目录下不可能存在两个a.php 那么 ...

  7. weui 搜索框

    点击搜索,会显示关键字取消按钮,输入文字,会在搜索框下,有相应的列表显示. HTML: <!DOCTYPE html> <html> <head> <meta ...

  8. Python NaN

    NaN, Not a Number, 非数. 它即不是无穷大, 也不是无穷小, 而是python/numpy/... 觉得无法计算时返回的一个符号(自己的推测, 未考证(TODO)). import ...

  9. --关于null在oracle数据库中是否参与计算,进行验证,

    --关于null在oracle数据库中是否参与计算,进行验证,with td as (select null id,1 name from dual ),td1 as ( select null id ...

  10. Django进阶(二)

    Template 之前的好多HTML文件中都包含类似"{{ }}"."{% %}",其实他们都是模板语言,模板本质上是HTML,但是夹杂了一些变量和标签,可以方 ...