POJ 1002 487-3279

题目大意:

        为了方便记忆电话号码,可以将号码映射成一些单词或短语,设号码为7位数字(0 ~ 9),规定其正规形式为xxx-xxxx(第三位和第四位之间有一个连字符,x为0 ~ 9),其普通形式可以具有映射关系,由数字、大写字母(不包括Q和Z)以及连字符'-'组成,其中连字符可以出于任意位置,而字母和数字总共有7位,其映射规则为:

        A B C  -> 2

        D E F  -> 3

        G H I   -> 4

        J K L   -> 5

        M N O -> 6

        P R S -> 7

        T U V  -> 8

        W X Y -> 9

        比如TUT-GLOP的规范形式为88-4567,310-GINO的规范形式为310-4466,-3-10-10-10-的规范形式为310-1010等。

        现只有一个测例,测例中先给出电话号码薄中共有n条号码(n ≤ 100,000),接下来每行给出一条号码,都是普通形式的,要求输出重复的号码,重复是指两个号码的规范形式相同,输出格式为"规范形式 重复次数",每条占一行,并且要求按照字典序升序输出,如果没有重复号码则输出一行"No duplicates.”。

题目链接

BST:

注释代码:

/*                                                   
 * Problem ID : POJ 1002 487-3279
 * Author     : Lirx.t.Una                                                   
 * Language   : C                                  
 * Run Time   : 2876 ms                                                   
 * Run Memory : 516 KB                                                   
*/  

#include <stdlib.h>
#include <stdio.h>

//用int保存7位号码!!!
//输出的规范形式为前三位
//所以/_LOC得前三位,%_LOC得后四位
#define	_LOC		10000
//格式字符串长度
#define	FMTLEN		20

char	aton['Y' + 1];//alphabet to number,字符映射
char	fmt[FMTLEN];

int		dup;//表示是否有重复号码,1表示有0表示没有

//二叉树
struct  Node;  
  
typedef struct Node *   PtNode;  
typedef struct Node *   Tree;  
  
struct  Node {  
  
	int		num;//保存号码
  
    Tree    lft;      
    Tree    rht;  

    int     cnt;  
};   
  
Tree  
Insert( Tree tree, int num ) {  
      
    if ( !tree ) {  
          
        PtNode  node;  
          
        node = (PtNode)malloc(sizeof(struct Node));  
          
        node->num = num; 

        node->lft = NULL;  
        node->rht = NULL;   
		
        node->cnt = 1;  
          
        return node;  
    }  
      
    if ( num == tree->num ) {  
          
        tree->cnt++;  
        return tree;  
    }  
      
    if ( num < tree->num )
		tree->lft = Insert( tree->lft, num );
	else
		tree->rht = Insert( tree->rht, num );

	return tree;
}  
  
void  
Travel(Tree tree) {  
      
    if (tree) {  
          
        Travel( tree->lft );  

		if ( tree->cnt > 1 ) {//表示有重复

			dup = 1;//置标志为真
			//按照规范形式打印出号码以及重复次数
			printf("%03d-%04d %d\n", tree->num / _LOC, tree->num % _LOC, tree->cnt);
		}

        Travel( tree->rht );  
    }  
}  

void
ini(void) {//定义映射关系

	int		i, j;

	//数字映射关系
	for ( i = '0', j = 0; i <= '9'; i++, j++ )
		aton[i] = j;

	//字母映射关系
	aton['A'] = 2;
	aton['B'] = 2;
	aton['C'] = 2;


	aton['D'] = 3;
	aton['E'] = 3;
	aton['F'] = 3;

	aton['G'] = 4;
	aton['H'] = 4;
	aton['I'] = 4;

	aton['J'] = 5;
	aton['K'] = 5;
	aton['L'] = 5;

	aton['M'] = 6;
	aton['N'] = 6;
	aton['O'] = 6;

	aton['P'] = 7;
	aton['R'] = 7;
	aton['S'] = 7;

	aton['T'] = 8;
	aton['U'] = 8;
	aton['V'] = 8;

	aton['W'] = 9;
	aton['X'] = 9;
	aton['Y'] = 9;
}

int
parseInt(char *fmt) {//将普通好吗解析成规范号码并以int保存

	int		num;

	num = 0;
	while ( *fmt )
		if ( '-' == *fmt ) {

			fmt++;
			continue;
		}
		else
			num = num * 10 + aton[*fmt++];

	return num;
}

int
main() {

	int		n;//号码个数
	Tree	tree;//二叉树


	//初始化
	ini();
	tree = NULL;
	scanf("%d", &n);

	while ( n-- ) {
	
		scanf("%s", fmt);
		tree = Insert( tree, parseInt(fmt) );		
	}

	dup = 0;
	Travel(tree);
	
	if ( !dup ) puts("No duplicates.");

	return 0;
}
无注释代码:

#include <stdlib.h>
#include <stdio.h>

#define	_LOC		10000
#define	FMTLEN		20

char	aton['Y' + 1];
char	fmt[FMTLEN];

int		dup;

struct  Node;  
  
typedef struct Node *   PtNode;  
typedef struct Node *   Tree;  
  
struct  Node {  
  
	int		num;
  
    Tree    lft;      
    Tree    rht;  

    int     cnt;  
};   
  
Tree  
Insert( Tree tree, int num ) {  
      
    if ( !tree ) {  
          
        PtNode  node;  
          
        node = (PtNode)malloc(sizeof(struct Node));  
          
        node->num = num; 

        node->lft = NULL;  
        node->rht = NULL;   
		
        node->cnt = 1;  
          
        return node;  
    }  
      
    if ( num == tree->num ) {  
          
        tree->cnt++;  
        return tree;  
    }  
      
    if ( num < tree->num )
		tree->lft = Insert( tree->lft, num );
	else
		tree->rht = Insert( tree->rht, num );

	return tree;
}  
  
void  
Travel(Tree tree) {  
      
    if (tree) {  
          
        Travel( tree->lft );  

		if ( tree->cnt > 1 ) {

			dup = 1;
			printf("%03d-%04d %d\n", tree->num / _LOC, tree->num % _LOC, tree->cnt);
		}

        Travel( tree->rht );  
    }  
}  

void
ini(void) {

	int		i, j;

	for ( i = '0', j = 0; i <= '9'; i++, j++ )
		aton[i] = j;

	aton['A'] = 2;
	aton['B'] = 2;
	aton['C'] = 2;


	aton['D'] = 3;
	aton['E'] = 3;
	aton['F'] = 3;

	aton['G'] = 4;
	aton['H'] = 4;
	aton['I'] = 4;

	aton['J'] = 5;
	aton['K'] = 5;
	aton['L'] = 5;

	aton['M'] = 6;
	aton['N'] = 6;
	aton['O'] = 6;

	aton['P'] = 7;
	aton['R'] = 7;
	aton['S'] = 7;

	aton['T'] = 8;
	aton['U'] = 8;
	aton['V'] = 8;

	aton['W'] = 9;
	aton['X'] = 9;
	aton['Y'] = 9;
}

int
parseInt(char *fmt) {

	int		num;

	num = 0;
	while ( *fmt )
		if ( '-' == *fmt ) {

			fmt++;
			continue;
		}
		else
			num = num * 10 + aton[*fmt++];

	return num;
}

int
main() {

	int		n;
	Tree	tree;


	ini();
	tree = NULL;
	scanf("%d", &n);

	while ( n-- ) {
	
		scanf("%s", fmt);
		tree = Insert( tree, parseInt(fmt) );		
	}

	dup = 0;
	Travel(tree);
	
	if ( !dup ) puts("No duplicates.");

	return 0;
}
排序单一化:

注释代码:

无注释代码:

#include <algorithm>
#include <iostream>
#include <cstdio>

#define	_LOC		10000
#define	MAXN		100001
#define	FMTLEN		20

using namespace std;

int		num[MAXN];

char    aton['Y' + 1];  
char    fmt[FMTLEN];  
  
int     dup; 

void  
ini(void) {  
  
    int     i, j;  
  
    for ( i = '0', j = 0; i <= '9'; i++, j++ )  
        aton[i] = j;  
  
    aton['A'] = 2;  
    aton['B'] = 2;  
    aton['C'] = 2;  
  
  
    aton['D'] = 3;  
    aton['E'] = 3;  
    aton['F'] = 3;  
  
    aton['G'] = 4;  
    aton['H'] = 4;  
    aton['I'] = 4;  
  
    aton['J'] = 5;  
    aton['K'] = 5;  
    aton['L'] = 5;  
  
    aton['M'] = 6;  
    aton['N'] = 6;  
    aton['O'] = 6;  
  
    aton['P'] = 7;  
    aton['R'] = 7;  
    aton['S'] = 7;  
  
    aton['T'] = 8;  
    aton['U'] = 8;  
    aton['V'] = 8;  
  
    aton['W'] = 9;  
    aton['X'] = 9;  
    aton['Y'] = 9;  
}  
  
int  
parseInt(char *fmt) {  
  
    int     num;  
  
    num = 0;  
    while ( *fmt )  
        if ( '-' == *fmt ) {  
  
            fmt++;  
            continue;  
        }  
        else  
            num = num * 10 + aton[*fmt++];  
  
    return num;  
}  

int
main() {

	int		n;
	int		i;
	int		cnt;

	bool	dup;

	ini();
	scanf("%d", &n);
	for ( i = 0; i < n; i++ ) {
	
		scanf("%s", fmt);
		num[i] = parseInt(fmt);
	}
	sort(num, num + n);

	dup = false;
	for ( i = 0; i < n; i++ ) {
	
		cnt = 1;
		while ( i + 1 < n && num[i] == num[i + 1] ) {
		
			i++;
			cnt++;
		}

		if ( cnt > 1 ) {

			dup = true;
			printf("%03d-%04d %d\n", num[i] / _LOC, num[i] % _LOC, cnt);
		}
	}
	if ( !dup ) puts("No duplicates.");

	return 0;
}
单词解释:

duplicate:vt, 复制; n, 副本

generate:vt, 产生,形成

exclude:vt, 排除,不包括

quality control:n, 质量管理

equivalent:adj, 等价的,相等的

keypad:n, 按键,小键盘

hyphen:n, 连字符

group:vt, 将...分组,聚合

dial:n, 转盘,钟面; vt, 拨号

phrase:n, 短语

spell:vt, 拼写

memorable:adj, 难忘的,值得纪念的

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值