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函数要比较 ...
随机推荐
- VS2013无法启动 IIS Express Web解决办法
开发环境:windows8.1+VS2013 使用VS2013有一段时间了,因前期都是编写C/S程序,没有使用到B/S调试器.前几日,创建了一个MVC项目,突然发现VS2013无法调试,报了这样的错. ...
- 项目游戏开发日记 No.0x000003
14软二杨近星(2014551622) 刚刚过去清明节, 意味着离交项目的时间, 还有三个星期, 有点着急了, 可是, 还是觉得无所适从... 项目进展: 刚刚过去的一周, 事非常多, 以至于, 进展 ...
- 项目游戏开发日记 No.0x000002
14软二杨近星(2014551622) 项目开发的开始, 到现在已经很久了, 软件工程的课也上了很久了, 不过, 我们的游戏现在依然还没有影子, 只能说...还是啥也不会... 从一开始, 兴致勃勃地 ...
- 使用Proj库进行大地坐标转空间坐标、投影坐标的一个示例
最近研究了proj库的使用,自己写了一个小demo,仅供参考. void demoPROJ() { const char* wgs84 = "+proj=tmerc +ellps=WGS84 ...
- [LeetCode] Trapping Rain Water 收集雨水
Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...
- 在WPF程序中打开网页:使用代理服务器并可进行JS交互
本项目环境:使用VS2010(C#)编写的WPF程序,通过CefSharp在程序的窗体中打开网页.需要能够实现网页后台JS代码中调用的方法,从网页接收数据,并能返回数据给网页.运行程序的电脑不允许上网 ...
- 标准库String类
下面的程序并没有把String类的所有成员方法实现,只参考教程写了大部分重要的成员函数. [cpp] view plain copy #include<iostream> #include ...
- com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'dd' in 'where clause'
今天在使用mysql数据库查找数据的时候报错,错误信息如下: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown co ...
- BZOJ 3931: [CQOI2015]网络吞吐量
3931: [CQOI2015]网络吞吐量 Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 1555 Solved: 637[Submit][Stat ...
- codevs 2287 火车站
2287 火车站 时间限制: 1 s 空间限制: 32000 KB 题目等级 : 钻石 Diamond 题目描述 Description 火车从始发站(称为第1站)开出,在始发站上车的人 ...