#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;
}
分享到:
相关推荐
在使用Apache Hive进行大数据处理时,可能会遇到一种错误提示:“hdfs exceeded directory item limit”。这个错误是因为HDFS(Hadoop Distributed File System)的某个目录下的子目录数量超过了默认的最大限制,即...
Rate Limit Exceeded(解决方案).md
Kudu :Service unavailable: Soft memory limit exceeded at xxx% of capacity 3.原因 内存限制问题(Memory Limits): Kudu都有一个硬性和软性的内存限制。 硬存储器限制是Kudu进程允许使用的最大数量,由--...
在编程过程中,我们有时会遇到“File size limit exceeded”这样的错误,这通常意味着我们的程序尝试创建或操作的文件大小超过了操作系统或特定软件允许的最大限制。在本文中,我们将深入探讨这个问题,以及如何解决...
Resource Limit Exceeded(处理方案).md
Resource Limit Exceeded(解决方案).md
Memory Limit Exceeded(解决方案).md
Cache Size Limit Exceeded(解决方案).md
Request Rate Limit Exceeded(解决方案).md
在MySQL数据库操作中,"SQLSTATE[HY000]: General error: 1205 Lock wait timeout exceeded" 是一个常见的错误,它意味着在执行事务时,系统等待锁定资源的时间超过了预设的限制。这个错误通常发生在并发环境中,当...
Flow Rule Limit Exceeded(亲测可用).md
Upload failed. File size limit exceeded(解决方案).md
非最大抑制(Non-Maximum Suppression,NMS)是一种在目标检测算法中常见的后处理技术,用于消除重叠的边界框。它通常应用于像FASTER RCNN这样的区域提议网络,目的是从大量的候选框中筛选出最具有代表性的物体框。...
Tiny : 648B (minified, gzipped) 由size-limit监控。 如果您需要更多控制,则公开onResize 回调。 box 选项。 与SSR一起使用。 适用于CSS-in-JS。 支持自定义 refs,以防您已经拥有一个。 默认使用 RefCallback来...
- Time Limit Exceeded (TLE):程序在规定时间内未能完成执行,可能是算法效率低下导致。 - Memory Limit Exceeded (MLE):程序运行时超过了内存限制。 - Runtime Error (RE):程序运行时出现错误,如除零错误、...
windows2008默认是禁ping的,需要开通方可被ping
vue-response-components使用ResizeObserver创建响应组件。 想法请查看我在ITNEXT安装上的帖子npm install vue-sensitive-c vue-response-components使用ResizeObserver创建响应性组件。 想法看看我在ITNEXT安装上的...
该系统可以自动编译、运行和评估程序的输出结果,提供了多种判题结果,包括 Accept、Presentation Error、Time Limit Exceeded、Memory Limit Exceeded、Wrong Answer、Runtime Error、Output Limit Exceeded、...