http://acm.hdu.edu.cn/showproblem.php?pid=1298

题解:字典树+DFS

/*
*@Author:   STZG
*@Language: C++
*/
#include <bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<string>
#include<vector>
#include<bitset>
#include<queue>
#include<deque>
#include<stack>
#include<cmath>
#include<list>
#include<map>
#include<set>
//#define DEBUG
#define RI register int
using namespace std;
typedef long long ll;
//typedef __int128 lll;
const int N=100000+10;
const int MOD=1e9+7;
const double PI = acos(-1.0);
const double EXP = 1E-8;
const int INF = 0x3f3f3f3f;
int t,n,m,k,q,p;
int ans,cnt,flag,temp;
int a[N][26];
char str[200];
bool en[N];
int sum[N];
char ANS[N];
int ansnum[N];
string tmp[110];
int value[110];
string mp[10]={"","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};
void init(){
    memset(en,0,sizeof(en));
    memset(sum,0,sizeof(sum));
    memset(a,0,sizeof(a));
    memset(value,0,sizeof(value));
    cnt=0;
}
void insert(char *s,int v){
    int len=strlen(s),p=0;
    for(int i=0;i<len;i++){
        int ch=s[i]-'a';
        if(a[p][ch]==0)a[p][ch]=++cnt;
        p=a[p][ch];
        sum[p]+=v;//cout<<p<<sum[p]<<endl;
    }
    en[p]=1;
}
int search(string s){
    int len=s.size(),p=0;//c
    for(int i=0;i<len;i++){
        int ch=s[i]-'a';//cout<<p<<sum[p]<<endl;
        if(a[p][ch]==0)
            return 0;
        p=a[p][ch];
    }

    return sum[p];
}

void dfs(char*s,int len,int now,string as){
    if(len==now){
        return;
    }
    int lenc=mp[s[0]-'0'].size();
    for(int i=0;i<lenc;i++){
        string ch;
        ch=mp[s[0]-'0'][i];//cout<<as+ch<<endl;
        int v=search(as+ch);

        if(v>value[now+1]){
            value[now+1]=v;//cout<<now+1<<" "<<v<<endl;
            tmp[now+1]=as+ch;
        }
        if(v)
            dfs(s+1,len,now+1,as+ch);
    }
}
int main()
{
#ifdef DEBUG
	freopen("input.in", "r", stdin);
	//freopen("output.out", "w", stdout);
#endif
    //
    scanf("%d",&t);
    int T=0;
    while(t--){
        printf("Scenario #%d:\n",++T);
        init();
        scanf("%d",&n);
        for(int i=1;i<=n;i++){
            scanf("%s %d",str,&p);
            insert(str,p);
        }
        scanf("%d",&m);
        for(int i=1;i<=m;i++){
            memset(ansnum,0,sizeof(ansnum));
            memset(value,0,sizeof(value));
            scanf("%s",str);
            int len=strlen(str)-1;
            dfs(str,len,0,"");
            for(int i=1;i<=len;i++){
                if(value[i])
                    cout<<tmp[i]<<endl;
                else
                    cout<<"MANUALLY"<<endl;

            }
            printf("\n");
        }
        printf("\n");
    }


    //cout << "Hello world!" << endl;
    return 0;
}