程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> HDU—Intelligent IME(字典樹)

HDU—Intelligent IME(字典樹)

編輯:C++入門知識

HDU—Intelligent IME(字典樹)


Intelligent IME

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2681 Accepted Submission(s): 1322



Problem Description   We all use cell phone today. And we must be familiar with the intelligent English input method on the cell phone. To be specific, the number buttons may correspond to some English letters respectively, as shown below:
  2 : a, b, c 3 : d, e, f 4 : g, h, i 5 : j, k, l 6 : m, n, o
  7 : p, q, r, s 8 : t, u, v 9 : w, x, y, z
  When we want to input the word “wing”, we press the button 9, 4, 6, 4, then the input method will choose from an embedded dictionary, all words matching the input number sequence, such as “wing”, “whoi”, “zhog”. Here comes our question, given a dictionary, how many words in it match some input number sequences?

Input   First is an integer T, indicating the number of test cases. Then T block follows, each of which is formatted like this:
  Two integer N (1 <= N <= 5000), M (1 <= M <= 5000), indicating the number of input number sequences and the number of words in the dictionary, respectively. Then comes N lines, each line contains a number sequence, consisting of no more than 6 digits. Then comes M lines, each line contains a letter string, consisting of no more than 6 lower letters. It is guaranteed that there are neither duplicated number sequences nor duplicated words.

Output   For each input block, output N integers, indicating how many words in the dictionary match the corresponding number sequence, each integer per line.

Sample Input
1
3 5
46
64448
74
go
in
night
might
gn

Sample Output
3
2
0

Source 2012 ACM/ICPC Asia Regional Tianjin Online
題意:手機英文輸入法九宮格鍵盤中,2~9數字各自代表了自己的字母,求給出的幾個數字串分別能夠打出下列幾個英文單詞。
分析:一開始我是建立給出的單詞的字典樹,然後dfs數字串,枚舉數字串能夠組成的單詞數,這樣毫無疑問地TLE了。後來換了種方法,建立給出數字串的字典樹,然後把單
詞串hash為對應的的數字串,再進行查詢。
題目鏈接:http://acm.hdu.edu.cn/showproblem.php?pid=4287
AC代碼:
#include
#include
#include
#include
using namespace std;
const int MAX = 10 ;
const int maxn = 5005 ;
struct trie{
    int first;
    trie *next[MAX];
};

trie *root;

int T,N,M;
char str[7],ss;
int ans[maxn],slen;
void createTrie(char *s,int n){
    trie *p=root,*q;
    int len=strlen(s),pos;
    for(int i=0;inext[pos]==NULL){
            q=new trie;
            q->first=0;
            for(int j=0;jnext[j]=NULL;
            p->next[pos]=q;
            p=p->next[pos];
        }
        else{
            p=p->next[pos];
        }
    }
    p->first=n;
}

int findTrie(char *s){
    trie *p=root;
    int len=strlen(s),pos;
    for(int i=0;inext[pos]==NULL)
            return 0;
        p=p->next[pos];
    }
    return p->first;
}

void delTrie(trie *Root){
    for(int i=0;inext[i]!=NULL)
            delTrie(Root->next[i]);
    }
    free(Root);
}

char getLetter(char s){
    if(s>='a'&&s<='c') return '2';
    else if(s>='d'&&s<='f') return '3';
    else if(s>='g'&&s<='i') return '4';
    else if(s>='j'&&s<='l') return '5';
    else if(s>='m'&&s<='o') return '6';
    else if(s>='p'&&s<='s') return '7';
    else if(s>='t'&&s<='v') return '8';
    else return '9';
}

int main(){
    scanf("%d",&T);
    while(T--){
        root=new trie;
        for(int i=0;inext[i]=NULL;
        memset(ans,0,sizeof(ans));
        memset(str,'\0',sizeof(str));
        scanf("%d%d",&N,&M);
        for(int i=1;i<=N;i++){
            scanf("%s",str);
            createTrie(str,i);
        }
        for(int i=0;i

TLE代碼:(trie+dfs)
#include
#include
#include
#include
using namespace std;
const int MAX = 28;
const int maxn = 5005;
char str[10][4]={{'a','b','c'},{'d','e','f'},{'g','h','i'},{'j','k','l'},
                {'m','n','o'},{'p','q','r','s'},{'t','u','v'},{'w','x','y','z'}};
int ank[8]={3,3,3,3,3,4,3,4};

struct trie{
    bool point;
    trie *next[MAX];
};

struct edge{
    int n;
    int x[6];
}num[maxn];

trie *root;
char s[7];
int T,N,M,slen,nn,sum;
int vis[10][7];
void createTrie(char *s){
    trie *p=root,*q;
    int len=strlen(s),pos;
    for(int i=0;inext[pos]==NULL){
            q=new trie;
            q->point=false;
            for(int j=0;jnext[j]=NULL;
            p->next[pos]=q;
            p=p->next[pos];
        }
        else{
            p=p->next[pos];
        }
    }
    p->point=true;
}

bool findTrie(char *s){
    trie *p=root;
    int len=strlen(s),pos;
    for(int i=0;inext[pos]==NULL)
            return false;
        p=p->next[pos];
    }
    return p->point;
}

void delTrie(trie *Root){
    for(int i=0;inext[i]!=NULL)
            delTrie(Root->next[i]);
    }
    free(Root);
}

void findAns(int a[],int k,int n,char *s){
    if(k==n){
        //printf("%s  ",s);
        if(findTrie(s)){
            sum++;
        }return ;
    }
    int kk=a[k]-2;
    for(int i=0;inext[i]=NULL;
        scanf("%d%d",&N,&M);
        for(int i=0;i




  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved