#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* It is not used.
*
char dict[8][3] = {
{'A', 'B', 'C'},
{'D', 'E', 'F'},
{'G', 'H', 'I'},
{'J', 'K', 'L'},
{'M', 'N', 'O'},
{'P', 'R', 'S'},
{'T', 'U', 'V'},
{'W', 'X', 'Y'}
};
*/
/* Return:
* 0: for str1 is before str2.
* 1: for str2 is before str1.
*/
int compare_sort(char *str1, char *str2)
{
int i;
for (i = 0;i < 9;++i) {
if (str1[i] < str2[i])
return 0;
else if (str2[i] < str1[i])
return 1;
else if (str1[i] == str2[i])
continue;
}
/* should not go into this subroutine. */
return -1;
}
/* Return:
* 0: for str1 is equal to str2.
* non-0: for not equal.
*/
int compare(char *str1, char *str2)
{
int i;
for (i = 0;i < 9;++i) {
if (str1[i] != str2[i])
return 1;
}
return 0;
}
/* It is stupid way. ^_^ */
char myitoa(int i)
{
switch(i) {
case 1:
return '1';
case 2:
return '2';
case 3:
return '3';
case 4:
return '4';
case 5:
return '5';
case 6:
return '6';
case 7:
return '7';
case 8:
return '8';
case 9:
return '9';
case 0:
return '0';
default:
return '0';
}
}
/* A two dimension array for storing standard form phone number,
* Also used for counting duplicates. For instance,
* (Note, the first one is a pointer to phone number, the second
* one is the duplicate times.
*
* {&"333-5555", 3},
* {&"444-6666", 4},
* etc...
*/
char ***phone = NULL;
void format_to_standard(char *phone_number, int count)
{
int len = 0, i;
char *number = NULL;
int cnt_of_digit = 0;
len = strlen(phone_number);
if (len < 7)
return;
number = (char*)malloc(9 * sizeof(char));
if (number == NULL)
exit(-2);
for (i = 0;i < len;++i) {
if (phone_number[i] == '-')
continue;
/* Digit validation check.
* It cannot be Q or Z.
* And it must be 1~9 or A~Y.
*/
if (phone_number[i] < '0' || (phone_number[i] > '9' && phone_number[i] < 'A')
|| phone_number[i] >= 'Z' || phone_number[i] =='Q')
break;
/* If it is a number. */
if (phone_number[i] < 'A') {
number[cnt_of_digit++] = phone_number[i];
} else {
/* It must be a upper character */
if (phone_number[i] < 'Q') {
number[cnt_of_digit++] = myitoa((phone_number[i] - 'A') / 3 + 2);
} else if (phone_number[i] == 'R' || phone_number[i] == 'S') {
number[cnt_of_digit++] = '7';
} else if (phone_number[i] == 'U' || phone_number[i] == 'V' || phone_number[i] == 'T') {
number[cnt_of_digit++] = '8';
} else if (phone_number[i] == 'W' || phone_number[i] == 'X' || phone_number[i] == 'Y') {
number[cnt_of_digit++] = '9';
}
}
if (cnt_of_digit == 3) {
number[cnt_of_digit++] = '-';
}
}
number[cnt_of_digit] = '\0';
/* Assign mem of storing phone number to Array */
phone[count][0] = number;
phone[count][1] = (char*)1;
return;
}
void count_duplicate(int count)
{
int flag_loop = 0; /* This flag is used in i-loop for determining if dup exists. */
int flag_all = 0; /* This flag is used for indicating duplicate, and whether need to print */
int *index_dup = NULL;
int i, j, ret = -1;
int cnt_dup = 0;
int tmp;
index_dup = (int*)malloc(count * sizeof(int));
if (index_dup == NULL)
exit(-2);
for (i = 0;i < count;++i) {
if ((int)phone[i][1] == -1)
continue;
for (j = i + 1;j < count;++j) {
ret = compare(phone[i][0],phone[j][0]);
if (ret == 0) {
phone[i][1]++;
phone[j][1] = (char*)-1;
flag_loop = 1;
}
}
if (flag_loop == 1) {
index_dup[cnt_dup++] = i;
flag_all = 1;
}
flag_loop = 0;
}
if (flag_all == 0) {
printf("No duplicates.\n");
} else {
for (i = 0;i < cnt_dup;++i) {
for (j = i + 1;j < cnt_dup;++j) {
ret = compare_sort(phone[index_dup[i]][0],phone[index_dup[j]][0]);
if (ret == 1) {
tmp = index_dup[i];
index_dup[i] = index_dup[j];
index_dup[j] = tmp;
}
}
}
/* This is used for printing phone number in ascending order. */
for (i = 0;i < cnt_dup;++i) {
printf("%s %d\n",phone[index_dup[i]][0],(int)phone[index_dup[i]][1]);
}
}
}
void free_mem(int len)
{
int i;
for (i = 0;i < len;++i) {
if (phone[i][0] != NULL) {
free(phone[i][0]);
phone[i][0] = NULL;
}
free(phone[i]);
phone[i] = NULL;
}
}
int main(void)
{
char number[10];
char phone_number[16];
int count = 0, i;
scanf("%s", number);
count = atoi(number);
if (count <= 0 || count > 100000)
exit(-1);
phone = (char***)malloc(count * sizeof(char**));
if (phone == NULL)
exit(-2);
/* Malloc the array of char pointer, and initialize */
for (i = 0;i < count;++i) {
phone[i] = (char**)malloc(2 * sizeof(char*));
if (phone[i] == NULL)
exit(-2);
phone[i][0] = NULL;
phone[i][1] = NULL;
}
for (i = 0;i < count;++i) {
scanf("%s", phone_number);
format_to_standard(phone_number,i);
}
count_duplicate(count);
free_mem(count);
return 0;
}
分享到:
相关推荐
hive报hdfs exceeded directory item limit错误,是由于每次任务失败或者异常退出会造成存在HDFS上的临时目录不自动删除,HDFS目录文件数达到上限,可以脚本清理旧的无用目录解决
Kudu :Service unavailable: Soft memory limit exceeded at xxx% of capacity 3.原因 内存限制问题(Memory Limits): Kudu都有一个硬性和软性的内存限制。 硬存储器限制是Kudu进程允许使用的最大数量,由--...
本篇文章是对File size limit exceeded 错误进行了详细的分析介绍,需要的朋友参考下
解决SQLSTATE[HY000]: General error: 1205 Lock wait timeout exceeded
非最大抑制算法:作为FASTER RCNN中的一部分,主要用于去除掉与得分最高的物体框的IOU值大于threadd其他物体框;soft-nms原代码,包括python版、pytorch版,
windows2008默认是禁ping的,需要开通方可被ping
Tiny : 648B (minified, gzipped) 由size-limit监控。 如果您需要更多控制,则公开onResize 回调。 box 选项。 与SSR一起使用。 适用于CSS-in-JS。 支持自定义 refs,以防您已经拥有一个。 默认使用 RefCallback来...
vue-response-components使用ResizeObserver创建响应组件。 想法请查看我在ITNEXT安装上的帖子npm install vue-sensitive-c vue-response-components使用ResizeObserver创建响应性组件。 想法看看我在ITNEXT安装上的...
Time Limit Exceeded 。 经过几次尝试,我用谷歌搜索。 1041. 机器人围成一圈(中) - 观看了如何优化方向/位置问题。 980. 独特的路径 III (难) - 在观看了几种方法/解决方案后解决了。 399. 评估部门(中) - 我...
use RateLimit \ Exception \ LimitExceeded ; use RateLimit \ Rate ; use RateLimit \ RedisRateLimiter ; use Redis ; $ rateLimiter = new RedisRateLimiter ( new Redis ()); $ apiKey = 'abc123' ; try { $ ...
64 bytes from 220.181.38.148 (220.181.38.148): icmp_seq=1 ttl=50 time=27.5 ms 64 bytes from 220.181.38.148 (220.181.38.148): icmp_seq=2 ttl=50 time=26.8 ms 64 bytes from 220.181.38.148
本篇文章是对解决PHP mysql_query执行超时(Fatal error: Maximum execution time of 300 seconds exceeded in…)的问题进行了详细的分析介绍,需要的朋友参考下
React调整观察者挂钩 ResizeObserver + React钩子 演示版 安装 yarn add react-resize-observer-hook 用法 import { useState , useRef } from 'react' ; import useResize from 'react-resize-observer-hook' ;...
报错:Job for sshd.service failed because a timeout was exceeded. See "systemctl status sshd.service" and "journalctl -xe" for details. 问题2:程序sftp登陆不上 低版本的连接工具无法连接,现有程序sftp到...
xmemcached-1.2.6.2
func_timeout Python模块支持以给定的超时时间运行任何现有功能。... Raises any exceptions #func# would raise, returns what #func# would return (unless timeout is exceeded), in which case it raises Functi
poi读取大量数据会造成gc内存溢出的报错,由于垃圾回收机制无法将大量的对象及时的回收,而这些对象又会保存在内存中,会导致内存不够用的情况,这时候我们就需要使用新的方法,读取为cvs即可.此解决方案可支持千万数据的...
Symbol limit exceeded ---------------符号超限 Too few parameters in call -----------------函数调用时的实参少于函数的参数不 Too many default cases ------------------- Default太多(switch语句中一个) ...
Energy Audit for Building