Uva 11732 "strcmp()" Anyone? 左儿子右兄弟的trie

10 篇文章 0 订阅

题目链接:Uva 11732

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

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

The number of comparisons required to compare two strings in strcmp() function is never returnedby the function. But for this problem you will have to do just that at a larger scale. strcmp() functioncontinues to compare characters in the same position of the two strings until two different charactersare 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 inboth cases please consult the code block given above.

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. Eachof 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.

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 N(N1) times. You have to calculate total number of comparisons2

inside the strcmp() function in those N(N1) calls. You can assume that the value of T will fit safely2

in a 64-bit signed integer. Please note that the most straightforward solution (Worst Case ComplexityO(N 2 1000)) will time out for this problem.

Sample Input

2
a
b
4
cat
hat
mat
sir
0

Sample Output

Case 1: 1
Case 2: 6

题意:原文是pdf,图片复制不过来,这题首先给出strcmp的一种实现:

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];
}
问给出一堆字符串,两两执行strcmp函数,一共会运行多少次‘==’运算。

题目分析:多字符串一般都用字典树来存,但是这里字符串太长4000,字典树会MLE,但是字符串个数不多只有1000,所以可以考虑左儿子右兄弟的字典树结构,对于求值,首先对于前缀l相同的2个字符串,会运行2*l次==运算,如果第l+1位不同,这一位只会运行1次,如果第l+1位相同是‘\0’还要+2,插入过程中直接统计,对于每个字符串只统计与之前字符串的匹配情况,注意ans需要用long long来存。

//
//  main.cpp
//  UVA11732 "strcmp()" Anyone?
//
//  Created by teddywang on 2017/5/3.
//  Copyright © 2017年 teddywang. All rights reserved.
//

#include <iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cstdlib>
using namespace std;
typedef long long ll;

typedef struct node{
    int flag;
    char c;
    ll sum;
    node *left,*right;
}trienode;
trienode *root;

char s[10010];
ll ans;

trienode* create_node()
{
    trienode *r;
    r=new node;
    r->left=r->right=NULL;
    r->sum=0;
    r->flag=0;
    return r;
}

void insert_node(char *s)
{
    trienode *r=root,*t,*p;
    int len=strlen(s);
    for(int i=0;i<=len;i++)
    {
        char ss=s[i];
        t=r->left;
        int flag1=0;
        while(t!=NULL)
        {
            if(t->c==s[i])
            {
                flag1=1;
                break;
            }
            t=t->right;
        }
        if(flag1==0)
        {
            p=create_node();
            t=p;
            t->c=s[i];
            t->right=r->left;
            r->left=t;
        }
        ans+=(r->sum-t->sum)*(2*i+1);
        if(i==len)
        {
            ans+=(t->sum)*(2*i+2);
            t->sum++;
        }
        r->sum++;
        r=t;
    }
}


void dfs(trienode *r)//just for fun
{
    if(r->left!=NULL)
    {
        printf("%c ->left: %c\n",r->c,r->left->c);
        dfs(r->left);
    }
    if(r->right!=NULL)
    {
        printf("%c ->right: %c\n",r->c,r->right->c);
        dfs(r->right);
    }
}


void del(trienode * r)
{
    if(r->left!=NULL)
        del(r->left);
    if(r->right!=NULL)
        del(r->right);
}

int n;
int main()
{
    int cas=1;
    while(scanf("%d",&n)&&n)
    {
        root=create_node();
        ans=0;
        for(int i=0;i<n;i++)
        {
            scanf("%s",s);
            insert_node(s);
        }
        printf("Case %d: %lld\n",cas++,ans);
        //dfs(root);
        del(root);
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
是的,C++中确实有`strcmp`函数。`strcmp`是一个字符串比较函数,用于比较两个字符串是否相等。它是在C++标准库中的`<cstring>`(在C++11及以后的版本中,这个库通常被重命名为`<string>`)中定义的。 函数的原型通常如下: ```cpp int strcmp(const char *str1, const char *str2); ``` 这个函数会返回一个整数,如果`str1`和`str2`完全相同,它会返回0。如果`str1`在字典序上小于`str2`,它会返回一个负数。如果`str1`在字典序上大于`str2`,它会返回一个正数。 使用这个函数时,需要注意以下几点: * 字符串必须以空字符('\0')结尾,否则可能会产生未定义的行为。 * 字符串必须以空字符结尾的相同方式进行比较。否则可能会得到错误的结果。 * 两个字符串的指针参数不能是NULL,否则会抛出运行时错误。 使用这个函数的一个例子可能是这样的: ```cpp #include <cstring> #include <iostream> int main() { std::string str1 = "Hello"; std::string str2 = "Hello"; int result = strcmp(str1.c_str(), str2.c_str()); if (result == 0) { std::cout << "Strings are equal." << std::endl; } else if (result < 0) { std::cout << "String1 is less than String2." << std::endl; } else { std::cout << "String1 is greater than String2." << std::endl; } return 0; } ``` 这个程序将打印出 "Strings are equal.",因为两个字符串是相同的。请注意,为了安全起见,最好始终使用字符串的`.c_str()`方法来获取C风格的字符串,以便在需要使用`strcmp`或类似函数的情况下使用它们。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值