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函数要比较 ...
随机推荐
- c# 三种常见的委托
参考 <编写高质量代码:改善C#程序的157个建议> , 尽量使用FCL中的委托声明. FCL: FrameWork Class Library 三种常用:Action.Func.Pre ...
- [LeetCode] Remove Duplicate Letters 移除重复字母
Given a string which contains only lowercase letters, remove duplicate letters so that every letter ...
- [LeetCode] Candy 分糖果问题
There are N children standing in a line. Each child is assigned a rating value. You are giving candi ...
- 【C#】新建服务自动发送邮件
---windows服务,---自动发送邮件 邮件发送code #region 发送邮件函数 public void SendMailUseZj() { System.Net.Mail.MailMes ...
- html种种
DIV+CSS如何让文字垂直居中?--https://zhidao.baidu.com/question/69214815.html
- 【codeforces 148D】 Bag of mice
http://codeforces.com/problemset/problem/148/D (题目链接) 题意 包中有w个白鼠,b个黑鼠.公主和龙轮流画老鼠,公主先画,谁先画到白鼠谁就赢.龙每画完一 ...
- Emeditor批量修改文件编码格式(UTF-8)
采用宏的形式进行,直接在Emeidor导入宏即可使用: emeditor导入宏:[宏]->[自定义]->[新建]->找到EncodingChange.jsee文件即可. 链接:htt ...
- MySQL二进制日志
一.二进制日志(The Binary Log) 1.简介 包含所有更新了的数据或者已经潜在更新了的数据(比如一条没有匹配任何行的delete语句) 包含所有更新语句执行时间的信息 不记录没有修改数据的 ...
- 关闭SELinux和iptables防火墙
1.关闭SELinux: 编辑SELinux配置文件: [root@Redis selinux]# vim /etc/selinux/config 修改SELINUX配置项为disable SELIN ...
- pip apt source images
~/.pip/pip.conf [global] index-url = https://pypi.douban.com/simple download_cache = ~/.cache/pip [i ...