zoukankan      html  css  js  c++  java
  • HDU 4099 Revenge of Fibonacci(字典树)

    Revenge of Fibonacci

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


    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
     
    Source
     
    Recommend
    lcy
     
     
    2011年上海赛区现场赛的题目。
    那时比赛的时候做法想到了,不断加,并进行截断处理。
    但是那个时候不会用字典树,导致查找的时候TLE了。。。那个时候太弱了,和银牌失之交臂了~~~~
    今年再努力吧!
    其实这题就是求以某串数字开头的斐波那契数列。
    因为只要前40位数字。所以在加的时候长度大于50左右就截掉个位上的,保留高位。
    然后要建立一颗字典树来查找。
    /*
    HDU 4099
    字典树
    */
    #include<stdio.h>
    #include<string.h>
    #include<algorithm>
    #include<iostream>
    using namespace std;
    
    char c[100];
    void add(char a[],char b[],char back[])//计算 a+b,结果存入 c,
    {
    
        int i,j,k;
        int x,y,z;
        int up;
        i=strlen(a)-1;
        j=strlen(b)-1;
        k=0;
        up=0;
        while(i>=0||j>=0)
        {
            if(i<0)x=0;
            else x=a[i]-'0';
            if(j<0)y=0;
            else y=b[j]-'0';
            z=x+y+up;
            c[k++]=z%10+'0';
            up=z/10;
            i--;
            j--;
        }
        if(up>0)c[k++]=up+'0';
        for(i=0;i<k;i++)back[i]=c[k-1-i];
        back[k]='\0';
    }
    
    const int MAX=10;
    typedef struct Node
    {
        int id;
        struct Node *next[MAX];
    }TrieNode;
    
    TrieNode *head;
    
    void Tree_insert(char str[],int index)//插入单词
    {
        Node *t,*s=head;
        int i,j;
        int len=strlen(str);
        for(i=0;i<len&&i<41;i++)
        {
            int id=str[i]-'0';
            if(s->next[id]==NULL)
            {
                t=new Node;
                for(j=0;j<10;j++)
                {
                    t->next[j]=NULL;
                }
                t->id=-1;
                s->next[id]=t;
            }
            s=s->next[id];
            if(s->id<0)s->id=index;
        }
    }
    
    int Tree_Find(char str[])
    {
        Node *s=head;
        int count,i;
        int len=strlen(str);
        for(i=0;i<len;i++)
        {
            int id=str[i]-'0';
            if(s->next[id]==NULL)
            {
                return -1;
            }
            else
            {
                s=s->next[id];
                count=s->id;
            }
        }
        return count;
    }
    
    
    
    void Tree_Del(Node *p)
    {
        for(int i=0;i<10;i++)
        {
            if(p->next[i]!=NULL)
              Tree_Del(p->next[i]);
        }
        free(p);
    }
    
    
    char str[3][100];
    int main()
    {
     //   freopen("in.txt","r",stdin);
     //   freopen("out.txt","w",stdout);
        head=new Node;
        for(int i=0;i<10;i++)head->next[i]=NULL;
        head->id=-1;
        str[0][0]='1';
        str[0][1]=0;
        Tree_insert(str[0],0);
        str[1][0]='1';
        str[1][1]=0;
        Tree_insert(str[1],1);
        for(int i=2;i<100000;i++)//注意题目是小于,不能取等号。。WA了很多次啊
        {
            int len1=strlen(str[0]);
            int len2=strlen(str[1]);
            if(len2>60)
            {
                str[1][len2-1]=0;
                str[0][len1-1]=0;
            }
            add(str[0],str[1],str[2]);
          //  printf("%s\n",str[2]);
            Tree_insert(str[2],i);
            strcpy(str[0],str[1]);
            strcpy(str[1],str[2]);
         //   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;
        char str1[60];
        scanf("%d",&T);
        int iCase=0;
        while(T--)
        {
            iCase++;
            scanf("%s",&str1);
            printf("Case #%d: %d\n",iCase,Tree_Find(str1));
        }
        Tree_Del(head);
        return 0;
    }

    还有一个地方就是建树的时候只要建到长度为40就可以了。否则长点就MLE了

  • 相关阅读:
    想要快速上手 Spring Boot?看这些教程就足够了!| 码云周刊第 81 期
    Eclipse Java注释模板设置详解以及版权声明
    java跨域解决
    微信公众号创建
    Java与JS生成二维码与条形码
    Java基础break、continue语句的用法
    分布式文件系统介绍
    Hadoop的RPC机制及简单实现
    RPC简介与hdfs读过程与写过程简介
    JVM的Client模式与Server模式
  • 原文地址:https://www.cnblogs.com/kuangbin/p/2673897.html
Copyright © 2011-2022 走看看