码迷,mamicode.com
首页 > 其他好文 > 详细

T9 HDU1298

时间:2019-02-05 11:48:36      阅读:130      评论:0      收藏:0      [点我收藏+]

标签:mes   clu   clear   while   void   dfs   eof   put   就是   

就是字典树加dfs

把所有操作封在结构体里面

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

using namespace std;

const int maxn = 1e5 + 10;

char dic[10][8] = {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
int n, w, p, m, P;
char s[105], num[105], Find[105], ans[105];

struct Trie
{
    int pos;
    int tree[maxn][26];
    int val[maxn];

    void clear()
    {
        pos = 1;
        memset(tree[0], 0, sizeof(tree[0]));
        memset(val, 0, sizeof(val));
    }

    int idx(char c)
    {
        return c - a;
    }

    void insert(char *s, int v)
    {
        int root = 0, n = strlen(s);
        for(int i = 0; i < n; i++)
        {
            int ch = idx(s[i]);
            if(!tree[root][ch])
            {
                memset(tree[pos], 0, sizeof(tree[pos]));
                tree[root][ch] = pos++;
            }

            root = tree[root][ch];
            val[root]+=v;
        }
    }
                
    void query(int cur, int len, int u)
    {
        if(cur == len && val[u] > P)
        {
            P = val[u];
            for(int i = 0; i < len; i++)
            {
                Find[i] = ans[i];
                Find[len] = \0;
            }
            return ;
        }
        int id = num[cur] - 0;
        for(int i = 0; dic[id][i]; i++)
        {
            int c = idx(dic[id][i]);
            if(!tree[u][c])
                continue;
            ans[cur] = dic[id][i];
            query(cur+1, len, tree[u][c]);
        }
        return ;
    }
}trie;

int main()
{
    int flag = 1;
    int T;
    scanf("%d", &T);
    while(T--)
    {
        trie.clear();
        printf("Scenario #%d:\n", flag++);
        scanf("%d", &w);
        for(int i = 0; i < w; i++)
        {
            scanf("%s%d", s, &p);
            trie.insert(s, p);
        }
        scanf("%d", &m);
        for(int i = 0; i < m; i++)
        {
            scanf("%s", num);
            int len = strlen(num);
            for(int j = 1; j < len; j++)
            {
                P = 0;
                trie.query(0, j, 0);
                if(P>0)
                    puts(Find);
                else
                    puts("MANUALLY");
            }
            puts("");
        }
        puts("");
    }
}

 

T9 HDU1298

标签:mes   clu   clear   while   void   dfs   eof   put   就是   

原文地址:https://www.cnblogs.com/bxd123/p/10352564.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!