Uva 11732 strcmp()函数

题目链接:https://vjudge.net/contest/158125#problem/A

题意:

系统中,strcmp函数是这样执行的,给定 n 个字符串,求两两比较时,strcmp函数要比较多少次?

如:

t  h  a  n  \n       t  h  e  r  e  \n

t  h  a  t  \n        t  h  e  \n

2  2  2  1            2  2  2  1

直接两两比较是超时的。

可以这样建立一个字典树:

可以发现一直要比较到交叉处,和这个交叉点的深度有关(2*depth+1)。

1、这个字典序每一个交叉点都是要计算的,所以采用邻接表的形式存图;

2、每个结点都要计算他的叶子结点的个数,当分叉的时候,那么说明这里有两个(多个)不同的单词,从中要选择两个单词,计算有多少种搭配,比较次数就是这些搭配*(2*depth+1);

3、递归找下一个结点。

 1 #include <bits/stdc++.h>
 2 
 3 using namespace std;
 4 
 5 const int maxnode = 4000 * 1000 + 10;
 6 const int sigma_size = 26;
 7 
 8 struct Trie
 9 {
10     int head[maxnode];
11     int next[maxnode];
12     char ch[maxnode];
13     int tot[maxnode];
14     int sz;
15     long long ans;
16     void clear()
17     {
18         sz = 1;
19         tot[0] = head[0] = next[0] = 0;
20     }
21 
22     void insert(const char *s)
23     {
24         int u = 0, v, n = strlen(s);
25         tot[0]++;
26         for(int i = 0; i <= n; i++)
27         {
28             // 找字符a[i]
29             bool found = false;
30             for(v = head[u]; v != 0; v = next[v])
31                 if(ch[v] == s[i])   // 找到了
32                 {
33                     found = true;
34                     break;
35                 }
36             if(!found)
37             {
38                 v = sz++; // 新建结点
39                 tot[v] = 0;
40                 ch[v] = s[i];
41                 next[v] = head[u];
42                 head[u] = v; // 插入到链表的首部
43                 head[v] = 0;
44             }
45             u = v;
46             tot[u]++;
47         }
48     }
49 
50 
51     void dfs(int depth,int u)
52     {
53         if(head[u]==0) //叶子节点
54             ans+=tot[u]*(tot[u]-1)*depth;
55         else
56         {
57             int sum =0;
58             for(int v=head[u]; v!=0; v=next[v])
59             {
60                 sum+=tot[v]*(tot[u]-tot[v]);
61             }
62             ans+=sum/2*(2*depth+1);
63             for(int v=head[u]; v!=0; v=next[v])
64                 dfs(depth+1,v);
65         }
66     }
67 
68     long long count()
69     {
70         ans = 0;
71         dfs(0,0);
72         return ans;
73     }
74 
75 } trie;
76 
77 char word[1010];
78 
79 int main()
80 {
81     int n;
82     int kase = 1;
83     while(scanf("%d",&n),n)
84     {
85         trie.clear();
86         for(int i=0; i<n; i++)
87         {
88             scanf("%s",word);
89             trie.insert(word);
90         }
91         printf("Case %d: %lld\n",kase++,trie.count());
92     }
93 
94     return 0;
95 }
View Code

 

转载于:https://www.cnblogs.com/TreeDream/p/6691301.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
strcmp函数是C语言中的字符串比较函数。它用于比较两个字符串的大小,并返回一个整数值作为比较结果。引用给出了一个使用strcmp函数的示例代码,其中使用strcmp函数比较了两个字符串s1和s2,并根据比较结果输出不同的信息。strcmp函数需要引用string.h头文件。引用是另一个使用strcmp函数的示例代码,用于验证用户输入的密码是否正确。在这个示例中,通过strcmp函数将用户输入的密码与正确的密码进行比较,如果相等则输出登录成功,否则输出密码错误。引用给出了strcmp函数函数原型,它接受两个const char类型的字符串作为参数,并返回一个整数值,如果两个字符串相等则返回0,如果第一个字符串小于第二个字符串则返回一个负数,如果第一个字符串大于第二个字符串则返回一个正数。通过对strcmp函数的理解和使用,可以实现字符串的比较操作。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [strcmp()函数](https://blog.csdn.net/WWJ970529/article/details/81778909)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [C语言中的字符串比较函数strcmp()](https://blog.csdn.net/xingyuncao520025/article/details/130698277)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值