hdu4099之字典树

Revenge of Fibonacci

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 204800/204800 K (Java/Others)
Total Submission(s): 1170    Accepted Submission(s): 263


Problem Description
The well-known Fibonacci sequence is defined as following:


  Here we regard n as the index of the Fibonacci number F(n).
  This sequence has been studied since the publication of Fibonacci's book Liber Abaci. So far, many properties of this sequence have been introduced.
  You had been interested in this sequence, while after reading lots of papers about it. You think there’s no need to research in it anymore because of the lack of its unrevealed properties. Yesterday, you decided to study some other sequences like Lucas sequence instead.
  Fibonacci came into your dream last night. “Stupid human beings. Lots of important properties of Fibonacci sequence have not been studied by anyone, for example, from the Fibonacci number 347746739…”
  You woke up and couldn’t remember the whole number except the first few digits Fibonacci told you. You decided to write a program to find this number out in order to continue your research on Fibonacci sequence.
 

Input
  There are multiple test cases. The first line of input contains a single integer T denoting the number of test cases (T<=50000).
  For each test case, there is a single line containing one non-empty string made up of at most 40 digits. And there won’t be any unnecessary leading zeroes.
 

Output
  For each test case, output the smallest index of the smallest Fibonacci number whose decimal notation begins with the given digits. If no Fibonacci number with index smaller than 100000 satisfy that condition, output -1 instead – you think what Fibonacci wants to told you beyonds your ability.
 

Sample Input
  
  
15 1 12 123 1234 12345 9 98 987 9876 98765 89 32 51075176167176176176 347746739 5610
 

Sample Output
  
  
Case #1: 0 Case #2: 25 Case #3: 226 Case #4: 1628 Case #5: 49516 Case #6: 15 Case #7: 15 Case #8: 15 Case #9: 43764 Case #10: 49750 Case #11: 10 Case #12: 51 Case #13: -1 Case #14: 1233 Case #15: 22374
 
n行,没行输入一个字符串,求这个斐波那契数列的第i位含有这个字符串前缀,如果i不存在或>=100000则输出-1

分析:首先计算出斐波那契数列的前100000项并插入字典树,但是要注意的是我们只需要计算前50位即可,因为输入的字符串<=40位,前50位是为了保证进位不会出错
然后每次输入字符串进行字典树查找即可

这里需要注意:在构建字典树的时候只需要构建字符串的前40位,即while(a[k] && k<41),否则MLE

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<queue>
#include<algorithm>
#include<map>
#include<iomanip>
#define INF 99999999
using namespace std;

const int MAX=100+10;
const int Base=10;
char a[MAX],b[MAX],sum[MAX];

struct TrieNode{
	int id;
	TrieNode *next[10];
	TrieNode(){
		id=-1;
		memset(next,0,sizeof next);
	}
}root;

void InsertNode(char *s,int id){
	int k=0;
	TrieNode *p=&root;
	while(s[k] && k<41){
		if(!p->next[s[k]-'0'])p->next[s[k]-'0']=new TrieNode;
		p=p->next[s[k++]-'0'];
		if(p->id == -1)p->id=id;
	}
}

int SearchTrie(char *s){
	int k=0;
	TrieNode *p=&root;
	while(s[k] && p->next[s[k]-'0'])p=p->next[s[k++]-'0'];
	if(s[k])return -1;
	return p->id;
}

void add(){
	int x,y,c=0;
	a[0]=b[0]='1';
	a[1]=b[1]=0;
	InsertNode(a,0);
	for(int i=2;i<100000;++i){
		int lena=strlen(a)-1;//a是0~lena是从高位到低位 
		int lenb=strlen(b)-1;//b是0~lenb是从高位到低位
		int k=0;
		if(lenb>49){//b是较长的串,只需要a,b的前50位即可 
			a[lena--]=0;//保证a,b相应的位置对应,比如a=45,b=456 ==>a=4,b=45,这样前50位不会出错 
			b[lenb--]=0;
		}
		while(lena>=0 || lenb>=0 || c){
			if(lena<0)x=0;
			else x=a[lena--]-'0';
			if(lenb<0)y=0;
			else y=b[lenb--]-'0';
			sum[k++]=(x+y+c)%10+'0';
			c=(x+y+c)/10;
		}
		for(int j=0;j<k/2;++j){ 
			sum[j]=sum[k-1-j]^sum[j];
			sum[k-1-j]=sum[j]^sum[k-1-j];
			sum[j]=sum[j]^sum[k-1-j];
		}
		sum[k]=0;
		//cout<<i<<": "<<sum<<endl;
		//system("pause");
		InsertNode(sum,i);//插入斐波那契数前缀
		strcpy(a,b);
		strcpy(b,sum);
	}
}

int main(){
	add();
	int n;
	while(scanf("%d",&n)!=EOF){ 
		for(int i=0;i<n;++i){
			scanf("%s",a);
			printf("Case #%d: %d\n",i+1,SearchTrie(a));
		}
	}
	return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值