TeleTable( Data Structure { Trie && SegmentTree} )
来源:互联网 发布:新浪微博怎么修改域名 编辑:程序博客网 时间:2024/05/29 13:40
在幻想乡拨打不同的电话号码收费标准是完全不同的。博丽神社有一个古老的费用表,来确定拨打电话的花费。
每一个幻想乡的电话号码由11 位数字组成。费用表共有N 行,每一行给出一个号码的前缀范围和对应收费标准的名称。对于给出的前缀,如4239-241 ,指所有的前缀为4239,4240,4241 的电话号码。要确定拨打每个号码的收费标准,需要从费用表的第一行开始
依次向下查找,第一个匹配的号码前缀范围所对应的收费标准即为拨打这个号码的收费标准。如果没有找到匹配的前缀,那么认为此号码无效。无效号码的收费标准定义为’invalid’(不带引号)。一种收费标准的名称可以在表上多次出现。
博丽神社的费用表已经很旧了,包含了很多很多的数据,这使得管理变得非常困难。神社的巫女决定将这份费用表修改到更简单易读的格式。她希望表内所有号码前缀都按照字典序排序,并且没有‘-’符号,同时没有一个串是另一个串的前缀,且串的总个数最少。’invalid’
和原先不存在的收费标准不能出现在新费用表中。
Input
每个测试点有多组输入数据。每组数据的第一行有一个整数N,含义如上所述。
接下来N 行,描述了一张费用表,每行元素按顺序分别为前缀A,字母‘-’,前缀B,该收费标准的名字。前缀最多有11 个数字,收费标准由1~20 个小写字母组成。保证B 的长度<=A 的长度,且A 的后|B|个数字的小于等于B。
Output
每组数据第一行输出一个k 表示最少行数。
后面k 行,按字典序顺序输出,每行输出前缀与名字。注意每个前缀长度至少为1。
如果没有合法的电话号码,仅在第一行输出一个0。
做法挺有趣,我们开一个类似trie的线段树,但线段树是10进制的。
对于每个范围,求出其L与R并用0补全L,9补全R,在线段树内区间赋值,
区间赋值完后,要保证输出的串是最短的,我们还得把一些节点合并起来。然后按字典序从小到大输出。
#include<cstdio>#include<cstring>#include<algorithm>#include<iostream>using namespace std ;#define N 110int i , j , k , n , m , T ;string s[N] , st[N] , en[N] , Zab ;struct node { int fl ,son[10] ;}g[N*N*N] ;bool visit[N*N] ;void ASSIGN( string l , string r , int po , int bel ) { if( g[po].fl ) return ; if( l=="" || ( po && l==string( l.size() , 48 ) && r==string( r.size() , 48+9 ) && visit[po]==0 ) ) { visit[po] = 1 ; g[po].fl = bel ; return ; } visit[po] = 1 ; int x = l[0] - 48 , y = r[0] - 48 ; for( int i = 0 ; i<10 ; i++ ) if( g[po].son[i] == 0 ) g[po].son[i] = ++ T ; if( x==y ) ASSIGN( l.substr( 1 , l.size() - 1 ) , r.substr( 1 , r.size() - 1 ) , g[po].son[x] , bel ) ; else { for( int i = x + 1 ; i<y ; i++ ) ASSIGN( string( l.size() - 1 , 48 ) , string( r.size() - 1 , 48+9 ) , g[po].son[i] , bel ) ; ASSIGN( l.substr( 1 , l.size() - 1 ) , string( r.size() - 1 , 48 + 9 ) , g[po].son[x] , bel ) ; ASSIGN( string( l.size() - 1 , 48 ) , r.substr( 1 , r.size() - 1 ) , g[po].son[y] , bel ) ; }}void Shorten( int po ) { if( g[po].fl ) return ; if( visit[po] == 0 ) return ; for( int i=0 ; i<=9 ; i++ ) Shorten( g[po].son[i] ) ; int les = 0 ; if( po ) { for( int i=0 ; i<=9 ; i++ ) { if( visit[ g[po].son[i] ] == 0 || g[ g[po].son[i] ].fl == 0 ) return ; if( les && s[les] != s[ g[ g[po].son[i] ].fl ] ) return ; les = g[ g[po].son[i] ].fl ; } g[po].fl = les ; }}void Count( int po ) { if( visit[po] == 0 ) return ; if( g[po].fl ) { if( s[ g[po].fl ]=="invalid" ) return ; ++T ; return ; } for( int i=0 ; i<10 ; i++ ) Count( g[po].son[i] ) ;}void Output( int po , string pre ) { if( visit[po] == 0 ) return ; if( g[po].fl ) { if( s[ g[po].fl ]=="invalid" ) return ; cout << pre << " " << s[ g[po].fl ] << endl ; return ; } for( int i=0 ; i<10 ; i++ ) Output( g[po].son[i] , pre + char( i + 48 ) ) ;}int main() { ios::sync_with_stdio(0); while( cin >> n ) { memset( g , 0 , sizeof g ) ; memset( visit , 0 , sizeof visit ) ; T = 0 ; for( i=1 ; i<=n ; i++ ) { cin >> st[i] >> Zab >> en[i] >> s[i] ; int ST = st[i].length() , EN = en[i].length() ; while( EN < ST ) { EN ++ ; en[i] = st[i][ST-EN] + en[i] ; } while( EN<11 ) en[i] = en[i] + '9' , EN ++; while( ST<11 ) st[i] = st[i] + '0' , ST ++; ASSIGN( st[i] , en[i] , 0 , i ) ; } Shorten( 0 ) ; T = 0 ; Count( 0 ) ; cout << T << endl ; Output( 0 , "" ) ; }}
- TeleTable( Data Structure { Trie && SegmentTree} )
- Data Structure: Trie(字典树)
- SegmentTree
- LeetCode Add and Search Word - Data structure design Trie
- leetcode:Trie:Add and Search Word - Data structure design(211)
- leetcode---Add and Search Word - Data structure design---Trie树
- Trie (2) -- Add and Search Word - Data structure design
- Data Structure
- Data structure
- Data Structure
- Data structure
- Data structure
- data structure
- Data Structure
- Data structure
- Data Structure
- Data Structure
- Data Structure
- How Many Maos Does the Guanxi Worth 5137 (最短路+删点+最大边)
- OC_Delegate(代理)
- Daemon守护进程
- Java中Iterator(迭代器)的一般用法
- leetcode 134: Gas Station
- TeleTable( Data Structure { Trie && SegmentTree} )
- c++类的初创建(复数类)
- MCV中字典转模型的KVC底层实现
- Spring与Hibernate与Struts整合(一)配置文件
- 今日总结
- 设计模式之Builder
- Shell - 1
- windows远程桌面域的问题
- Java Web笔记:Tomcat数据源