HDU 4287 Intelligent IME (字典树 && map)

分析:此题题意很简单,我就不说了,第一想到的就是用字典树做,首先你得考虑用哪个作为字典树,显然,这里用后面的字符串作为树更好。

接着就是套模板了。此题WA了无数次,找bug找了一天,也没找到头绪,后来看别人的结题报告,才明白,原来坑爹的把memset(mark)放在了循环外面,一直以为只循环一次就够了,,真是坑爹啊,,,

做到现在了,感觉字典树的问题基本上都能用map来解决。

代码一:字典树

学会了用内联,确实会快一些

#include <iostream>
#include <cstring>
#include <cstdlib>
using namespace std;

int cnt;
int haash[151];
int res[5005];
const int MAX = 500000;
int a[]={2,2,2,3,3,3,4,4,4,5,5,5,6,6,6,7,7,7,8,8,8,9,9,9,9};

struct trie
{
	trie *next[26];
	int v;   // 数组编号
	int vv;  // 末尾的标志
	void init()
	{
		vv = 1;
		v=-1;
		memset(next,NULL,sizeof(next));
	}
}heap[MAX];

inline trie *new_trie()
{
	heap[cnt].init();
	return &heap[cnt++];
}

inline void creat(char *s,trie *p,int num)
{

	for(;*s;s++)
	{
		int id = *s - '0';
		if(p->next[id] == NULL)
		{
			p->next[id] = new_trie();
		}
		p = p->next[id];
	}
	p->v = num;
	p->vv = -1;
}

inline void  find(char *s,trie *p)
{
	for(;*s;s++)
	{
		p = p->next[a[*s-'a']];
		if(p == NULL)
			return ;
	}
	if(p->vv == -1)
	{
		res[p->v]++;
	}
	else
		 return ;

}

int main()
{
	int k,i,m,n,j;
	char s[10];

    cnt = 0;
    trie *root = new_trie();

    cin>>m;
    while(m--)
    {
    	cin>>k>>n;
    	getchar();
        for(i=0;i<k;i++)
        {
    	  gets(s);
    	  creat(s,root,i);
	   }
	   memset(res,0,sizeof(res));
	   while(n--)
       {
          gets(s);
    	  find(s,root);
	   }
	   for(i=0;i<k;i++)
	   {
	   	  cout<<res[i]<<endl;
	   }
	}
	return 0;
}

代码二:map

#include <iostream>
#include <cstring>
#include <cstdlib>
#include <map>
using namespace std;

int haash[151];
int mark[5001];
int a[]={2,2,2,3,3,3,4,4,4,5,5,5,6,6,6,7,7,7,7,8,8,8,9,9,9,9};

int main()
{
	int k,i,m,n,j,num,sum;
	char s[10];
	map<int,int> mp;

    scanf("%d",&m);
    while(m--)
    {
    	scanf("%d%d",&k,&n);
        for(i=1;i<=k;i++)
        {
           scanf("%d",&num);
    	   mp[num] = i;
	   }
	   memset(mark,0,sizeof(mark));
	   while(n--)
       {
          scanf("%s",s);
    	  for(i=0,sum=0;s[i];i++)
    	  {
    	  	  sum = sum*10 + a[s[i]-'a'];
		  }
		  mark[mp[sum]]++;
	   }
	   for(i=1;i<=k;i++)
	   {
	   	  printf("%d\n",mark[i]);
	   }
	}
	return 0;
}

代码三:

#include <iostream>
#include <cstring>
#include <cstdlib>
#include <map>
using namespace std;

int haash[151];
int mark[5001];
int a[]={2,2,2,3,3,3,4,4,4,5,5,5,6,6,6,7,7,7,7,8,8,8,9,9,9,9};

int main()
{
	int k,i,m,n,j,num[5001],sum;
	char s[10];
	map<int,int> mp;

    scanf("%d",&m);
    while(m--)
    {
    	scanf("%d%d",&k,&n);
    	memset(num,0,sizeof(num));
        for(i=1;i<=k;i++)
        {
           scanf("%d",&num[i]);
	   }
	   memset(mark,0,sizeof(mark));
	   while(n--)
       {
          scanf("%s",s);
    	  for(i=0,sum=0;s[i];i++)
    	  {
    	  	  sum = sum*10 + a[s[i]-'a'];
		  }
		  for(i=1;i<=k;i++)
		  {
		  	  if(sum == num[i])
		  	     mark[i]++;
		  }
	   }
	   for(i=1;i<=k;i++)
	   {
	   	  printf("%d\n",mark[i]);
	   }
	}
	return 0;
}

Intelligent IME

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 2721    Accepted Submission(s): 1340

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

时间: 2024-07-28 13:11:11

HDU 4287 Intelligent IME (字典树 && map)的相关文章

ACM学习历程—HDU 4287 Intelligent IME(字典树 || map)

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

HDU 4287 Intelligent IME(map运用)

转载请注明出处:http://blog.csdn.net/u012860063 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4287 Intelligent IME Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2091    Accepted Submission(s): 1031

HDU 4287 Intelligent IME(字典树数组版)

Intelligent IME Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 4776    Accepted Submission(s): 2227 Problem Description We all use cell phone today. And we must be familiar with the intelligen

HDU 4287 Intelligent IME hash

Intelligent IME Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4287 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,

hdu 4287 Intelligent IME

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 

HDU 4825 Xor Sum 字典树+位运算

点击打开链接 Xor Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Others) Total Submission(s): 291    Accepted Submission(s): 151 Problem Description Zeus 和 Prometheus 做了一个游戏,Prometheus 给 Zeus 一个集合,集合中包含了N个正整数,随后 Prometheus

hdu 1251 统计难题 字典树

// hdu 1251 统计难题 字典树 // // 题目大意: // // 有一系列的单词表,以空行结尾,之后会有一些字母串,找出以这些字符串 // 作为前缀的单词的个数 // // // 解题思路: // // 字典树 Trie,在插入字符串的时候每遇到一个节点,该节点的值++.查找的时候 // 字符串时,如果找到了,那么返回当前的val,否则返回0,因为没有以这个字符串 // 为前缀的单词. // // // 感悟: // // 这段时间想学学数据结构,就看了看刘老的大白书,感觉用数组挺巧

HDOJ/HDU 1251 统计难题(字典树啥的~Map水过)

Problem Description Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本身也是自己的前缀). Input 输入数据的第一部分是一张单词表,每行一个单词,单词的长度不超过10,它们代表的是老师交给Ignatius统计的单词,一个空行代表单词表的结束.第二部分是一连串的提问,每行一个提问,每个提问都是一个字符串. 注意:本题只有一组测试数据,处理到文件结束. Output 对于每个提

杭电(hdu)ACM 4287 Intelligent IME

Intelligent IME Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3453    Accepted Submission(s): 1647 Problem Description We all use cell phone today. And we must be familiar with the intelligen