POJ 1451 T9 字典树+优先队列

题目来源:POJ 1451 T9

题意:给你一些单词 和优先值 然后当你按下数字的时候首先会出现哪个单词 就是我们平时手机的按键

思路:建一颗字典树 因为按一个数字对应多个字母 那么就有多种情况 我们要输出权值最大的一个 我用了优先队列 这里每个前缀的优先值是所有单词优先值的和

例如abc 5 abd 6 acd 7 那么a这个前缀的优先值是18 ab的优先值是11

#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
const int maxnode = 100010;
const int sigma_size = 26;
const int maxn = 1010;
int ch[maxnode][sigma_size];
int val[maxnode];
int sz;

char s[maxn][110];
char id[12][6] = {"0", "1", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
void init()
{
	sz = 1;
	memset(ch[0], 0, sizeof(ch[0]));
}
struct node
{
	int u, v, x;
	char str[110];
	node(){}
	node(int _u, int _v, int _x, char* s)
	{
		u = _u;
		v = _v;
		x = _x;
		strcpy(str, s);
	}
	bool operator < (const node& rhs) const
	{
		if(v != rhs.v)
			return v > rhs.v;
		return x < rhs.x;
	}
};
int idx(char c)
{
	if(c >= 'a' && c <= 'c')
		return 2;
	if(c >= 'd' && c <= 'f')
		return 3;
	if(c >= 'g' && c <= 'i')
		return 4;
	if(c >= 'j' && c <= 'l')
		return 5;
	if(c >= 'm' && c <= 'o')
		return 6;
	if(c >= 'p' && c >= 's')
		return 7;
	if(c >= 't' && c <= 'v')
		return 8;
	if(c >= 'w' && c <= 'z')
		return 9;
}

void insert(char *s, int v)
{
	int u = 0, n = strlen(s);
	for(int i = 0; i < n; i++)
	{
		int c = s[i] - 'a';
		if(!ch[u][c])
		{
			memset(ch[sz], 0, sizeof(ch[sz]));
			val[sz] = 0;
			ch[u][c] = sz++;
		}
		u = ch[u][c];
		val[u] += v;
	}
	//val[u] += v;
}
void find(char *s)
{
	int u = 0, n = strlen(s);
	priority_queue <node> Q;
	Q.push(node(0, 0, 0, ""));
	for(int i = 0; i < n-1; i++)
	{
		int c = s[i]-'0';
		while(!Q.empty())
		{
			node x = Q.top();
			if(x.v != i)
				break;
			Q.pop();
			u = x.u;

			for(int j = 0; id[c][j]; j++)
			{
				int v = id[c][j] - 'a';
				//printf("%d\n", v);
				if(!ch[u][v])
					continue;
				v = ch[u][v];
				x.str[i] = id[c][j];
				x.str[i+1] = 0;
				//printf("%c\n", id[c][j]);
				Q.push(node(v, i+1, val[v], x.str));
			}
		}
		if(Q.empty())
		{
			puts("MANUALLY");
		}
		else
		{
			node x = Q.top();
			printf("%s\n", x.str);
		}
	}
}

int main()
{
	int cas = 1;
	int T;
	scanf("%d", &T);
	while(T--)
	{
		int n, m;
		scanf("%d", &n);
		init();
		for(int i = 0; i < n; i++)
		{
			//char s[maxn];
			int x;
			scanf("%s %d", s[i], &x);
			insert(s[i], x);
		}
		printf("Scenario #%d:\n", cas++);
		scanf("%d", &m);
		while(m--)
		{
			char str[210];
			scanf("%s", str);
			find(str);
			puts("");
		}
		puts("");
	}
	return 0;
}

POJ 1451 T9 字典树+优先队列

时间: 2024-08-03 17:19:07

POJ 1451 T9 字典树+优先队列的相关文章

POJ 1451 T9 字典树

题意和手机的九键输入法一样.输入数据第一行给出有多少组测试数据,每组数据第一行给出w(0<=w<=1000),接下来w行给出一个单词以及该单词的出现频率p(1<=p<=100),每个单词的最大长度不超过100个字母:然后,给出一个整数m,接下来m行给出一个输入串,代表在手机上按了哪些键,每个输入串最多有100个字符,且以数字1作为结尾.要求根据给出输入串,输出在按这些键的过程中,输入法给出的首选项(即出现频率最高的单词),若没有对应输入的单词,则输出"MANUALLY&q

hdu 1298 T9(字典树+DFS)

题目连接:hdu 1298 T9 题目大意:模拟手机打字的猜想功能,根据概率,每按一个按键,输出可能性最高的串.先给定N个单词,以及频率, 然后是Q次询问,每次询问给定一个按按键的顺序,以1为终止. 解题思路:对单词表建立字典树,每个节点有一个经过的频率,这个频率是根据所有经过该节点的单词频率总和.然后 DFS搜索一遍,将答案保存在ans中. #include <cstdio> #include <cstring> #include <algorithm> using

POJ 1451 -- T9

T9 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 4131   Accepted: 1481 Sample Input 2 5 hell 3 hello 4 idea 8 next 8 super 3 2 435561 43321 7 another 5 contest 6 follow 3 give 13 integer 6 new 14 program 4 5 77647261 6391 4681 26684371

POJ 2503 Babelfish(字典树)

Babelfish Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 35009   Accepted: 14979 Description You have just moved from Waterloo to a big city. The people here speak an incomprehensible dialect of a foreign language. Fortunately, you have

poj 2503 Babelfish(字典树或着STL)

Babelfish Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 35828   Accepted: 15320 Description You have just moved from Waterloo to a big city. The people here speak an incomprehensible dialect of a foreign language. Fortunately, you have

HDU 1298 T9 ( 字典树 )

题意 : 给你 w 个单词以及他们的频率,现在给出模拟 9 键打字的一串数字,要你在其模拟打字的过程中给出不同长度的提示词,出现的提示词应当是之前频率最高的,当然提示词不需要完整的,也可以是 w 个单词出现最多次数的前缀. 分析 : 利用字典树存储这 w 个单词,然后给每一个结点的权值赋上其单词出现频率,接下来的工作就简单多了,只要暴力枚举模拟输入的数字键盘可能产生的单词组合,然后保存频率最高的那个即可! #include<string.h> #include<stdio.h> #

【POJ】1451 T9

DFS+字典树. 1 #include <cstdio> 2 #include <cstring> 3 #include <cstdlib> 4 5 typedef struct Trie { 6 int v; 7 Trie *next[26]; 8 } Trie; 9 10 Trie root; 11 int arr[11] = {0,0,0,3,6,9,12,15,19,22,26}; 12 char buf[105], tmp[105]; 13 char map[

poj 3764 The xor-longest Path(字典树)

题目链接:poj 3764 The xor-longest Path 题目大意:给定一棵树,每条边上有一个权值,找出一条路径,使得路径上权值的亦或和最大. 解题思路:dfs一遍,预处理出每个节点到根节点路径的亦或和rec,那么任意路径均可以表示rec[a] ^ rec[b],所以问题 就转换成在一些数中选出两个数亦或和最大,那么就建立字典树查询即可. #include <cstdio> #include <cstring> #include <algorithm> us

poj 2513 并查集,Trie(字典树), 欧拉路径

- Colored Sticks POJ - 2513 You are given a bunch of wooden sticks. Each endpoint of each stick is colored with some color. Is it possible to align the sticks in a straight line such that the colors of the endpoints that touch are of the same color?