HDU 4099 Revenge of Fibonacci (数学+字典数)
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=4099
这个题目就是一个坑或。
题意:给你不超过40的一串数字,问你这串数字是Fibonacci多少的开头几位数字,如果不存在则输出-1.
题解:明明说好的不超过40,但是在建字典数的时候不加i<41就超内存了,杭电你是想咋地,害的我比较好多人的代码,一点一点试出来的。
AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <list>
#include <deque>
#include <queue>
#include <iterator>
#include <stack>
#include <map>
#include <set>
#include <algorithm>
#include <cctype>
using namespace std; #define si1(a) scanf("%d",&a)
#define si2(a,b) scanf("%d%d",&a,&b)
#define sd1(a) scanf("%lf",&a)
#define sd2(a,b) scanf("%lf%lf",&a,&b)
#define ss1(s) scanf("%s",s)
#define pi1(a) printf("%d\n",a)
#define pi2(a,b) printf("%d %d\n",a,b)
#define mset(a,b) memset(a,b,sizeof(a))
#define forb(i,a,b) for(int i=a;i<b;i++)
#define ford(i,a,b) for(int i=a;i<=b;i++) typedef __int64 LL;
const int N=10;
const int INF=0x3f3f3f3f;
const double PI=acos(-1.0);
const double eps=1e-7; char str[4][100]; struct Trie
{
int v;
Trie *next[N];
Trie()
{
v=-1;
for(int i=0;i<N;i++)
next[i]=NULL;
}
}*root; void creat_trie(char s[],int x)
{
int len=strlen(s);
Trie *p=root;
for(int i=0;i<len&&i<41;i++)//这个地方太肯爹了,明明说好的不超过40,不加i<41就超内存了,杭电你是想咋地
{
int id=s[i]-'0';
if(p->next[id]==NULL)
p->next[id]=new Trie();
p=p->next[id];
if(p->v<0)
p->v=x;
}
} void add(char a[],char b[],char c[])
{
int lena=strlen(a)-1,lenb=strlen(b)-1;
int k=0,up=0;
int x,y,z;
while(lena>=0||lenb>=0)
{
if(lena<0) x=0;
else x=a[lena]-'0'; if(lenb<0) y=0;
else y=b[lenb]-'0';
z=x+y+up;
c[k++]=z%10+'0';
up=z/10;
lena--;
lenb--;
}
if(up>0) c[k++]=up+'0';
c[k]=0;
for(int i=0;i<k/2;i++)
swap(c[i],c[k-i-1]);
// cout<<k<<" "<<c<<endl;system("pause");
} int find_trie(char st[])
{
Trie *p=root;
int len=strlen(st);
int tmp;
for(int i=0;i<len;i++)
{
int id=st[i]-'0';
if(p->next[id]==NULL)
return -1;
else
{
p=p->next[id];
tmp=p->v;
}
}
return tmp;
} void init()
{
str[1][0]='1'; str[1][1]=0;
creat_trie(str[1],0);
str[2][0]='1'; str[2][1]=0;
creat_trie(str[2],1);
for(int i=2;i<100000;i++)//注意题目是小于,不能取等号。。
{
int len1=strlen(str[1]);
int len2=strlen(str[2]);
if(len2>60)//舍去地位
{
str[2][len2-1]=0;
str[1][len1-1]=0;
}
add(str[1],str[2],str[3]); creat_trie(str[3],i);
strcpy(str[1],str[2]);
strcpy(str[2],str[3]);
}
} int main()
{
// freopen("input.txt","r",stdin);
root=new Trie();
init();
int T,ca=0;
char st[66];
si1(T);
while(T--)
{
ss1(st);
printf("Case #%d: %d\n",++ca,find_trie(st));
}
return 0 ;
}
HDU 4099 Revenge of Fibonacci (数学+字典数)的更多相关文章
- HDU 4099 Revenge of Fibonacci(高精度+字典树)
题意:对给定前缀(长度不超过40),找到一个最小的n,使得Fibonacci(n)前缀与给定前缀相同,如果在[0,99999]内找不到解,输出-1. 思路:用高精度加法计算斐波那契数列,因为给定前缀长 ...
- hdu 4099 Revenge of Fibonacci 大数+压位+trie
最近手感有点差,所以做点水题来锻炼一下信心. 下周的南京区域赛估计就是我的退役赛了,bless all. Revenge of Fibonacci Time Limit: 10000/5000 MS ...
- HDU 4099 Revenge of Fibonacci Trie+高精度
Revenge of Fibonacci Problem Description The well-known Fibonacci sequence is defined as following: ...
- hdu 4099 Revenge of Fibonacci Trie树与模拟数位加法
Revenge of Fibonacci 题意:给定fibonacci数列的前100000项的前n位(n<=40);问你这是fibonacci数列第几项的前缀?如若不在前100000项范围内,输 ...
- hdu 4099 Revenge of Fibonacci 字典树+大数
将斐波那契的前100000个,每个的前40位都插入到字典树里(其他位数删掉),然后直接查询字典树就行. 此题坑点在于 1.字典树的深度不能太大,事实上,超过40在hdu就会MLE…… 2.若大数加法时 ...
- hdu 5018 Revenge of Fibonacci
大水题 #include<time.h> #include <cstdio> #include <iostream> #include<algorithm&g ...
- HDU4099 Revenge of Fibonacci(高精度+Trie)
Revenge of Fibonacci Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 204800/204800 K (Java/ ...
- UVA 12333 Revenge of Fibonacci
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- [刷题]算法竞赛入门经典(第2版) 5-15/UVa12333 - Revenge of Fibonacci
题意:在前100000个Fibonacci(以下简称F)数字里,能否在这100000个F里找出以某些数字作为开头的F.要求找出下标最小的.没找到输出-1. 代码:(Accepted,0.250s) / ...
随机推荐
- jQuery中使用 .html() function在IE8和9中显示不正常源码中多出sizset和sizcache
错误原因:在引入jquery的时候,使用了html function,在IE8和IE9下面有可能会出现不兼容 解决办法:在html头部加一句 <meta http-equiv="X-U ...
- phpmyadmin登陆提示#2002 无法登录 MySQL 服务器和设置自增
看看mysql启动没有,结果是mysql服务没有启动,找了半天,是这个原因,那就右键计算机->管理->服务->启动mysql服务 设置自增:在显示出来的一行字段定义中把浏览器的滚动条 ...
- String or binary data would be truncated
在使用Typed Dataset进行数据的插入时,会报这样的错:String or binary data would be truncated. 我碰到的原因是 数据库中字段的长度过段,插入时内容被 ...
- VS2013打包与部署
近期做一个配置工具,完事了想打包一下:由于用的是VS2013:与之前的略有不同,简单的做了一下,在这里分享一下,直接看吧: 首先 是自己新建一个项目 ,我的WPF应用程序 第二步:右键解决方案添加新 ...
- Ajax--JavaScript实现
Ajax:一种不用刷新整个页面便可与服务器通讯的办法 Ajax实现的步骤: 1.创建XMLHttpRequest对象 2.服务器向浏览器响应请求(注册监听) 3.浏览器与服务器建立连接 4.浏览器向服 ...
- java对象初级知识
this属于类 方法中没有自己的this指针(本来以为js方法中有,其实并没有里,那是被new出来的function 在声明的时候可以赋初值: int a =12 但不能 int a ; a ...
- 猜数字-js
var n = Math.round(Math.random()*10); //随机数 // alert(n); while(true){ var Onum = prompt('请输入1-10之间的数 ...
- Array and its point.
a is the array name. &a is the ponit of 2-D array which contains a[5]. the type of &a should ...
- 【学习笔记】【Foundation】集合Set
不可变集合 NSSet :集合元素无顺序,没有索引号,元素不可重复. NSSet在功能上可看做是NSArray的父集,它是一个更通用的类. NSSet包含如下常用方法: setByAddingObje ...
- JQuery的$和其它JS发生冲突的快速解决方法
众所周知,jQuery是目前最流行的JS封装包,简化了很多复杂的JS程序,JQuery讲浏览器DOM树定义为$,通过$来获取各个子节点. 然后,JS插件并非只有JQuery,还有prototype.j ...