(字典树)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(竖式加法模拟 & 字典树)
题目: 给出一个斐波那契数字的前缀,问第一个有这个前缀的数字在斐波那契数列中是第几个. 思路: 紫书提示:本题有一定效率要求.如果高精度代码比较慢,可能会超时. 利用滚动数组和竖式加法来模拟斐波那契相 ...
随机推荐
- linux shell 模拟post请求
Linux 下curl模拟Http 的get or post请求. 一.get请求 curl "http://www.baidu.com" 如果这里的URL指向的是一个文件或 ...
- 使用用WCF中的双工(Duplex)模式将广告图片推送到每个Winform客户端机子上
参考资料地址:http://www.cnblogs.com/server126/archive/2011/08/11/2134942.html 代码实现: WCF宿主(服务端) IServices.c ...
- springMVC等小知识点记录。。。持续更新
1.springMVC 项目根路径访问页面配置 <!-- 表示当访问主页时自动转发到index控制器 --> <mvc:view-controller path="/&qu ...
- 文件读取错误UnicodeDecodeError: 'utf-8' codec can't decode byte 0x92 in position 884: invalid start byte
参考: https://segmentfault.com/q/1010000004268196/a-1020000004269556 ubuntu下Python3使用open('filename', ...
- [html][javascript] 正则匹配示例
var str="akdlfaklhello 1234klfd1441ksalfd9000kals8998j2345fd;lsa"; var reg = new RegExp(/( ...
- 在 Laravel 5 中使用 Laravel Excel 实现 Excel/CSV 文件导入导出功能
1.简介 Laravel Excel 在 Laravel 5 中集成 PHPOffice 套件中的 PHPExcel ,从而方便我们以优雅的.富有表现力的代码实现Excel/CSV文件的导入和 导出 ...
- Android开发入门——Button绑定监听事件三种方式
import android.app.Activity; import android.os.Bundle;import android.view.View;import android.widget ...
- 深入了解 JPA
转载自:http://www.cnblogs.com/crawl/p/7703679.html 前言:谈起操作数据库,大致可以分为几个阶段:首先是 JDBC 阶段,初学 JDBC 可能会使用原生的 J ...
- Angular2中Input和Output
@Input @Input是用来定义模块的输入的,用来让父模块往子模块传递内容: @Output 子模块自定义一些event传递给父模块用@Output. 对于angular2中的Input和Outp ...
- iOS开发者有价值的工具集
转载于:http://www.cocoachina.com/applenews/devnews/2014/0307/7936.html 我一直比较推崇聪明地工作要远胜于刻苦地工作.使用正确的工具可以帮 ...