hdu 1800 Flying to the Mars 字典树
来源:互联网 发布:数控机床螺纹编程实例 编辑:程序博客网 时间:2024/05/16 08:06
给出一些个长度可能爆long long的数,每个长度不超过30位,问同一个数最多出现多少次。
显而易见字典树,6因为long long存不下。
注意输入中有的数字有前导零。
下面的代码中找最大值写麻烦了,我用的是树形dp,因为只需求这棵树里面访问最多的结点,故只需用一个变量ans保存结果即可。
/**========================================== * This is a solution for ACM/ICPC problem * * @source:hdu 1800 Flying to the Mars * @type: dp * @author: wust_ysk * @blog: http://blog.csdn.net/yskyskyer123 * @email: 2530094312@qq.com *===========================================*/#include<cstdio>#include<string>#include<cstring>#include<iostream>#include<cmath>#include<algorithm>using namespace std;typedef long long ll;const int INF =0x3f3f3f3f;const int maxn= 3000 ;const int maxlen=30 ;int n;int root,cnt;struct Node{ int nex[10],cnt; bool end; Node() { for(int i=0;i<10;i++) { nex[i]=-1; cnt=0; } }} nodes[(maxn+3)*(maxlen+3)];int dp[(maxn+3)*(maxlen+3)];int newnode(){ nodes[++cnt]=Node(); return cnt;}void init(){ cnt=0; root=newnode();}void insert(char *s){ int p=root; for(int i=0;s[i];i++) { int x=s[i]-'0'; if(nodes[p].nex[x]==-1) nodes[p].nex[x]=newnode(); p=nodes[p].nex[x]; } nodes[p].cnt++;}void dfs(int x){ dp[x]=nodes[x].cnt; for(int i=0;i<10;i++) if(~nodes[x].nex[i]) { int y=nodes[x].nex[i]; dfs(y); dp[x]=max(dp[y],dp[x]); }}int main(){ char s[maxlen+5]; while(~scanf("%d",&n)) { init(); for(int i=1;i<=n;i++) { scanf("%s",s); int j; for( j=0;s[j]=='0';j++); insert(s+j); } dfs(1); printf("%d\n",dp[1]); } return 0;}
字典树是处理字符串问题的一种方法,这里同时可以处理数的问题,因为数可以当作字符串处理。
0 0
- hdu 1800 Flying to the Mars(字典树)
- [字典树、map] HDU 1800 - Flying to the Mars
- hdu 1800 Flying to the Mars 字典树
- HDU 1800 Flying to the Mars(字典树)
- hdu 1800 Flying to the Mars 字典树
- hdu 1800 Flying to the Mars 字典树
- hdu 1800 Flying to the Mars(字典树统计)
- hdu 1800 Flying to the Mars (字典树)
- 字典树水题Flying to the Mars HDU 1800
- HDU 1800 Flying to the Mars(字典树水题)
- 【字典树】 hdu1800 Flying to the Mars
- hdu1800 Flying to the Mars (字典树)
- Hdu1800 - Flying to the Mars - 字典树
- HDU1800 Flying to the Mars【字典树】
- Flying to the Mars(字典树)
- hdu1800 Flying to the Mars--字典树
- Flying to the Mars hdu1800 字典树
- hdu 1800 Flying to the Mars(HashMap水过,可用字典树)
- 考考你的页面跳转
- 小结
- 《图解HTTP》读书笔记
- 【Mapreduce】去除重复的行
- Git 学习笔记(一)
- hdu 1800 Flying to the Mars 字典树
- 如何将Oracle数据库中的数据导入到hbase中 使用 Sqoop工具
- MAC 安装 Android studio 教程
- RemObjects SDK 9.0 Beta_CodeFirst
- spark load file的几种方式
- 【HNOI2008】【BZOJ1008】越狱
- qemu-kvm virtio 虚拟化-----Linux客户机 virtio设备初始化
- Android 关于屏幕的一些事儿
- 虚拟机 开发板 PC机 三者之间不能ping通的各种原因分析