当前位置:网站首页>UVa 11732 – strcmp() Anyone?
UVa 11732 – strcmp() Anyone?
2022-07-06 15:10:00 【全栈程序员站长】
大家好,又见面了,我是全栈君。
称号:给你一些话,给你一个字符串比较函数,所有的话都当奇偶校验,什么是比较次数。
分析:弦、特里。
首先。看数据大小,假设正常的发现线索,会议TLE和MLE。
由于,常规的字典树深度为1000,并且有可能会有大量的指正空间浪费。
所以,採用树的压缩算法(左兄弟,右孩子)。能够提高执行效率。
然后。在字典树上同级就可以。统计时,能够先建树再统计,或者边建树边统计。
这里我选用边建树边统计的方法(网上大量的做法,都是先建树再统计的,搜索求解)
每次插入单词时。它仅仅与前面插入的单词比較;单词的比較次数为当中每一个字母的比較次数的和。
单词中每一个字母的比較次数。就是这个字母的根节点包括的单词个数。
单词比較函数例如以下:
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];
}
由于比較函数如上,计算时要注意下面几点:
1.同样长度为L的两个单词比較代价为2L-1。出最后一次外要进行s结束的推断;
2.单词的比較长度应该为strlen(str)+1,结束符也是比較的一环;
3.假设两个单词同样,则多计算一次结束推断。比較次数+1。
注意:使用LL,否则数据会溢出。
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
using namespace std;
typedef long long LL;
char words[1010];
/* Trie define */
#define nodesize 4444444 //节点个数
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;
}
版权声明:本文博主原创文章。博客,未经同意不得转载。
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/116985.html原文链接:https://javaforall.cn
边栏推荐
- AI enterprise multi cloud storage architecture practice | Shenzhen potential technology sharing
- ResNet-RS:谷歌领衔调优ResNet,性能全面超越EfficientNet系列 | 2021 arxiv
- Management background --3, modify classification
- Hardware development notes (10): basic process of hardware development, making a USB to RS232 module (9): create ch340g/max232 package library sop-16 and associate principle primitive devices
- The ceiling of MySQL tutorial. Collect it and take your time
- 网络基础入门理解
- UDP programming
- Mysql database basic operations DML
- 做国外LEAD2022年下半年几点建议
- Assembly and Interface Technology Experiment 6 - ADDA conversion experiment, AD acquisition system in interrupt mode
猜你喜欢
Leetcode exercise - Sword finger offer 26 Substructure of tree
Attack and defense world miscall
Export MySQL table data in pure mode
Leetcode question brushing (XI) -- sequential questions brushing 51 to 55
Mysql database basic operations DML
Web APIs DOM time object
MySQL数据库基本操作-DML
The nearest common ancestor of binary (search) tree ●●
树的先序中序后序遍历
Clip +json parsing converts the sound in the video into text
随机推荐
UDP programming
volatile关键字
[Digital IC hand tearing code] Verilog burr free clock switching circuit | topic | principle | design | simulation
Balanced Multimodal Learning via On-the-fly Gradient Modulation(CVPR2022 oral)
基於 QEMUv8 搭建 OP-TEE 開發環境
柔性数组到底如何使用呢?
PVL EDI 项目案例
第3章:类的加载过程(类的生命周期)详解
二叉(搜索)树的最近公共祖先 ●●
AI enterprise multi cloud storage architecture practice | Shenzhen potential technology sharing
Common sense: what is "preservation" in insurance?
LeetCode 练习——剑指 Offer 26. 树的子结构
extern关键字
Seata聚合 AT、TCC、SAGA 、 XA事务模式打造一站式的分布式事务解决方案
2022-07-04 the high-performance database engine stonedb of MySQL is compiled and run in centos7.9
Management background --4, delete classification
UE4蓝图学习篇(四)--流程控制ForLoop和WhileLoop
Unity3d Learning Notes 6 - GPU instantiation (1)
What are the interface tests? What are the general test points?
GD32F4XX串口接收中断和闲时中断配置