当前位置:网站首页>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
边栏推荐
猜你喜欢
Unity3d minigame-unity-webgl-transform插件转换微信小游戏报错To use dlopen, you need to use Emscripten‘s...问题
(18) LCD1602 experiment
AdaViT——自适应选择计算结构的动态网络
LeetCode 练习——剑指 Offer 26. 树的子结构
手写ABA遇到的坑
Web APIs DOM time object
Leetcode question brushing (XI) -- sequential questions brushing 51 to 55
Config:invalid signature solution and troubleshooting details
在IPv6中 链路本地地址的优势
网络基础入门理解
随机推荐
2022-07-05 use TPCC to conduct sub query test on stonedb
2022-07-04 the high-performance database engine stonedb of MySQL is compiled and run in centos7.9
Dealing with the crash of QT quick project in offscreen mode
重磅新闻 | Softing FG-200获得中国3C防爆认证 为客户现场测试提供安全保障
go多样化定时任务通用实现与封装
volatile关键字
Plafond du tutoriel MySQL, bien collecté, regardez lentement
Attack and defense world ditf Misc
Crawler obtains real estate data
Seata aggregates at, TCC, Saga and XA transaction modes to create a one-stop distributed transaction solution
Aardio - 不声明直接传float数值的方法
UVa 11732 – strcmp() Anyone?
UDP programming
Chapter 4: talk about class loader again
空结构体多大?
Attack and defense world miscall
Leetcode exercise - Sword finger offer 26 Substructure of tree
Pit encountered by handwritten ABA
SQL server generates auto increment sequence number
【LeetCode】19、 删除链表的倒数第 N 个结点