当前位置:网站首页>UVa 11732 – strcmp() Anyone?
UVa 11732 – strcmp() Anyone?
2022-07-06 22:33:00 【Full stack programmer webmaster】
Hello everyone , I meet you again , I'm the king of the whole stack .
The title of : Here's something for you , Give you a string comparison function , All words are parity , What is the number of comparisons .
analysis : string 、 terry .
First . Look at the data size , Suppose you find clues normally , meeting TLE and MLE.
because , The conventional dictionary tree depth is 1000, And there may be a lot of waste of correction space .
therefore , Adopt the compression algorithm of tree ( Brother left , The right child ). Can improve execution efficiency .
then . The same level in the dictionary tree can . In Statistics , Be able to make achievements before making statistics , Or make statistics while building trees .
Here I choose the method of statistics while building trees ( A large number of online practices , They are built first and then counted , Search for solutions )
Every time you insert a word . It is only compared with the words inserted before ; The comparison times of words are the sum of the comparison times of each letter .
The number of comparisons of each letter in a word . It is the number of words included in the root node of this letter .
Word comparison functions such as the following :
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];
}Because the comparison function is as above , Pay attention to the following points when calculating :
1. The same length is L The cost of comparing two words of is 2L-1. Go out for the last time s Concluded inference ;
2. The comparative length of words should be strlen(str)+1, The terminator is also a part of comparison ;
3. Suppose two words are the same , Then calculate one more time to end the inference . Comparison times +1.
Be careful : Use LL, Otherwise the data will overflow .
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
using namespace std;
typedef long long LL;
char words[1010];
/* Trie define */
#define nodesize 4444444 // Number of nodes
typedef struct node1
{
char value;
int size;
int count;
node1* Lchild;
node1* Rchild;
}tnode;
tnode dict[nodesize];
class Trie
{
private:
LL count;
int size;
tnode* root;
public:
Trie() {initial();}
void initial() {
memset( dict, 0, sizeof(dict) );
size=0;count=0LL;root=newnode(0);
}
tnode* newnode( char c ) {
dict[size].value = c;
return &dict[size ++];
}
void insert( char* word, int L ) {
tnode* now = root->Rchild,*save = root;
int same = 1;
for ( int i = 0 ; i <= L ; ++ i ) {
if ( !now ) {
save->Rchild = newnode(word[i]);
now = save->Rchild;
now->count = 1;
same = 0;
}else {
if ( i ) count += now->count;
count += now->count ++;
while ( now->Lchild && now->value != word[i] )
now = now->Lchild;
if ( now->value != word[i] ) {
now->Lchild = newnode(word[i]);
now = now->Lchild;
same = 0;
}
}
save = now;
now = save->Rchild;
}
if ( same ) save->size ++;
count += save->size;
}
LL query() {return count;}
}trie;
/* Trie end */
int main()
{
int Case = 1,N;
while ( scanf("%d",&N) != EOF ) {
if ( !N ) break;
trie.initial();
for ( int i = 0 ; i < N ; ++ i ) {
scanf("%s",words);
trie.insert(words,strlen(words));
}
printf("Case %d: %lld\n",Case ++,trie.query());
}
return 0;
}Copyright notice : This article is the original article of the blogger . Blog , Do not reprint without permission .
Publisher : Full stack programmer stack length , Reprint please indicate the source :https://javaforall.cn/116985.html Link to the original text :https://javaforall.cn
边栏推荐
- 新手程序员该不该背代码?
- Leetcode exercise - Sword finger offer 26 Substructure of tree
- RESNET rs: Google takes the lead in tuning RESNET, and its performance comprehensively surpasses efficientnet series | 2021 arXiv
- Web APIs DOM 时间对象
- NPDP certification | how do product managers communicate across functions / teams?
- MySQL约束的分类、作用及用法
- Inno setup packaging and signing Guide
- Aardio - 不声明直接传float数值的方法
- 【LeetCode】19、 删除链表的倒数第 N 个结点
- 2022-07-04 mysql的高性能数据库引擎stonedb在centos7.9编译及运行
猜你喜欢

Netxpert xg2 helps you solve the problem of "Cabling installation and maintenance"

Export MySQL table data in pure mode

【数字IC手撕代码】Verilog无毛刺时钟切换电路|题目|原理|设计|仿真

Chapter 4: talk about class loader again

云原生技术--- 容器知识点

Sword finger offer question brushing record 1

Build op-tee development environment based on qemuv8

(十八)LCD1602实验

Memorabilia of domestic database in June 2022 - ink Sky Wheel

Aardio - 利用customPlus库+plus构造一个多按钮组件
随机推荐
树的先序中序后序遍历
C# 三种方式实现Socket数据接收
Gd32f4xx serial port receive interrupt and idle interrupt configuration
【踩坑合辑】Attempting to deserialize object on CUDA device+buff/cache占用过高+pad_sequence
MySQL教程的天花板,收藏好,慢慢看
Inno Setup 打包及签名指南
qt quick项目offscreen模式下崩溃的问题处理
雅思口语的具体步骤和时间安排是什么样的?
Leetcode question brushing (XI) -- sequential questions brushing 51 to 55
Advantages of link local address in IPv6
Dealing with the crash of QT quick project in offscreen mode
重磅新闻 | Softing FG-200获得中国3C防爆认证 为客户现场测试提供安全保障
(十八)LCD1602实验
Extern keyword
Leetcode: interview question 17.24 Maximum cumulative sum of submatrix (to be studied)
Mise en place d'un environnement de développement OP - tee basé sur qemuv8
基于 QEMUv8 搭建 OP-TEE 开发环境
Report on technological progress and development prospects of solid oxide fuel cells in China (2022 Edition)
What are the specific steps and schedule of IELTS speaking?
Senior soft test (Information System Project Manager) high frequency test site: project quality management