要想通这个题目应该很容易,由于斐波纳契数在近100项之后很大,早就超出long long了。而输入最长的序列才40个数字,所以大约保留前50位,前40位是没有误差的!!!其实,想想我们判断double数字相等时fabs(a-b)<1e-10来忽略double数字由于加法带来的误差,道理是一样的,甚至C++默认的还是1e-5。而保险起见,我的方法中保留了100位。当然这个保留50位的数字加法,也只能模拟一遍,因为要计算10^5项。

  然而测试样例有50000组,所以我们先要对输入的字符串做一个字典树!虽然我没学过也没听过,后来AC了队友跟我说这个叫字典树,写个博客纪念一下。

  YY一下,我弹了近8次,前3次因为没有建字典树超时,后5次竟然是因为第0项也是计入的,斐波那契序列为1,1,2,3,5,8……。而我竟然认为第一个1不算的。擦擦,其实样例都没过,我一直以为过了,然后还找不到思路错哪了。我是有多眼瞎啊!!!今年还有最后一次机会,希望比赛拿点成绩,千万别再坑这里了。

 #include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
const int maxlen=;
const int maxn=maxlen+;
char target[];
int active_id,active_idx;
void ccplus(char *&ch,int &cl,char *&sh,int &sl){
if(cl < sl) ch--,cl++, ch[]='';
char carry=; int idx=cl;
while(idx--){
ch[idx] += sh[idx]+carry-'';
if(ch[idx] > '') ch[idx]-=, carry=;
else carry=;
}
if(carry) ch--,cl++, ch[]='';
if(cl > ) cl--, sl--;
ch[cl]='\0';
//if(active_idx <= 25) printf("active_idx = %d, ch = %s, cl = %d\n",active_idx,ch,cl);
swap(ch,sh); swap(cl,sl);
}
char f1[maxn],f2[maxn];
char *ch,*sh;
int cl,sl;
const int maxt=;
struct tree{
int id;
int next[];
void init(int i=-){ id=i; for(int j=;j<;j++) next[j]=-; }
}tr[maxt*];
int tcnt=;
int value[maxt],idnext[maxt];
void make_tree(int idx,char *tt){
//printf("idx = %d, tt[1] = %s\n",idx,tt+1); //char cc; cin>>cc;
if(tt[]=='\0'){
if(tr[idx].id <= ) tr[idx].id = active_id;
else {
int t = tr[idx].id;
while(idnext[t] > ) t = idnext[t];
idnext[t] = active_id;
}
return;
}
int ne=tt[]-'';
//if(ne < 0) cout<<"fuck ne = "<<ne<<endl;
if(tr[idx].next[ne] <= )
tr[idx].next[ne]=tcnt, tr[tcnt].init(), tcnt++;
make_tree(tr[idx].next[ne],tt+);
}
void search_tree(int idx,char *tt){
int id=tr[idx].id;
if(id > && value[id] < ) {
value[id]=active_idx;
while(idnext[id] > ) id=idnext[id], value[id]=active_idx;
}
int ne=tt[]-'';
if(tt[] == '\0' || tr[idx].next[ne] <= ) return;
search_tree(tr[idx].next[ne],tt+);
}
int main()
{
int cases; cin>>cases;
memset(idnext,-,sizeof(idnext));
memset(value,-,sizeof(value));
tr[].init();
for(int cas=;cas<=cases;cas++) {
scanf("%s",target+);
active_id = cas;
make_tree(,target);
//cout<<"case "<<cas<<" is over!"<<endl;
}
ch=&f1[maxn-],sh=&f2[maxn-];
ch[]='', ch[]='\0';
sh[]='', sh[]='\0';
cl=sl=;
active_idx=; search_tree(,ch-);
active_idx=; search_tree(,ch-);
active_idx=; search_tree(,sh-);
active_idx=;
while(active_idx < maxlen)
ccplus(ch,cl,sh,sl), search_tree(,sh-), active_idx++;
for(int cas=;cas<=cases;cas++)
printf("Case #%d: %d\n",cas,value[cas]);
return ;
}

  

hdu4099的更多相关文章

  1. HDU4099 Revenge of Fibonacci(高精度+Trie)

    Revenge of Fibonacci Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 204800/204800 K (Java/ ...

  2. HDU4099(斐波那契数列与字典树)

    题目:Revenge of Fibonacci 题意:给出斐波那契数列的前k位,k不超过40,找出最小的正整数n,满足F(n)的前k位与给定数的前k位相同,斐波那契数列的项数不超过100000. 解析 ...

  3. hdu4099 Revenge of Fibonacci 字典树

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4099 思想很容易想到 就是预处理出前10w个的fib数,然后建树查询 建树时只用前40位即可,所以在计 ...

  4. HDU--4099

    题目: Revenge of Fibonacci 原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=4099 分析:字典树的应用.在求Fibonacci的前 ...

  5. hdu4099 Revenge of Fibonacci

    题意:给定fibonacci数列,输入前缀,求出下标.题目中fibonacci数量达到100000,而题目输入的前缀顶多为40位数字,这说明我们只需要精确计算fibinacci数前40位即可.查询时使 ...

  6. hdoj4099(字典树+高精度)

    题目链接:https://vjudge.net/problem/HDU-4099 题意:给T组询问,每个询问为一个字符串(长度<=40),求以该字符串为开始的fibonacci数列的第一个元素的 ...

随机推荐

  1. javascript高级知识分析——作为对象的函数

    代码信息来自于http://ejohn.org/apps/learn/. 函数和对象是不是非常相似? var obj = {}; var fn = function(){}; console.log( ...

  2. Maven命令行创建web项目,并部署到jobss当中(解决No plugin found for prefix 'jboss-as' in the current project and in the plugin groups [org.apache.maven.plugins,问题)

    首件创建项目:此处可参照:http://maven.apache.org/guides/mini/guide-webapp.html mvn archetype:generate -DgroupId= ...

  3. C# 创建数组的几种方法

    第一种 string[] myArray=new string[10]; 第二种 string[] myArray={"1","2"}; 第三种 string[ ...

  4. gcc -lpthread 干什么用

    #include <stdio.h> #include <pthread.h> void *ThreadFunc(void *pArg)  //参数的值为123 { int i ...

  5. Linux学习之服务器端口查看的方法

    1.用netstat查看: [grid@rac121 admin]$ netstat -anp | grep oracle (Not all processes could be identified ...

  6. Apache新版配置虚拟主机的注意事项

    1.关于没有默认索引文件(index.php或者index.html)时,列出目录:需要开启模块 LoadModule autoindex_module modules/mod_autoindex.s ...

  7. hibernate中多对多关联

    hibernate中多对多关联 “计应134(实验班) 凌豪” 在关系数据库中有一种常见的关系即多对多关系,例如课程和学生的关系,一个学生可以选择多门课程,同时一门课程也可以被多个学生选择, 因此课程 ...

  8. Latex命令笔记

    1.\documentclass[hyperref, UTF8]{ctexart} 2.\numberwithin{equation}{section} %article中让公式按章节名编号 3.\p ...

  9. 黑马程序员-- C语言交换两个整数变量值几种函数比较

    总结了C语言中几种交换两个整数数值的函数,欢迎交流 #include <stdio.h> 使用多种交换变量值的函数比较 方法一:使用第三方临时变量 这种函数a,b只是值传递,实质上不能修交 ...

  10. Html5离线应用程序

    最近,整理了一下关于 H5离线应用缓存的知识,今天在家休息,和大家分享一下,希望对大的学习和工作,能有所帮助. HTML5的离线web应用允许我们在脱机时与网站进行交互.这在提高网站的访问速度和制作一 ...