Uva 11732 strcmp() Anyone?
| Time Limit: 2000MS | Memory Limit: Unknown | 64bit IO Format: %lld & %llu |
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) |
|
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
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?的更多相关文章
- UVA 11732 - strcmp() Anyone?(Trie)
UVA 11732 - strcmp() Anyone? 题目链接 题意:给定一些字符串,要求两两比較,须要比較的总次数(注意.假设一个字符同样.实际上要还要和'\0'比一次,相当比2次) 思路:建T ...
- 左儿子右兄弟Trie UVA 11732 strcmp() Anyone?
题目地址: option=com_onlinejudge&Itemid=8&category=117&page=show_problem&problem=2832&qu ...
- UVa 11732 "strcmp()" Anyone? (左儿子右兄弟前缀树Trie)
题意:给定strcmp函数,输入n个字符串,让你用给定的strcmp函数判断字符比较了多少次. 析:题意不理解的可以阅读原题https://uva.onlinejudge.org/index.php? ...
- UVA 11732 - strcmp() Anyone? 字典树
传送门:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&a ...
- UVA 11732 strcmp() Anyone? (压缩版字典树)
题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...
- UVA 11732 strcmp() Anyone?(Trie的性质)
strcmp() Anyone? strcmp() is a library function in C/C++ which compares two strings. It takes two st ...
- UVA - 11732 "strcmp()" Anyone?左兄弟右儿子trie
input n 2<=n<=4000 s1 s2 ... sn 1<=len(si)<=1000 output 输出用strcmp()两两比较si,sj(i!=j)要比较的次数 ...
- UVA - 11732 "strcmp()" Anyone? (trie)
https://vjudge.net/problem/UVA-11732 题意 给定n个字符串,问用strcmp函数比较这些字符串共用多少次比较. strcmp函数的实现 int strcmp(cha ...
- Uva 11732 strcmp()函数
题目链接:https://vjudge.net/contest/158125#problem/A 题意: 系统中,strcmp函数是这样执行的,给定 n 个字符串,求两两比较时,strcmp函数要比较 ...
随机推荐
- Nginx+PHP On windows
前期准备 Nginx 下载 http://nginx.org/ PHP下载 PHP Windows binaries 问题 问题1: opened a DOS window to la ...
- Java实现office文档与pdf文档的在线预览功能
最近项目有个需求要java实现office文档与pdf文档的在线预览功能,刚刚接到的时候就觉得有点难,以自己的水平难以在三四天做完.压力略大.后面查找百度资料.以及在同事与网友的帮助下,四天多把它做完 ...
- angular学习笔记(二十八-附2)-$http,$resource中的promise对象
下面这种promise的用法,我从第一篇$http笔记到$resource笔记中,一直都有用到: HttpREST.factory('cardResource',function($resource) ...
- 商城项目:装nginx时碰到的各种问题
因为项目需要,我们要在linux上nginx.碰到了各种问题.在这里一一记录下来. 首先我要开启我的两个虚拟机,开起来之后.用主机的SeureCRT去连接.都是好的. 但是我在虚拟机机上去ping I ...
- [LeetCode] Closest Binary Search Tree Value 最近的二分搜索树的值
Given a non-empty binary search tree and a target value, find the value in the BST that is closest t ...
- [LeetCode] Strobogrammatic Number II 对称数之二
A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside ...
- C#调用vbs脚本实现Windows版Siri
最近新加入,把自己一些有意思的小东西分享给大家,我是一个学生,代码写得少,哪里不规范,希望大家见谅. 这事我封装好的一个类,可以直接实例化对象之后,调用"对象.Talk()"方法, ...
- linux系统下使用流行的版本管理工具 Git
前几天被版本管理困扰了好久,主要是因为 没法回到之前的版本,新版本又出了问题真的很尴尬. 终于决定使用目前网上很火的版本管理工具-------Git 历史啥的就不说了,说些有用的. 我用的是oschi ...
- Linux 基础操作
根据关键字查找文件信息: cat <文件名> | grep <关键字>查询文件信息 显示100行: tail -100f easyhome.all.log |grep &quo ...
- Django Admin
//设置admin列表名称 def __str__(self): return u'%s' % self.name class Meta: db_table ="数据库的那个表" ...