POJ 3149 Billing Tables__poj

Source: Internet
Author: User

Description

In the world's telecommunications phone calls to different phone numbers have to is charged using different rate or Diffe Rent billing plan. International Carrier of Phone Communications (ICPC) has a antique billing table that determines which Phone call has to Be charged using which billing.

Each international phone number has one digits. The billing table Has n lines. Each line specifies a range of prefixes of the phone numbers like "7919-921". This specification means so all phone numbers starting from 7919, 7920, and 7921 match this line. A Billing plan name was specified for each prefix. To determine a billing the "a" call, the "table is scanned" to bottom and the "the", "the" Billing plan. If no match is found, the phone number is invalid and no billing the plan is needed. A Special Billing plan named "invalid" (without quotes) are used as an alternative way to define invalid phone numbers. Some billing plans are used for quite differently looking phone numbers and their names the May is specified on different line s in different places of the table.

ICPC ' s billing table is old and contains many entries. Some of those entries may is even used anymore. It is very hard to figure out which the phone numbers each billing the "is" actually for. The ICPC ' s management has reached a decision to transform this billing table into a more legible format. In the new format table consists of the lexicographically ordered list of simple prefixes (without the "-" range feature The ' old format ' with a billing ' plan ' name for each prefix. No prefix of this new billing table should is a prefix of any other prefix from the table. Thus, a simple dictionary lookup (binary search, for example) 'll be sufficient to figure out a billing plan for a given Phone number. Finding all phone numbers to a given billing plan would also become a simple task. The number of lines in the new billing table should is minimized. Billing plan named "Invalid" should is present in the new billing table at all, since invalid phone numbers'll be de Noted by ABSence of the corresponding prefix in the new billing table.

Input

The ' I ' input file contains a single integer number n (1≤n≤100)-the number of lines in the old billing Table. The following n lines describe the old billing table with one rule on a line. Each rule contains four tokens separated by Spaces-prefix A, minus sign ("-"), PREFIXB, and billing plan name. Prefixes contain from 1 to one digits each, and the billing plan name is contains from 1 to lower case letters.

Further, let-us denote with | a| The number of digits in the prefix A. It is true that 1≤| b| ≤| a| ≤11. Moreover, last | b| Digits of prefix a form a string this is lexicographically equal or precedes B. Such pair of prefixes A and B matches all phone numbers with the | a| −| b| Digits matching the the digits of A and with the following | b| Digits being lexicographically between | b| Digits of A and B (inclusive).

Output

Write to, output file a single integer number k-the minimal number of lines, the new table should contain to DESC Ribe the given old billing table. Then write k lines with the lexicographically ordered new billing table. Write two tokens separated by a spaces on each line-the prefix and the billing plan name. Note, which is the prefix in the new billing table shall contain at least one one digit.

If All phone numbers are invalid (every phone number has no matching line or matches line with billing plan "invalid") the n the output file should contain just number zero.

Sample Input

8
7919-921 cell
7921800-999 priv
1-1 USA 760-9 RSV 7928-29 RSV
spec
73- Invalid
7-7 CIS

Sample Output

1 USA CIS RSV CIS------spec. Spec.
790 CIS
7910 CIS
7911 CIS 7912 CIS 7913 CIS 7914 CIS 7915 CIS 7916 CIS 7917 CIS 7918 CIS
7919 Cell
7920 cell
7921 cell
7922 CIS
7923 CIS
7924 CIS 7925 CIS 7926 CIS 7927 CIS
7928 RSV
7929 RSV
793 cis
794 CIS 795 CIS
796 CIS 797 CIS 798 CIS

799 CIS

Convert the original telephone number into a dictionary, build a dictionary tree, and insert the range from the back.

#include <set> #include <map> #include <ctime> #include <cmath> #include <stack> #include <queue> #include <bitset> #include <cstdio> #include <string> #include <cstring> #include <iostream> #include <algorithm> #include <functional> #define REP (i,j,k) for (int i = j; I <= K; i++) # Define per (int i = j; I >= K; i--) #define Loop (I,J,K) for (int i = J;i!=-1; i = k[i]) #define Lson x ;< 1, L, mid #define Rson x << 1 | 1, Mid + 1, r #define FI #define SE second #define MP (I,J) Make_pair (i,j) #define PII pair<string,string>
G namespace Std;
typedef long Long LL;
const int low (int x) {return x&-x;} const double EPS = 1e-8;
const int INF = 0x7fffffff;
const int mod = 1E8;
const int N = 5e5 + 10;
const int M = 1E3;
	const int Read () {char ch = getchar ();
	while (ch< ' 0 ' | | | ch> ' 9 ') ch = getchar ();
	int x = ch-' 0 '; while (ch = getchar ()) >= ' 0 ' &&ch <= ' 9x = x + ch-' 0 ';
return x;
int n, root, tot;
int nt[n][10];
String l[m], r[m], s[m], na[n];

Vector<pii> p;
	int node () {Rep (i, 0, 9) nt[tot][i] = 0;	Na[tot] = "";
return tot++;	
	void Insert (int &x, int pos, string l, string R, int L, int R, string g) {if (!x) x = node ();
	if (POS >= l.size ()) L = R = 0;
	if (!l &&!r) {na[x] = g; return;}
	if (Na[x]!= "") Rep (i, 0, 9) Insert (nt[x][i), POS + 1, L, R, 0, 0, na[x]);
	NA[X] = ""; int ll = l? L[pos]-' 0 ': 0, rr = r?
	R[pos]-' 0 ': 9;
	if (LL = RR) {insert (Nt[x][ll], pos + 1, L, R, L, R, G);
	Insert (Nt[x][ll], pos + 1, L, R, L, 0, G);
	Insert (NT[X][RR], pos + 1, L, R, 0, R, G);
Rep (I, ll + 1, rr-1) Insert (Nt[x][i], pos + 1, L, R, 0, 0, G);
	} void Dfs (int x) {if (!x | | na[x]!= "") return;
	int flag = 0;
	String now = "";
		Rep (i, 0, 9) {DFS (nt[x][i]);
		if (Nt[x][i] && Na[nt[x][i]]!= "");
			{if (!flag) {now = na[nt[x][i]]; flag = 1;} else flag + = Now = NA[nt[x][i]];
} if (flag = n) na[x] = now;
	} void Dfs (int x,string id) {if (!x) return;
	if (Na[x]!= "" &&x!= root) {if (na[x]!= "Invalid") P.push_back (MP (ID, na[x));
Else Rep (i, 0, 9) Dfs (Nt[x][i], id + (char) (' 0 ' + i));  int main () {while (scanf ("%d", &n)!= EOF) {Rep (i, 1, n) {cin >> l[i] >> s[i] >> R[i]
			>> S[i];
			int k = L[i].size ()-r[i].size ();
		Per (j, k-1, 0) r[i] = L[i][j] + r[i]; root = 0;
		tot = 1;
		Per (i, n, 1) insert (root, 0, l[i], r[i], 1, 1, s[i]);	
		DFS (root); P.clear ();
		Dfs (Root, "");
		printf ("%d\n", P.size ());
	Rep (i, 0, (int) (P.size ()-1)) cout << p[i].fi << "" << p[i].se << Endl;
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.