(字典树)Revenge of Fibonacci -- HDU -- 4099
链接:
http://acm.hdu.edu.cn/showproblem.php?pid=4099
要用c++交哦, G++ MLE
不是很懂,先粘上慢慢学习
代码:
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
using namespace std; #define N 100 struct node
{
int ID;
node *next[];
}*Head; char c[], str[][]; void Add(char a[], char b[], char back[]) ///计算a+b, 结果存入c
{
int i, j, k;
int x, y, z;
int up; i = strlen(a)-;
j = strlen(b)-;
k = ;
up = ; while(i>= || j>=)
{
if(i<) x = ;
else x = a[i]-''; if(j<) y = ;
else y = b[j]-''; z = x+y+up;
c[k++] = z%+'';
up = z/;
i--;
j--;
}
if(up>) c[k++] = up+'';
for(i=; i<k; i++)
back[i] = c[k--i];
back[k] = '\0';
} void Tree_Insert(char str[], int Index)///插入单词
{
node *t, *s = Head;
int i;
int len = strlen(str); for(i=; i<len && i<; i++)
{
int id = str[i]-'';
if(s->next[id]==NULL)
{
t = new node();
for(int i=;i<;i++)t->next[i]=NULL;
t->ID = -;
s->next[id] = t;
}
s = s->next[id];
if(s->ID<) s->ID = Index;
}
} int Tree_Find(char str[])
{
node *s=Head;
int count, i;
int len = strlen(str); for(i=; i<len; i++)
{
int id = str[i]-'';
if(s->next[id]==NULL)
return -;
else
{
s = s->next[id];
count = s->ID;
}
}
return count;
} void Tree_Del(node *p)
{
for(int i=; i<; i++)
{
if(p->next[i]!=NULL)
Tree_Del(p->next[i]);
}
free(p);
} int main()
{
Head = new node();
for(int i=;i<;i++)Head->next[i]=NULL; Head->ID = -;
str[][] = '';
str[][] = ;
Tree_Insert(str[], ); str[][]='';
str[][]=;
Tree_Insert(str[], ); for(int i=; i<; i++)
{
int len1 = strlen(str[]);
int len2 = strlen(str[]); if(len2>)
{
str[][len2-]=;
str[][len1-]=;
}
Add(str[], str[], str[]); /// printf("%s\n",str[2]); Tree_Insert(str[], i);
strcpy(str[], str[]);
strcpy(str[], str[]); /// for(int i=0;i<100;i++)str[0][i]=str[1][i];
/// for(int i=0;i<100;i++)str[1][i]=str[2][i];
} int t, iCase=;
char str1[];
scanf("%d", &t);
while(t--)
{
scanf("%s", str1);
printf("Case #%d: %d\n", iCase++, Tree_Find(str1));
} Tree_Del(Head);
return ;
}
(字典树)Revenge of Fibonacci -- HDU -- 4099的更多相关文章
- 字典树 && 例题 Xor Sum HDU - 4825 (板子)
一.字典树描述:Trie树,即字典树,又称单词查找树或键树,是一种树形结构,是一种哈希树的变种.典型应用是用于统计和排序大量的字符串(但不仅限于字符串),所以经常被搜索引擎系统用于文本词频统计.它的优 ...
- BestCoder10 1001 Revenge of Fibonacci(hdu 5018) 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5018 题目意思:给出在 new Fibonacci 中最先的两个数 A 和 B(也就是f[1] = A ...
- HDU 4099 Revenge of Fibonacci(高精度+字典树)
题意:对给定前缀(长度不超过40),找到一个最小的n,使得Fibonacci(n)前缀与给定前缀相同,如果在[0,99999]内找不到解,输出-1. 思路:用高精度加法计算斐波那契数列,因为给定前缀长 ...
- hdu 4099 Revenge of Fibonacci 字典树+大数
将斐波那契的前100000个,每个的前40位都插入到字典树里(其他位数删掉),然后直接查询字典树就行. 此题坑点在于 1.字典树的深度不能太大,事实上,超过40在hdu就会MLE…… 2.若大数加法时 ...
- hdu 4099 Revenge of Fibonacci Trie树与模拟数位加法
Revenge of Fibonacci 题意:给定fibonacci数列的前100000项的前n位(n<=40);问你这是fibonacci数列第几项的前缀?如若不在前100000项范围内,输 ...
- UVA - 12333 Revenge of Fibonacci 高精度加法 + 字典树
题目:给定一个长度为40的数字,问其是否在前100000项fibonacci数的前缀 因为是前缀,容易想到字典树,同时因为数字的长度只有40,所以我们只要把fib数的前40位加入字典树即可.这里主要讨 ...
- UVa 12333 - Revenge of Fibonacci manweifc(模拟加法竖式 & 字典树)
题意: 给定n个(n<=40)数字, 求100000个以内有没有前面n个数字符合给定的数字的fibonacci项, 如果有, 给出最小的fibonacci项, 如果没有, 输出-1. 分析: 可 ...
- hdu 4099 Revenge of Fibonacci 大数+压位+trie
最近手感有点差,所以做点水题来锻炼一下信心. 下周的南京区域赛估计就是我的退役赛了,bless all. Revenge of Fibonacci Time Limit: 10000/5000 MS ...
- UVA-12333 Revenge of Fibonacci(竖式加法模拟 & 字典树)
题目: 给出一个斐波那契数字的前缀,问第一个有这个前缀的数字在斐波那契数列中是第几个. 思路: 紫书提示:本题有一定效率要求.如果高精度代码比较慢,可能会超时. 利用滚动数组和竖式加法来模拟斐波那契相 ...
随机推荐
- eclipse调试时增加jvm参数
下面的程中我们限制Java 堆的大小为20MB,不可扩展(将堆的最小值-Xms 参数与最大值-Xmx 参数设置为一样即可避免堆自动扩展),通过参数-XX:+HeapDumpOnOutOfMemoryE ...
- 自动手动随便你 Win7驱动程序安装自己设
Win7系统是非常智能方便的操作系统,可以自动安装硬件驱动程序,为用户提供了很多方便.但是并不是所有的驱动程序和硬件都能完美兼容,如果不合适就需要卸载了重新安装:还有一些朋友就习惯自己安装驱动,那么, ...
- 蓝桥杯历届试题-垒色子(DP+矩阵快速幂)
一.题目 垒骰子 赌圣atm晚年迷恋上了垒骰子,就是把骰子一个垒在另一个上边,不能歪歪扭扭,要垒成方柱体.经过长期观察,atm 发现了稳定骰子的奥秘:有些数字的面贴着会互相排斥!我们先来规范一下骰子: ...
- python编程遇见的异常
import sys print('目前系统的编码为:',sys.getdefaultencoding()) # 目前系统的编码为: utf-8 name = 'this is a test!' pr ...
- php利用curl实现多进程下载文件类
批量下载文件一般使用循环的方式,逐一执行下载.但在带宽与服务器性能允许的情况下,使用多进程进行下载可以大大提高下载的效率.本文介绍PHP利用curl的多进程请求方法,实现多进程同时下载文件. 原理: ...
- NodeJs通过async/await处理异步
##场景 远古时代 我们在编写express后台,经常要有许多异步IO的处理.在远古时代,我们都是用chunk函数处理,也就是我们最熟悉的那种默认第一个参数是error的函数.我们来模拟一个Mongo ...
- C# 查找其他应用程序并打开、显示、隐藏、关闭
软件开发中,有时迫不得已要用到第三方的软件,这时就涉及到在C#应用程序需要对第三方软件打开.显示.隐藏以及关闭. 下面列举了几个常用的方式 打开应用程序,下面是2种简单用法: 第一种: public ...
- 「小程序JAVA实战」小程序的关注功能(65)
转自:https://idig8.com/2018/09/24/xiaochengxujavashizhanxiaochengxudeguanzhugongneng64/ 在个人页面,根据发布者个人和 ...
- 两种定时器 setInterval(一直执行) setTimeout(只执行一次)
//第一种 var obj = setInterval(function () { console.log(123); clearInterval(obj) }, 1000); //第二种 var t ...
- eclipse cut copy paste plugin
The Cut Copy Paste Plus plug-in enhances the standard Cut, Copy and Paste commands in Eclipse IDE. W ...