hdu1298 T9

基于字典树的广搜,将所有给定的单词打成一棵字典树,然后从根开始广搜其每一个节点,用优先队列弹出当前长度下最大优先级的字母。

#include <iostream>
#include <queue>
using namespace std;
const int size = 26;
struct Trie
{
       int por;
       int end;
       int len;
       char ch;
       Trie *now;
       Trie *son[size];
       string wo;
       Trie()
       {
             por = end = 0;
             for (int i = 0; i  < size; i ++){
                 son[i] = NULL;   
             }     
       }
       bool operator <(const Trie t)const
       {
            if (len == t.len){
               if (por == t.por){
                  return ch > t.ch;       
               }
               return por < t.por;
            }
            return len > t.len;
       }
      
};
string map1[11] = {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
void insert(char *a, Trie *root, int por)
{
     Trie *temp = root;
     int len = strlen(a);
     for (int i = 0; i < len; i ++){
         int k = a[i] - 'a';
         temp -> por += por;
         if (temp -> son[k] == NULL){
            temp -> son[k] = new Trie;        
         }   
         temp = temp -> son[k];
     }
     temp -> end ++;
}
bool visited[1000];
void find(char *a, Trie *root)
{
     priority_queue  <Trie>Que;
     memset(visited, false, sizeof(visited));
     visited[0] = true;
     Trie pre;
     pre.len = 0;
     pre.wo = "";
     pre.now = root;
     pre.por = 0;
     Que.push(pre);
     while (!Que.empty()){
           pre = Que.top();
           Que.pop();
           if (!visited[pre . len]){
              cout<<pre.wo<<endl;
              visited[pre.len] = true;              
           }
           int x = int(a[pre.len] - '0');
           for (int i = 0; map1[x][i]; i ++){
               if (pre.now -> son[map1[x][i]-'a'] != NULL){
                  Trie next;
                  next.wo = pre.wo +map1[x][i];
                  next.ch = map1[x][i];
                  next.len = pre.len + 1;
                  next.now = pre.now->son[map1[x][i]-'a'];
                  next.por = next.now->por;
                  Que.push(next);       
               }   
           }
     }
          
}
int main()
{
    int t;
    scanf("%d", &t);
    int nc = 0;
    while (t --){
          int n;
          scanf("%d", &n);
          getchar(); 
          char a[110];
          Trie *root;
          root = new Trie;
          while (n --){
                int p;
                scanf("%s %d", a, &p);
                insert(a, root, p);
          }    
          scanf("%d", &n);
          getchar(); 
          char word[110];
          printf("Scenario #%d:\n", ++nc);
          while (n --){
                scanf("%s", word);
                find(word, root);
                for (int i = 0; word[i] != '1' && word[i]; i ++){
                    if (visited[i+1])continue;
                    else {
                         printf("MANUALLY\n");    
                    }
                }
                printf("\n");
          }
          printf("\n");
    }   
    return 0;   
}

你可能感兴趣的:(hdu1298 T9)