uva11732 strcmp() Anyone?

题意:给出多个字符串,两两配对,求总配对次数。

思路:如果两个字符串一样,ans=strlen(字符串)*2+2,如果不同,ans=公共前缀长度*2+1;用左儿子右兄弟建字典树。插入一个字符计算一次。

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <cmath>
 4 #include <cstdlib>
 5 #include <iostream>
 6 #include <algorithm>
 7 #include <queue>
 8 #include <map>
 9 #include <vector>
10 using namespace std;
11 const int maxn=111;
12 const int maxnode=4000010;
13 typedef long long LL;
14 
15 struct trie
16 {
17     int son[maxnode];
18     int bro[maxnode];
19     char ch[maxnode];
20     LL ans;
21     int num[maxnode];
22     int sizee;
23 
24     void init()
25     {
26         sizee=1;
27         son[0]=bro[0]=num[0]=0;
28         ans=0;
29     }
30 
31     void insertt(char *s)
32     {
33         int len=strlen(s);
34         int p,u=0;
35         for(int i=0; i<=len; i++)
36         {
37             for(p=son[u]; p; p=bro[p])
38             {
39                 if(ch[p]==s[i])
40                     break;
41             }
42             if(!p)
43             {
44                 p=sizee++;
45                 ch[p]=s[i];
46                 bro[p]=son[u];
47                 son[p]=0;
48                 num[p]=0;
49                 son[u]=p;//p为u的左儿子
50             }
51             ans+=(num[u]-num[p])*(2*i+1);//前缀长度为i,当前这个字母的比较次数为num[u]-num[p]
52             if(i==len)//匹配到最后一个字符,还需与当前这个字母相同的字符比较,而且之前减去num[p]次
53             {
54                 ans+=num[p]*(2*i+2);
55                 num[p]++;
56             }
57             num[u]++;
58             u=p;
59         }
60     }
61 } T;
62 
63 int main()
64 {
65     int n,cas=1;
66     char str[4010];
67     while(~scanf("%d",&n)&&n)
68     {
69         T.init();
70         for(int i=0; i<n; i++)
71         {
72             scanf("%s",str);
73             T.insertt(str);
74         }
75         printf("Case %d: %lld\n",cas++,T.ans);
76     }
77     return 0;
78 }
View Code

 

转载于:https://www.cnblogs.com/ITUPC/p/4964838.html

  • 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、付费专栏及课程。

余额充值