POJ 1002 487-3279

来源:互联网 发布:linux expec参数怎么用 编辑:程序博客网 时间:2024/05/16 09:05

题目大意:

        为了方便记忆电话号码,可以将号码映射成一些单词或短语,设号码为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_LOC10000//格式字符串长度#defineFMTLEN20charaton['Y' + 1];//alphabet to number,字符映射charfmt[FMTLEN];intdup;//表示是否有重复号码,1表示有0表示没有//二叉树struct  Node;    typedef struct Node *   PtNode;  typedef struct Node *   Tree;    struct  Node {    intnum;//保存号码      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 );elsetree->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 );      }  }  voidini(void) {//定义映射关系inti, 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;}intparseInt(char *fmt) {//将普通好吗解析成规范号码并以int保存intnum;num = 0;while ( *fmt )if ( '-' == *fmt ) {fmt++;continue;}elsenum = num * 10 + aton[*fmt++];return num;}intmain() {intn;//号码个数Treetree;//二叉树//初始化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_LOC10000#defineFMTLEN20charaton['Y' + 1];charfmt[FMTLEN];intdup;struct  Node;    typedef struct Node *   PtNode;  typedef struct Node *   Tree;    struct  Node {    intnum;      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 );elsetree->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 );      }  }  voidini(void) {inti, 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;}intparseInt(char *fmt) {intnum;num = 0;while ( *fmt )if ( '-' == *fmt ) {fmt++;continue;}elsenum = num * 10 + aton[*fmt++];return num;}intmain() {intn;Treetree;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_LOC10000#defineMAXN100001#defineFMTLEN20using namespace std;intnum[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;  }  intmain() {intn;inti;intcnt;booldup;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, 难忘的,值得纪念的

0 0
原创粉丝点击