Hdu 4099 Revenge of Fibonacci (dictionary tree)

Source: Internet
Author: User

Revenge of Fibonacci Time limit:10000/5000 MS (java/others) Memory limit:204800/204800 K (java/others)
Total submission (s): 2355 Accepted Submission (s): 587


Problem DescriptionThe Well-known Fibonacci sequence is defined as following:


Here we regard N as the index of the Fibonacci number F (n).
This sequence have been studied since the publication of Fibonacci ' s book Liber Abaci. So far, many properties of this sequence has been introduced.
You had been interested in this sequence and while after reading lots of papers about it. You think there's no need to the it anymore because of the lack of its unrevealed properties. Yesterday, you decided to study some and other sequences like Lucas sequence instead.
Fibonacci came into your dream last night. "Stupid human beings. Lots of important properties of Fibonacci sequence has 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. You decided to the write a program to the find this number out of order to continue your the Fibonacci sequence.
Input There is 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, the there is a single line containing one non-empty string made up of the at most of the digits. And there won ' t is 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 this condition, output-1 instead–you think what Fibonacci Wants to told you beyonds your ability.
Sample Input
151121231234123459989879876987658932510751761671761761763477467395610

Sample Output
Case #1:0Case #2:25Case #3:226Case #4:1628Case #5:49516Case #6:15Case #7:15Case #8:15Case #9:43764Case #10:49750 Case #11:10Case #12:51Case #13: -1case #14:1233Case #15:22374
Test instructions: give you a string that is the lowest Fibonacci number subscript, so that this string is the prefix of this Fibonacci number.
The puzzle: The 0~99999 Fibonacci number is stored in the dictionary tree, only the first 40 bits, in the search.
(Note: addition can only save the first 50 bits to ensure accuracy; The 100,000th Fibonacci cannot be pressed into the dictionary tree, WA; The dictionary tree can only be pressed into 40 digits, otherwise it will be hyper-memory)
#include <cstring> #include <iostream> #include <cstdlib> #include <cstdio> #include <string > #include <algorithm> #define N 100005using namespace Std;int n;char s[50],a[60],b[60],c[60];struct Trie {int i    D    struct Trie *next[10];        Trie () {id=-1;        for (int i=0; i<10; i++) {next[i]=null; }    }};    Trie *p;void trie_inser (Trie *p,char s[],int id) {int i=0;    Trie *q=p;        while (s[i]&&i<41) {int nx=s[i]-48;        if (q->next[nx]==null) {q->next[nx]=new Trie;        } q=q->next[nx];        if (q->id==-1) {q->id=id;    } i++;    }}void Add () {int La=strlen (a);    int Lb=strlen (b);    int res[100];    int l=0;    La--, lb--;    int t=0;        while (la>=0) {res[l]= (a[la]-48) + (b[lb]-48) +t;        t=0;            if (res[l]>=10) {res[l]-=10;        T=1;        } la--;        lb--;    l++;   } while (lb>=0) {     Res[l]=b[lb]-48+t;        t=0;            if (res[l]>=10) {res[l]-=10;        T=1;    } lb--, l++;    } if (t) {res[l++]=1;    } int i=0;    int k=0;    if (l>50) {k=l-50;    } l--;    while (l>=0&&i<50) {c[i++]=res[l--]+48;    } c[i]= ' + ';    Lb=strlen (b);    int Lc=strlen (c);    for (int j=0; j<lb-k; J + +) {A[j]=b[j];    } a[lb-k]= ' + ';    for (int j=0; j<lc; J + +) {B[j]=c[j]; } b[lc]= ' + ';}    void Init () {p=new Trie;    a[0]= ' 1 ', a[1]= ';    Trie_inser (p,a,0);    b[0]= ' 1 ', b[1]= ';        for (int i=2; i<100000; i++) {Add ();    Trie_inser (P,c,i);    }}int Trie_serch (Trie *p,char s[]) {int i=0;    Trie *q=p;        while (S[i]) {int nx=s[i]-48;        if (q->next[nx]==null) return-1;        q=q->next[nx];    i++; } return q->id;}    int main () {//freopen ("test.in", "R", stdin);    Init ();    int t;    cin>>t;    int ca=1; while (t--) {       scanf ("%s", s);    printf ("Case #%d:%d\n", Ca++,trie_serch (P,s)); } return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Hdu 4099 Revenge of Fibonacci (dictionary tree)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.