Skip to content

print

原始文件为 cpp 代码,本文是转换后的 Markdown 文件。

struct Node
{
    Node* child[10];
    bool have_value;
    int value;

    Node() { have_value = false;  for(int i = 0; i < 10; ++i) child[i] = NULL; }
    void setvalue(int index) { value = index; have_value = true; }
};

Node* root;

const int maxn = 100005;
int tree[40][10];

void init();
void build(string str, int index);
int query(string str);

int main()
{
    init();

    Bigint a = 0, b = 1;
    for(int i = 0; i < 100000; ++i)
    {
        build(to_string(b), i);
        (a, b) = (b, a + b);
    }

    int T; cin >> T;
    for(int i = 1; i <= T; ++i)
    {
        string str; cin >> str;
        printf("Case #%d: %d\n", i, query(str));
    }
}

void init()
{
    root = new Node();
}

void build(string str, int index)
{
    Node* cur_node = root;
    for(int i = 0; i < str.length(); ++i)
    {
        int num = str[i] - '0';
        if(!cur_node->child[num])
        {
            cur_node->child[num] = new Node();
            cur_node->child[num]->setvalue(index);
        }
        cur_node = cur_node->child[num];
    }
}

int query(string str)
{
    Node* cur_node = root;
    for(int i = 0; i < str.length(); ++i)
    {
        int num = str[i] - '0';
        if(!cur_node->child[num])
        {
            return -1;
        }
        cur_node = cur_node->child[num];
    }
    return cur_node->value;
}