现在的位置: 首页 > 综合 > 正文

HDU4287

2013年02月28日 ⁄ 综合 ⁄ 共 2591字 ⁄ 字号 评论关闭

Intelligent IME

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

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
 
方法大致有3:
1.直接模拟
2.hash表
3.线段树
 
我用的线段树:
题目背景是典型的的线段树,需要运用线段树的相关知识,同时由于一个数字对应多个字母,可以尝试用搜索解决问题!
 
#include<iostream>
#include<cstring>
using namespace std;

const int MAX=5000+10;
struct Trie
{
	Trie* next[26];
	int flag;
};

Trie *root;
//*****************************************************************
//将str插入以root为根节点的字典树中
void insert(char *str)
{
    int len = strlen(str);
    Trie *s = root;
    for (int i = 0; i < len; i++)
	{
		if (s->next[str[i] - 'a'])
            s = s->next[str[i] - 'a'];
        else
		{
            Trie* t = new Trie;
            memset(t, 0, sizeof (Trie));
            s->next[str[i] - 'a'] = t;
            s = t;
        }
	}
	s->flag = 1;
}
//*****************************************************************


int num;

int tran[8][4]=
{
	{0,1,2,-1},
	{3,4,5,-1},
	{6,7,8,-1},
	{9,10,11,-1},
	{12,13,14,-1},
	{15,16,17,18},
	{19,20,21,-1},
	{22,23,24,25}
};

void DFS(Trie *root,char *digit)
{
	int i,len=strlen(digit);
	if(len==0&&root->flag)
	{
		num++;
		return ;
	}
	for(i=0;i<4;i++)
	{
		if(tran[digit[0]-'2'][i]!=-1&&root->next[tran[digit[0]-'2'][i]]!=NULL)
		{
			DFS(root->next[tran[digit[0]-'2'][i]],digit+1);
		}
	}
}

int main()
{
	int t,i,n,m;
	char digit[MAX][10];
	char letter[MAX][10];
	cin>>t;
	while(t--)
	{
		scanf("%d%d",&n,&m);
		root=new Trie;
		memset(root,0,sizeof(Trie));
		for(i=0;i<n;i++)
		{
			scanf("%s",digit[i]);
		}

		for(i=0;i<m;i++)
		{
			scanf("%s",letter[i]);
			insert(letter[i]);
		}

		for(i=0;i<n;i++)
		{
			num=0;
			DFS(root,digit[i]);
			printf("%d\n",num);
		}

	}
	return 0;
}

 

 
 
 
 
 
 
 
 
 

抱歉!评论已关闭.