POJ 1451 T9 Dictionary tree + priority queue

Source: Internet
Author: User
Tags cas strlen

Title Source: POJ 1451 T9

Test instructions: Give you some words and priority values and then when you press the number, the first word is the key of our usual phone.

Idea: Build a dictionary tree because there are multiple letters in a number, so there's a lot of things we want to output the highest value of the one I used priority queue here the precedence value of each prefix is the value of all words and

For example ABC 5 ABD 6 ACD 7 then a the precedence value of this prefix is that the precedence value of AB is 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; }


 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.