统计难题 HDU - 1251 + Phone List HDU - 1671

2024-02-22 19:18

本文主要是介绍统计难题 HDU - 1251 + Phone List HDU - 1671,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

点击打开链接

两道字典树模板 没啥说的 借鉴博客点击打开链接

1251可能是哪里有bug hdu上用G++提交就会MLE 只能过C++ 待解。。

但用同样方法写1671就没问题 难道是数据水

 

hdu1251

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;struct node
{node *next[26];int cnt;
};node *root;void getnode(node *& ptr)
{int i;ptr=new node;for(i=0;i<26;i++) ptr->next[i]=NULL;ptr->cnt=0;
}void update(node *ptr,char *ch,int cur,int len)
{if(cur==len) return;if(ptr->next[ch[cur]-'a']==NULL) getnode(ptr->next[ch[cur]-'a']);ptr->next[ch[cur]-'a']->cnt++;update(ptr->next[ch[cur]-'a'],ch,cur+1,len);
}int query(node* ptr,char *ch,int cur,int len)
{if(ptr->next[ch[cur]-'a']==NULL) return 0;if(cur==len-1) return ptr->next[ch[cur]-'a']->cnt;return query(ptr->next[ch[cur]-'a'],ch,cur+1,len);
}void destruct(node *ptr)
{int i;for(i=0;i<26;i++){if(ptr->next[i]!=NULL) destruct(ptr->next[i]);}delete ptr;
}int main()
{int len;char ch[20];getnode(root);while(gets(ch)&&ch[0]!=0){len=strlen(ch);update(root,ch,0,len);}while(gets(ch)&&ch[0]!=0){len=strlen(ch);printf("%d\n",query(root,ch,0,len));}destruct(root);return 0;
}

 

hdu1671

指针版

#include <bits/stdc++.h>
using namespace std;struct node
{node *next[10];int cnt;
};node *root;
int n;void getnode(node *& ptr)
{int i;ptr=new node;for(i=0;i<10;i++) ptr->next[i]=NULL;ptr->cnt=0;
}void destruct(node *ptr)
{int i;for(i=0;i<10;i++){if(ptr->next[i]!=NULL){destruct(ptr->next[i]);}}delete ptr;
}int update(node *ptr,char *ch,int flag1,int flag2,int cur,int len)
{if(ptr->next[ch[cur]-'0']==NULL){flag1=1;getnode(ptr->next[ch[cur]-'0']);}if(ptr->next[ch[cur]-'0']->cnt!=0) flag2=0;if(cur==len-1){ptr->next[ch[cur]-'0']->cnt=1;return flag1&flag2;//flag1判断当前串是不是之前某个串的前缀 flag2判断当前串是否已经出现子串}return update(ptr->next[ch[cur]-'0'],ch,flag1,flag2,cur+1,len);
}int main()
{int t,res;char ch[20];scanf("%d",&t);while(t--){getnode(root);scanf("%d",&n);res=1;while(n--){scanf("%s",ch);if(res) res=update(root,ch,0,1,0,strlen(ch));}if(res) printf("YES\n");else printf("NO\n");destruct(root);}return 0;
}

数组版

#include <bits/stdc++.h>
using namespace std;struct node
{int next[10];int cnt;
};node tree[100010];
int num;void init()
{memset(tree,0,sizeof(tree));num=1;
}int update(char *ch,int f1,int f2)
{int len,cur,i,id;len=strlen(ch),cur=0;for(i=0;i<len;i++){id=ch[i]-'0';if(tree[cur].next[id]==0){tree[cur].next[id]=num++;f1=1;}if(tree[tree[cur].next[id]].cnt) f2=0;if(i==len-1) tree[tree[cur].next[id]].cnt=1;cur=tree[cur].next[id];}return f1&f2;
}int main()
{int t,n,res;char ch[20];scanf("%d",&t);while(t--){init();scanf("%d",&n);res=1;while(n--){scanf("%s",ch);if(res) res=update(ch,0,1);}if(res) printf("YES\n");else printf("NO\n");}return 0;
}

 

这篇关于统计难题 HDU - 1251 + Phone List HDU - 1671的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/736228

相关文章

C#之List集合去重复对象的实现方法

《C#之List集合去重复对象的实现方法》:本文主要介绍C#之List集合去重复对象的实现方法,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录C# List集合去重复对象方法1、测试数据2、测试数据3、知识点补充总结C# List集合去重复对象方法1、测试数据

详解如何使用Python从零开始构建文本统计模型

《详解如何使用Python从零开始构建文本统计模型》在自然语言处理领域,词汇表构建是文本预处理的关键环节,本文通过Python代码实践,演示如何从原始文本中提取多尺度特征,并通过动态调整机制构建更精确... 目录一、项目背景与核心思想二、核心代码解析1. 数据加载与预处理2. 多尺度字符统计3. 统计结果可

Python中合并列表(list)的六种方法小结

《Python中合并列表(list)的六种方法小结》本文主要介绍了Python中合并列表(list)的六种方法小结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋... 目录一、直接用 + 合并列表二、用 extend() js方法三、用 zip() 函数交叉合并四、用

Java List排序实例代码详解

《JavaList排序实例代码详解》:本文主要介绍JavaList排序的相关资料,Java排序方法包括自然排序、自定义排序、Lambda简化及多条件排序,实现灵活且代码简洁,文中通过代码介绍的... 目录一、自然排序二、自定义排序规则三、使用 Lambda 表达式简化 Comparator四、多条件排序五、

Java使用Stream流的Lambda语法进行List转Map的操作方式

《Java使用Stream流的Lambda语法进行List转Map的操作方式》:本文主要介绍Java使用Stream流的Lambda语法进行List转Map的操作方式,具有很好的参考价值,希望对大... 目录背景Stream流的Lambda语法应用实例1、定义要操作的UserDto2、ListChina编程转成M

Pandas中统计汇总可视化函数plot()的使用

《Pandas中统计汇总可视化函数plot()的使用》Pandas提供了许多强大的数据处理和分析功能,其中plot()函数就是其可视化功能的一个重要组成部分,本文主要介绍了Pandas中统计汇总可视化... 目录一、plot()函数简介二、plot()函数的基本用法三、plot()函数的参数详解四、使用pl

Pandas统计每行数据中的空值的方法示例

《Pandas统计每行数据中的空值的方法示例》处理缺失数据(NaN值)是一个非常常见的问题,本文主要介绍了Pandas统计每行数据中的空值的方法示例,具有一定的参考价值,感兴趣的可以了解一下... 目录什么是空值?为什么要统计空值?准备工作创建示例数据统计每行空值数量进一步分析www.chinasem.cn处

Mysql如何将数据按照年月分组的统计

《Mysql如何将数据按照年月分组的统计》:本文主要介绍Mysql如何将数据按照年月分组的统计方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录mysql将数据按照年月分组的统计要的效果方案总结Mysql将数据按照年月分组的统计要的效果方案① 使用 DA

Java中List的contains()方法的使用小结

《Java中List的contains()方法的使用小结》List的contains()方法用于检查列表中是否包含指定的元素,借助equals()方法进行判断,下面就来介绍Java中List的c... 目录详细展开1. 方法签名2. 工作原理3. 使用示例4. 注意事项总结结论:List 的 contain

java streamfilter list 过滤的实现

《javastreamfilterlist过滤的实现》JavaStreamAPI中的filter方法是过滤List集合中元素的一个强大工具,可以轻松地根据自定义条件筛选出符合要求的元素,本文就来... 目录1. 创建一个示例List2. 使用Stream的filter方法进行过滤3. 自定义过滤条件1. 定