当前位置:网站首页>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
边栏推荐
- 2022-07-05 stonedb sub query processing parsing time analysis
- MySQL数据库基本操作-DML
- [Digital IC hand tearing code] Verilog burr free clock switching circuit | topic | principle | design | simulation
- Research and investment strategy report of China's VOCs catalyst industry (2022 Edition)
- Applet system update prompt, and force the applet to restart and use the new version
- Unity3d minigame unity webgl transform plug-in converts wechat games to use dlopen, you need to use embedded 's problem
- 云原生技术--- 容器知识点
- Memorabilia of domestic database in June 2022 - ink Sky Wheel
- How to use flexible arrays?
- General implementation and encapsulation of go diversified timing tasks
猜你喜欢

Aardio - 不声明直接传float数值的方法

Aardio - 通过变量名将变量值整合到一串文本中

Web APIs DOM time object

Aardio - 封装库时批量处理属性与回调函数的方法

Daily question 1: force deduction: 225: realize stack with queue

第4章:再谈类的加载器

Attack and defense world ditf Misc

signed、unsigned关键字

Chapter 3: detailed explanation of class loading process (class life cycle)

Self made j-flash burning tool -- QT calls jlinkarm DLL mode
随机推荐
HDR image reconstruction from a single exposure using deep CNN reading notes
Data storage (1)
Senior soft test (Information System Project Manager) high frequency test site: project quality management
Export MySQL table data in pure mode
Unity3d minigame unity webgl transform plug-in converts wechat games to use dlopen, you need to use embedded 's problem
Clip +json parsing converts the sound in the video into text
2022-07-04 mysql的高性能数据库引擎stonedb在centos7.9编译及运行
[linear algebra] determinant of order 1.3 n
[Digital IC hand tearing code] Verilog burr free clock switching circuit | topic | principle | design | simulation
【编译原理】做了一半的LR(0)分析器
qt quick项目offscreen模式下崩溃的问题处理
MySQL约束的分类、作用及用法
BasicVSR_PlusPlus-master测试视频、图片
项目复盘模板
How to use flexible arrays?
【雅思口语】安娜口语学习记录part1
新手程序员该不该背代码?
小程序系统更新提示,并强制小程序重启并使用新版本
Aardio - 通过变量名将变量值整合到一串文本中
MySQL数据库基本操作-DML