hdu 4099 Revenge of Fibonacci (字典树)

题目大意:

问前缀为给出的 串的斐波那契数列的最小下标,斐波那契最多给出前40个。


思路:
我们保存斐波那契的前50 个。

然后在高精度加的时候损失的精度也不会影响结果。

然后插入的时候只插入前40个  多了就不插



#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>

using namespace std;

struct foo
{
    int save[51];
    void init()
    {
        memset(save,0,sizeof save);
    }
    int get()
    {
        int pos=50;
        while(save[pos]==0)pos--;

        return pos;
    }
}s[3];

foo add(foo a,foo b)
{
    foo res;
    res.init();
    for(int i=0;i<50;i++)
    {
        res.save[i]=a.save[i]+b.save[i]+res.save[i];
        if(res.save[i]>=10)
        {
            res.save[i]%=10;
            res.save[i+1]++;
        }
    }
    return res;
}
struct node
{
    struct node *br[10];
    int num;
};

node *root;

void Trie_init()
{
    root=new node;
    root->num=0;
    for(int i=0;i<10;i++)root->br[i]=NULL;
}
void Trie_insert(foo str,int pos,int mak)
{
    node *s=root;
    int i,ct;
    for(i=pos,ct=0;i>=0 && ct<41;i--,ct++)//ct<=41 MLT 出翔  就卡的这么好  你敢信?
    {
        int id=str.save[i];
        if(s->br[id]==NULL)
        {
            node *t=new node;
            for(int j=0;j<10;j++)t->br[j]=NULL;
            t->num=mak;
            s->br[id]=t;
            s=s->br[id];
        }
        else {
            s=s->br[id];
        }
    }
}

int Trie_find(char str[])
{
    int len=strlen(str);

    node *s;
    s=root;
    int res;

    for(int i=0;i<len;i++)
    {
        int id=str[i]-'0';
        if(s->br[id]==NULL)return -1;
        else
        {
            s=s->br[id];
            res=s->num;
            //printf("%d ",res);
        }
    }
    return res;
}

void Trie_del(node *p)
{
    for(int i=0;i<10;i++)
    {
        if(p->br[i]!=NULL)
          Trie_del(p->br[i]);
    }
    free(p);
}

void move(foo &t)
{
    int pos=t.get();
    for(int i=1;i<=pos;i++)
    {
        t.save[i-1]=t.save[i];
    }
    t.save[pos]=0;
}

int main()
{
    Trie_init();

    s[0].init();
    s[1].init();

    s[0].save[0]=1;
    s[1].save[0]=1;

    Trie_insert(s[0],0,1);
    //cout<<"---"<<endl;
    for(int i=3;i<=100000;i++)
    {
        s[2].init();
        s[2]=add(s[0],s[1]);

        if(s[2].save[50])
        {
            move(s[2]);
            move(s[1]);
        }

        int pos=s[2].get();
       // s[i].print();
        Trie_insert(s[2],pos,i);

        s[0]=s[1];
        s[1]=s[2];
    }

    int n;
    char str[50];
    int CASE=1;

    while(scanf("%d",&n)!=EOF)
    {
        for(int cas=1;cas<=n;cas++)
        {
            scanf("%s",str);
            int ans=Trie_find(str);
            printf("Case #%d: %d\n",cas,ans!=-1?ans-1:ans);
        }
    }
    Trie_del(root);
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值