当前位置:网站首页>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
边栏推荐
- Advantages of link local address in IPv6
- 基於 QEMUv8 搭建 OP-TEE 開發環境
- Void keyword
- Aardio - 不声明直接传float数值的方法
- Applet system update prompt, and force the applet to restart and use the new version
- UVa 11732 – strcmp() Anyone?
- 自定义 swap 函数
- UDP编程
- Senior soft test (Information System Project Manager) high frequency test site: project quality management
- Should novice programmers memorize code?
猜你喜欢
Signed and unsigned keywords
How to confirm the storage mode of the current system by program?
[leetcode] 19. Delete the penultimate node of the linked list
NetXpert XG2帮您解决“布线安装与维护”难题
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
Unity3d minigame unity webgl transform plug-in converts wechat games to use dlopen, you need to use embedded 's problem
Improving Multimodal Accuracy Through Modality Pre-training and Attention
Sword finger offer question brushing record 1
【LeetCode】19、 删除链表的倒数第 N 个结点
随机推荐
SQL Server生成自增序号
Learn the principle of database kernel from Oracle log parsing
Advantages of link local address in IPv6
Inno setup packaging and signing Guide
Classification, function and usage of MySQL constraints
Is there any requirement for the value after the case keyword?
Leetcode: interview question 17.24 Maximum cumulative sum of submatrix (to be studied)
China 1,4-cyclohexanedimethanol (CHDM) industry research and investment decision-making report (2022 Edition)
【数字IC手撕代码】Verilog无毛刺时钟切换电路|题目|原理|设计|仿真
How big is the empty structure?
2022-07-04 mysql的高性能数据库引擎stonedb在centos7.9编译及运行
Comparison between variable and "zero value"
剪映+json解析将视频中的声音转换成文本
Void keyword
Aardio - 利用customPlus库+plus构造一个多按钮组件
i. Mx6ull build boa server details and some of the problems encountered
空结构体多大?
Spatial domain and frequency domain image compression of images
Windows Auzre 微软的云计算产品的后台操作界面
labelimg的安装与使用